摘要:動態產生Button並繫結Click事件
protected
void
Page_Load(
object
sender, EventArgs e)
{
for
(
int
i = 1; i <= 10; i++)
{
LinkButton olbtn =
new
LinkButton();
olbtn.ID =
"lbtn"
+ i;
olbtn.Text =
"測試按鈕"
+ i;
olbtn.Click +=
new
EventHandler(lbtn_Click); //按TAB會自動幫你產生函式
PlaceHolder1.Controls.Add(olbtn);
Label olab =
new
Label();
olab.Text =
" | "
;
PlaceHolder1.Controls.Add(olab);
}
}
/// <summary>
/// 動態連結按鈕事件
/// </summary>
/// <param name="sender">
/// <param name="e">
protected
void
lbtn_Click(
object
sender, EventArgs e)
{
LinkButton lbtn = (LinkButton)sender;
Response.Write(
"按鈕事件觸發:"
+ lbtn.ID);
}