我正在尝试在c#中生成一个动态html按钮,但是当我运行代码时,按钮正在生成。我正在尝试以两种不同的方式生成动态按钮,但没有一个对我有用。如果代码中有任何错误,请告诉我。我在页面_Load和page_init events中试过。这是我的代码我正在使用它.........
protected void page_Init(object sender,EventArgs e)
{
Table tblControls = new Table();
TableRow trControl1 = new TableRow();
TableCell tdControl1 = new TableCell();
tdControl1.Text = "<input id='Submit" + "' runat='server' type='button' value='Submit' onclick='alert('Yahooooo');' onserverclick='Submit_Click'></input>";
trControl1.Cells.Add(tdControl1);
tblControls.Rows.Add(trControl1);
int i = 1;
string strMessage = "hi";
HtmlButton htmSubmit = new HtmlButton();
htmSubmit.ID = "Submit" + i;
htmSubmit.InnerHtml = "Submit";
htmSubmit.Attributes.Add("onclick", "alert('Yahoooooo');");
htmSubmit.Attributes.Add("GuestId", "GuestUserId" + i.ToString());
htmSubmit.Attributes.Add("UserId", "UserId" + i.ToString());
htmSubmit.Attributes.Add("Message", strMessage);
htmSubmit.ServerClick += new System.EventHandler(this.Submit_Click);
}
protected void Submit_Click(object sender, EventArgs e)
{
HtmlButton btn = (HtmlButton)sender;
string strGuestUserId = btn.Attributes["GuestId"];
string strUserId = btn.Attributes["UserId"];
string strMessage = btn.Attributes["Message"];
Response.Write("Your have clicked " + btn.ID.ToString() + "<br/>" + strGuestUserId + "<br/>" + strUserId + "<br/>" + strMessage);
}
答案 0 :(得分:1)
看起来你错过了将tblControls添加到Page.Controls集合的语句。您可能还打算将动态按钮添加到tdControl1控件。