如何在asp.NET中的GridView中实现嵌套的DropDownList?

发布于 2024-09-08 14:31:50 字数 1837 浏览 7 评论 0原文

我有两个嵌套的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千笙结 2024-09-15 14:31:50

我尝试过类似的东西并且效果很好。我无法从您的帖子中判断数据源是否与 GridView 的 ItemTemplate 内的下拉列表一起。至少第二个应该位于 ItemTemplate 内,以便网格中的每一行都有一个数据源。

我已经在下面发布了我的页面。它使用不同的表,但想法相同。

<form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="ACTION1_DROPDOWNLIST" 
                        AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"  
                        DataSourceID="SqlDataSource1" DataTextField="Surname" DataValueField="FamilyID" > 
                        <asp:ListItem Value=""></asp:ListItem> 
                    </asp:DropDownList> 
                    <asp:DropDownList ID="ACTION1_DROPDOWNLIST2"  
                        ToolTip="Dropdown List" runat="server" CssClass="Select"  
                        DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="PersonID" > 
                        <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="SqlDataSource2" runat="server"  
                            ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"  
                            ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"  
                            SelectCommand="SELECT * FROM [Person] WHERE ([FamilyId] = @FamilyId)"> 
                            <SelectParameters> 
                                <asp:ControlParameter Name="FamilyId" ControlID="ACTION1_DROPDOWNLIST" 
                                    PropertyName="SelectedValue" Type="Int32" /> 
                            </SelectParameters> 
                        </asp:SqlDataSource> 
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:LinqDataSource ID="LinqDataSource1" runat="server">
    </asp:LinqDataSource>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"  
    ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"  
    ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"  
    SelectCommand="SELECT * FROM [Family]"> 
</asp:SqlDataSource> 
</form>

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.

<form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="ACTION1_DROPDOWNLIST" 
                        AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"  
                        DataSourceID="SqlDataSource1" DataTextField="Surname" DataValueField="FamilyID" > 
                        <asp:ListItem Value=""></asp:ListItem> 
                    </asp:DropDownList> 
                    <asp:DropDownList ID="ACTION1_DROPDOWNLIST2"  
                        ToolTip="Dropdown List" runat="server" CssClass="Select"  
                        DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="PersonID" > 
                        <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="SqlDataSource2" runat="server"  
                            ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"  
                            ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"  
                            SelectCommand="SELECT * FROM [Person] WHERE ([FamilyId] = @FamilyId)"> 
                            <SelectParameters> 
                                <asp:ControlParameter Name="FamilyId" ControlID="ACTION1_DROPDOWNLIST" 
                                    PropertyName="SelectedValue" Type="Int32" /> 
                            </SelectParameters> 
                        </asp:SqlDataSource> 
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:LinqDataSource ID="LinqDataSource1" runat="server">
    </asp:LinqDataSource>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"  
    ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"  
    ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"  
    SelectCommand="SELECT * FROM [Family]"> 
</asp:SqlDataSource> 
</form>
花桑 2024-09-15 14:31:50

实际的 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 + "');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文