显示 HTML 动态按钮

发布于 2024-08-04 04:11:50 字数 1578 浏览 2 评论 0原文

我正在尝试在 c# 中生成动态 html 按钮,但是当我运行代码时正在生成按钮。我正在尝试以两种不同的方式生成动态按钮,但没有一种方法适合我。如果代码有任何问题,请告诉我。我在 page _Load 和 page_init 事件中尝试过。这是我正在使用的代码......

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);

}

I am trying to generate a dynamic html button in c#,But buttons are being getting generated when i ran the code.I am trying to generate a dynamic buttons in 2 different ways but none is working out for me. Please let me if anything wrong in code.I tried in page _Load and page_init events.Here is my code i am using it .........

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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2024-08-11 04:11:50

看起来您缺少将 tblControls 添加到 Page.Controls 集合的语句。您可能还打算将动态按钮添加到 tdControl1 控件中。

It looks like you're missing the statement where you add tblControls to the Page.Controls collection. You probably also intend for the dynamic button to be added to the tdControl1 control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文