記錄一下使用C#來動態產生Button控制項並繫結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);
PlaceHolder1.Controls.Add(olbtn);
Label olab = new Label();
olab.Text = " | ";
PlaceHolder1.Controls.Add(olab);
}
}
///
/// 動態連結按鈕事件
///
///
///
protected void lbtn_Click(object sender, EventArgs e)
{
LinkButton lbtn = (LinkButton)sender;
Response.Write("按鈕事件觸發:" + lbtn.ID);
}
沒有留言:
張貼留言