下拉列表更改基于详细视图ASP.NET中的其他下拉列表

时间:2016-11-03 17:08:41

标签: asp.net dropdown detailsview

我在详细信息视图中有两个下拉列表,一个名为College,另一个是Department。如果用户选择大学,则部门下拉列表应生成所选大学的所有部门。

以下是详情视图和下拉列表:

    <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="600px" AutoGenerateRows="False" CssClass="table table-bordered mtop" DataKeyNames="ID" DataSourceID="SqlDataSource1" OnDataBound="DetailsView1_DataBound">
                <FieldHeaderStyle CssClass="DetailsViewHeader" Width="200px" />
                <Fields>

                  <asp:TemplateField HeaderText="College" SortExpression="Colleges">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3" DataTextField="ArName" DataValueField="Code"></asp:DropDownList>
                            <asp:HiddenField ID="HiddenColl" runat="server" value='<%# Eval("Colleges") %>' /> 
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label15" runat="server" Text='<%# Bind("Colleges") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                  <asp:TemplateField HeaderText="Department" SortExpression="ArName">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="ArName" DataValueField="Code"></asp:DropDownList>
                            <asp:HiddenField ID="HiddenDep" runat="server" value='<%# Eval("ArName") %>' />                       
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label11" runat="server" Text='<%# Bind("ArName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                   <asp:TemplateField ShowHeader="False">
                        <EditItemTemplate>
                            <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
                            &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
                            &nbsp;<asp:Button ID="btnDelete" runat="server" CausesValidation="False" Text="Delete"  OnClientClick="return confirm('Do you want to delete ?');" OnClick="btnDelete_Click" />
                        </ItemTemplate>
                        <ControlStyle CssClass="btn-login" />
                        <ItemStyle CssClass="text-center" />
                    </asp:TemplateField>

           </Fields>
        </asp:DetailsView>

这是SqlDataSources:

<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:UEDConnectionStringMarwaMarwa %>" SelectCommand="SELECT [ArName], [Code] FROM [College]"></asp:SqlDataSource>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:UEDConnectionStringMarwaMarwa %>" SelectCommand="SELECT [Code], [ArName] FROM [Department] WHERE ([CollegeCode] = @CollegeCode)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DetailsView1$DropDownList2" Name="CollegeCode" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
</asp:SqlDataSource>

这是错误消息:

  

无法找到控件&#39; DetailsView1 $ DropDownList2&#39;在   ControlParameter&#39; CollegeCode&#39;

我执行了此操作DetailsView1$DropDownList2,因此可以访问详细信息视图中的下拉列表

有什么问题?

1 个答案:

答案 0 :(得分:0)

我不熟悉DetailsView而没有使用SqlDataSource,但我会尽力帮助。

根据我的经验,我了解到你不应该试图控制ClientID。所以,首先我建议从服务器控件获取ClientID。 使用内联表达式,我会尝试这样的事情:

<asp:ControlParameter ControlID='<%= DetailsView1.Rows(0).FindControl("DropDownList2").ClientID %>' Name="CollegeCode" PropertyName="SelectedValue" Type="Int32" />

有关asp.net内联表达式的更多信息,请参阅Introduction to ASP.NET inline expressions in the .NET Framework

因为我不熟悉DetailsView控件,所以我不知道DetailsView执行此内联表达式时会在索引0处有一行(这会引发异常) 。

如果该代码不起作用,我建议在ControlID行绑定后(例如在DetailsView1内部,在后端动态设置DetailsView1 DataBound事件方法)。

<强> VB:

If DetailsView1.Rows.Count > 0 Then
    Dim objDropDownList2 as Control = DetailsView1.Rows(0).FindControl("DropDownList2")
    If objDropDownList2 IsNot Nothing Then
        SqlDataSource1.SelectParameters("CollegeCode").ControlID = objDropDownList2.ClientID
    End If  
End If

<强> C#:

if (DetailsView1.Rows.Count > 0) {
    Control objDropDownList2 = DetailsView1.Rows(0).FindControl("DropDownList2");
    if (objDropDownList2 != null) {
        SqlDataSource1.SelectParameters("CollegeCode").ControlID = objDropDownList2.ClientID;
    }
}

我希望这有帮助!