none
Create controls dynamically on button click RRS feed

  • Question

  • Hi,

    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>
     

    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? 
     


    • Edited by vickyto Monday, January 8, 2018 6:37 AM
    • Moved by Sabah ShariqMVP Monday, January 8, 2018 10:36 AM Moved From Visual C#
    Monday, January 8, 2018 6:05 AM

All replies

  • There is a dedicated forum here for ASP.NET

    https://forums.asp.net/


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 8, 2018 10:25 AM