Click here to Skip to main content
14,699,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 9-Jan-18 19:46pm
v6
Comments
Ram Nunna 8-Jan-18 3:16am
   
Hi Vignesh,
Please refer below link this will help you to how to implement same functionality.
https://www.codeproject.com/Questions/355847/how-to-create-dynamic-textbox-dropdown-list-in-asp
   
pnlSort should be a panel or group box
Add the option in dropdownlist .rest likes to be ok in your code
   
in html id is pnlSrt but in code behind id is pnlSort

1 solution

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" />


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
}



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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900