DropDownList控制元件的基本用法

wangjun_pfc發表於2007-10-04

 

 

protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            DateTime dt 
= DateTime.Now;
            DropDownList2.Items.Add(dt.Year.ToString());
            DropDownList3.Items.Add(dt.Month.ToString());
            DropDownList4.Items.Add(dt.Day.ToString());
            DropDownList4.Items.Add(
"23");
        }

       
    }

//加減乘除
    protected void Button1_Click(object sender, EventArgs e)
    
{
        
int a = int.Parse(TextBox1.Text);
        
int b = int.Parse(TextBox2.Text);
        
int c = 0;

        
switch(DropDownList1.SelectedValue)
        
{
            
case "+":
                c 
= a + b;
                Label2.Text 
= c.ToString();
                
break;
            
case "-":
                c 
= a - b;
                Label2.Text 
= c.ToString();
                
break;
            
case "*":
                c 
= a * b;
                Label2.Text 
= c.ToString();
                
break;
            
case "/":
                c 
= a / b;
                Label2.Text 
= c.ToString();
                
break;
        }

    }

//DropDownList 改變index時發生的事件
    protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
    
{
        Response.Write(DropDownList2.SelectedValue
+"-"+ DropDownList3.SelectedValue+"-"+DropDownList4.SelectedValue);
    }


 

//CheckBoxList 改變index時發生的事件
 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        
{
            
if (CheckBoxList1.Items[i].Selected)   //注意啟用postback,這樣可以實時觀測
            {
                Response.Write(CheckBoxList1.Items[i].Text);
            }

        }

    }


 

 

 

相關文章