单击按钮动态创建控件
Create controls dynamically 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>
代码:
单击 AddSorting 时,我尝试使用以下代码创建下拉控件,但控件未显示。
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);
}
图片:
pnlSrt
设置为 display: none
因此动态添加的子控件也不会显示。
根据@Yoshi 的回答。您可以使用 pnlSort.Style["display"] = "block";// or pnlSort.Attributes.Add("style", "display:block");
为 pnlSrt
控件设置可见 true
试试这个
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");
}
我正在尝试在单击按钮时创建动态控件。
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>
代码:
单击 AddSorting 时,我尝试使用以下代码创建下拉控件,但控件未显示。
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);
}
图片:
pnlSrt
设置为 display: none
因此动态添加的子控件也不会显示。
根据@Yoshi 的回答。您可以使用 pnlSort.Style["display"] = "block";// or pnlSort.Attributes.Add("style", "display:block");
pnlSrt
控件设置可见 true
试试这个
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");
}