當你要綁定一個處理程序在代碼隱藏的事件,實際上做的是寫的處理程序本身的名稱,而不是一個字符串
protected void Page_Load(object sender, EventArgs e)
{
TextBox textBox = new TextBox();
textBox.TextChanged += new EventHandler(textBox_TextChanged);
}
protected void textBox_TextChanged(object sender, EventArgs e)
{
// Your code here
}
爲了更加清楚一點,想象一下C#有一個名爲EventHandler
的列表,並且每次在文本框(客戶端模糊事件)上更改文本時,C#都將執行該列表中的所有方法。現在,你如何添加一個方法到列表中?您使用+=
運算符。現在,如果你想添加兩個處理器,你可以寫:
protected void Page_Load(object sender, EventArgs e)
{
TextBox textBox = new TextBox();
textBox.TextChanged += new EventHandler(textBox_TextChanged);
textBox.TextChanged += new EventHandler(textBox_TextChanged2);
}
protected void textBox_TextChanged(object sender, EventArgs e)
{
// This method is the first in the list. So gets executed first.
}
protected void textBox_TextChanged2(object sender, EventArgs e)
{
// This method is the second in the list.
}
它不是爲我工作。我的問題@ http://stackoverflow.com/questions/39293861/how-to-fire-textboxchanged-event-on-dynamically-added-textbox-in-gridview –