按钮单击动态创建控件

编程入门 行业动态 更新时间:2024-10-18 16:43:58
本文介绍了按钮单击动态创建控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

I need to create dynamic controls on button click. HTML: <asp:LinkButton ID="lnkSrt" runat="server" Text="Multi Sort" ForeColor="Black" Font-Names="Calibri" Font-Size="10pt" /> <ajax:ModalPopupExtender runat="server" ID="mpeSrt" TargetControlID="lnkSrt" PopupControlID="pnlSrt" CancelControlID="btnClose"> </ajax:ModalPopupExtender> <asp:UpdatePanel ID="upPanel" runat="server"> <ContentTemplate> <asp:Panel ID="pnlSrt" runat="server" align="center" Style="display: none" BackColor="LightGray"> <asp:Label runat="server" Text="Sort By Column" /> <asp:DropDownList ID="ddlColumnSort" runat="server" AutoPostBack="true" /> <asp:Label runat="server" Text="Order By" /> <asp:DropDownList ID="ddlOrder" runat="server" AutoPostBack="true"> <asp:ListItem Text="Ascending" Value="0"></asp:ListItem> <asp:ListItem Text="Descending" Value="1"></asp:ListItem> </asp:DropDownList> <asp:LinkButton ID="lnkAdd" runat="server" Text="Add Sorting" Font-Underline="true" OnClick="lnkAddOrder_Click" Font-Names="Calibri" Font-Size="10pt" ForeColor="Black" /> <asp:Button ID="btnSorting" runat="server" Text="Sort" OnClick="btnSorting_Click" /> <asp:Button ID="btnClose" runat="server" Text="Close" /> </asp:Panel> </ContentTemplate> </asp:UpdatePanel>

我的尝试:

What I have tried:

CODE: When clicked on AddSorting I am trying to create dropdown control using below code and the controls doesn't show. protected void lnkAddSort_Click(object sender, EventArgs e) { int index = pnlSort.Controls.OfType<DropDownList>().ToList().Count + 1; this.Createddl("ddldyn" + index); } public void Createddl(string id) { DropDownList ddl = new DropDownList(); ddl.ID = id; pnlSort.Controls.Add(ddl); Literal lt = new Literal(); lt.Text = "<br />"; pnlSort.Controls.Add(lt); }

Can anyone please provide suggestions on how to handle this?

推荐答案

您是否尝试调试代码以查看它是否符合 OnClick 事件?查看您的代码,基于以下这一行: Have you tried debugging your code to see if it hits the OnClick event? Looking at your code, based on this line: <asp:LinkButton ID="lnkAdd" runat="server" Text="Add Sorting" Font-Underline="true" OnClick="lnkAddOrder_Click" Font-Names="Calibri" Font-Size="10pt" ForeColor="Black" />

你有线了 OnClick 事件到您的 LinkBut​​ton ,其名称为 lnkAddOrder_Click 但你的代码后面使用名称 lnkAddSort_Click 。您应该更改事件名称以匹配您在标记/ HTML中设置的事件处理程序。所以你应该有类似的东西:

You wired-up an OnClick event to your LinkButton which is named "lnkAddOrder_Click" but your code behind uses the name "lnkAddSort_Click". You should change the event name to match with the event handler you set in your markup/HTML. So you should have something like this instead:

protected void lnkAddOrder_Click(object sender, EventArgs e) { //your code here }

你也可以参考这个例子来讨论如何 [ ^ ]在ASP.NET WebForms中创建动态控件:

You can also refer this example on how to [^]create dynamic controls in ASP.NET WebForms:

更多推荐

按钮单击动态创建控件

本文发布于:2023-11-02 14:22:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   单击   按钮   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!