[ASP.NET][Event] 當事件超過一個時判斷觸發事件

摘要:[ASP.NET][EventHandler]當事件超過一個時判斷觸發事件

 

================================================
 Alex70386 寫信:

如果textBox1和textBox2 共用一個 EventHandler
可以利用TextChanged(object sender, EventArgs e)中的 object sender
來查詢是 textBox1觸發的 還是 textBox2 

請問如何判斷 ""來查詢是 textBox1觸發的 還是 textBox2 """???

sender.tostring   or  ???? 

謝謝

Alex
 

===============================================
MyAnswer :


 

01 //sender強制轉換型別(這麼想會比較好記 ==> 事件物件實體化)
02
03
04 protected void tbAM_TextChanged(object sender, EventArgs e)
05     {
06         TextBox tb = (TextBox)sender;
07         try
08         {            
09             if (tb.ID == "tbAM")
10             {
11                 DropDownList ddl1 = (DropDownList)FindControl("ddlMeterial" + tb.ID.Substring(2, 1) + "1");
12                 DropDownList ddl2 = (DropDownList)FindControl("ddlMeterial" + tb.ID.Substring(2, 1) + "2");
13                 ddl1.Visible = true;
14                 ddl2.Visible = true;
15             }

16         }

17         catch
18         {
19             tb.Text = "Error";
20         }

21     }

_______________________________

Darren.NET