如何在asp.NET中的GridView中实现嵌套的DropDownList?
我有两个嵌套的 DropDownList(第一个 ddl 中选择的值将限制第二个 ddl 的内容)。
此处附加的示例在 GridView 之外运行良好。当我尝试将其插入 GridView 时,我收到错误,因为我无法识别要在 ControlParameter 中使用的 ControlID。
GridView 外部的代码如下所示:
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
数据源如下所示:
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [Items]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [SubItems] WHERE [itemId]=:v_ItemId">
<SelectParameters>
<asp:ControlParameter Name="v_ItemId" ControlID="ACTION1_DROPDOWNLIST" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
谢谢!
I have two nested DropDownLists (the value selected in the 1st ddl will limit the content of the 2nd ddl).
The example attached here runs fine outside of a GridView. As soon as I try to insert this into a GridView, I get an error because I cannot identify the ControlID to use in the ControlParameter.
The code outside of the GridView looks like this:
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
And the DataSources look like this:
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [Items]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [SubItems] WHERE [itemId]=:v_ItemId">
<SelectParameters>
<asp:ControlParameter Name="v_ItemId" ControlID="ACTION1_DROPDOWNLIST" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试过类似的东西并且效果很好。我无法从您的帖子中判断数据源是否与 GridView 的 ItemTemplate 内的下拉列表一起。至少第二个应该位于 ItemTemplate 内,以便网格中的每一行都有一个数据源。
我已经在下面发布了我的页面。它使用不同的表,但想法相同。
I have tried something similar and it works fine. I can't tell from your post whether the data sources are together with the dropdowns inside the ItemTemplate of the GridView. At least the second one should be inside the ItemTemplate so that you will have a data source for each line in the grid.
I have posted my page below. It's using different tables, but it's the same idea.
实际的 id 是根据它们所在的位置构建的,因此您将看到类似 $GridView1 的内容插入到 id 中。
要解决此问题,您可以在页面属性(.aspx 文件的第一行)上设置
ClientIDMode="Static"
。或者使用
"javascript:var a = document.getElementById('" + ACTION1_DROPDOWNLIST.ClientID + "');
The actual id's are build based on where they are, so you will see something like $GridView1 inserted in the id's.
To solve this you can set
ClientIDMode="Static"
on your page properties (first line of your .aspx file).Or use
"javascript:var a = document.getElementById('" + ACTION1_DROPDOWNLIST.ClientID + "');