0

I am trying 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="pnlSort" 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);
}

Image:

enter image description here

2 Answers 2

2

pnlSrt is set to display: none so the dynamically added child controls will not show either.

Sign up to request clarification or add additional context in comments.

1 Comment

It works now. The controls is visible when I click on the first time, if I click on the button for 2nd time the second dropdown is not getting added. Updated post with image for better clarity. Also the dropdown is not getting aligned correct to the first row.
0

As per the @Yoshi answer. You could set visible true for the pnlSrt control by using pnlSort.Style["display"] = "block";// or pnlSort.Attributes.Add("style", "display:block");

Try this

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);
    pnlSort.Attributes.Add("style", "display:block");
}

1 Comment

It works now. The controls is visible when I click on the first time, if I click on the button for 2nd time the second dropdown is not getting added. Updated post with image for better clarity. Also the dropdown is not getting aligned correct to the first row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.