ASP.Net:动态创建的下拉列表和数据源
我有大约 10 个已填充的下拉列表控件。 我想以编程方式创建它们,而不是复制/粘贴和修改每个字段的一些字段。 这可以做到吗?
这是其中之一的样子。 我想以编程方式添加 10 个下拉列表控件及其各自的 SqlDataSource 控件。
<asp:SqlDataSource ID = "ddlDAGender" runat=server
ConnectionString="<%$ ConnectionStrings:test1ConnectionString %>"
SelectCommand = "select GenderID,Gender from mylookupGender"
>
</asp:SqlDataSource>
<asp:Label ID="Label3" runat="server" Text="Gender"></asp:Label>
<asp:DropDownList ID="ddlGender" runat="server"
DataSourceid="ddlDAGender"
DataTextField="Gender" DataValueField="GenderID"
>
</asp:DropDownList>
I have about 10 drop down list controls that get populated. Instead of copying/pasting and modifying a few fields on each, I would like to create them programmatically. Can this be done?
Here is what one of them looks like. I would like to programmatically add 10 dropdownlist controls and their respective SqlDataSource controls.
<asp:SqlDataSource ID = "ddlDAGender" runat=server
ConnectionString="<%$ ConnectionStrings:test1ConnectionString %>"
SelectCommand = "select GenderID,Gender from mylookupGender"
>
</asp:SqlDataSource>
<asp:Label ID="Label3" runat="server" Text="Gender"></asp:Label>
<asp:DropDownList ID="ddlGender" runat="server"
DataSourceid="ddlDAGender"
DataTextField="Gender" DataValueField="GenderID"
>
</asp:DropDownList>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以动态创建控件。 但在这种情况下,如果您所有的下拉列表都不同,我不确定它是否会为您节省任何内容,因为您仍然需要为它们分配单独的 ID 和数据源。
代码可能如下所示:
You can always create your controls dynamically. In this case though, if all your drop down lists are different, I'm not sure it's saving you anything, since you'll still have to assign them individual ID's and datasources.
Here's what the code might look like: