依赖于FormView的下拉列表

本文关键字:下拉列表 FormView 依赖于 | 更新日期: 2023-09-27 18:14:37

我试图在表单视图中创建两个下拉列表,其中在第二个下拉列表中显示的值取决于第一个下拉列表。第一个列表包含sql表"Classes"中包含的类号,第二个下拉列表包含同一个sql表"Classes"中的类部分。我希望能够选择一个类号,只有相应的部分,类号弹出。

示例类表:

Number:   Section:   SLN:  
210       1          A-1  
210       2          A-2  
210       3          A-3  
340       1          B-1  
340       7          B-7 

我目前有第一个列表的数字设置正确使用

   <asp:DropDownList ID="ddlNumber" runat="server" 
    DataSourceID="SqlDSClasses"
     AutoPostBack="True" DataTextField="Number" DataValueField="Number">
    </asp:DropDownList> 
for the drop down and
<asp:SqlDataSource ID="SqlDSClasses" runat="server"       
ConnectionString="<%$ ConnectionStrings:ReinstatementCS %>" 
SelectCommand="SELECT [Prefix], [Number], [Location], [SLN],                              
[StartTime], [EndTime], [ClassDay], [ClassCredit], [ClassSection] FROM [Classes]">   
</asp:SqlDataSource>
for the corresponding SqlDataSource
到目前为止,我一直在尝试使用
SELECT [ClassSection] FROM [Classes] WHERE [Number] = NumberDropDownList.DataValueField
for the Section list:
<asp:DropDownList ID="ClassSectionDropDownList" runat="server" DataSourceID="SqlDSNumSec" 
                                            DataTextField="ClassSection" DataValueField="ClassSection"  AutoPostBack="True">
                                        </asp:DropDownList>

使用的表单视图设置如下:

<asp:FormView ID="FVStudentClass" runat="server" DataSourceID="SqlDSStudentClass"
                        DataSourceID2="SqlDSAccess" EmptyDataText="Student Class Not Completed">
                        <EditItemTemplate>
                            <table style="width:100%;">
                                <tr>
                                    <td>
                                        Prefix:
                                    </td>
                                    <td>
                                        UCOLL</td>
                                    <td>
                                        Number:
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddlNumber" runat="server" DataSourceID="SqlDSClasses"  AutoPostBack="True"
                                            DataTextField="Number" DataValueField="Number">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        Section:
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="SectionDropDownList" runat="server" DataSourceID="SqlDSNumSec" 
                                            DataTextField="Section" DataValueField="Section"  AutoPostBack="True">
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                            </table>
                            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                                Text="Update" />
                            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
                                CommandName="Cancel" Text="Cancel" />
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            IsTransfer:
                            <asp:CheckBox ID="IsTransferCheckBox" runat="server" 
                                Checked='<%# Bind("IsTransfer") %>' />
                            <br />
                            Prefix:
                            <asp:TextBox ID="PrefixTextBox" runat="server" Text='<%# Bind("Prefix") %>' />
                            <br />
                            Number:
                            <asp:TextBox ID="NumberTextBox" runat="server" Text='<%# Bind("Number") %>' />
                            <br />
                            Section:
                            <asp:TextBox ID="SectionTextBox" runat="server" 
                                Text='<%# Bind("Section") %>' />
                            <br />
                            ID:
                            <asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("ID") %>' />
                            <br />
                            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                Text="Insert" />
                            &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
                                CommandName="Cancel" Text="Cancel" />
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <table style="width:100%;">
                                <tr>
                                    <td>
                                        Prefix:</td>
                                    <td>
                                        <asp:Label ID="PrefixLabel" runat="server" Text='<%# Bind("Prefix") %>' />
                                    </td>
                                    <td>
                                        Number:</td>
                                    <td>
                                        <asp:Label ID="NumberLabel" runat="server" Text='<%# Bind("Number") %>' />
                                    </td>
                                    <td>
                                        ClassSection:</td>
                                    <td>
                                        <asp:Label ID="ClassSectionLabel" runat="server" 
                                            Text='<%# Bind("ClassSection") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        IsTransfer:</td>
                                    <td>
                                        <asp:CheckBox ID="IsTransferCheckBox" runat="server" 
                                            Checked='<%# Bind("IsTransfer") %>' Enabled="false" />
                                    </td>
                                </tr>
                            </table>
                            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="Edit" />
                        </ItemTemplate>
                    </asp:FormView>

使用SqlDataSource:

<asp:SqlDataSource ID="SqlDSStudentClass" runat="server" ConnectionString="<%$ ConnectionStrings:ReinstatementCS %>"
                        DeleteCommand="DELETE FROM [StudentClass] WHERE [SLN] = @SLN" 
                        InsertCommand="INSERT INTO [StudentClass] ([SLN],[ID]) VALUES (@SLN, @ID)"
                        SelectCommand="SELECT DISTINCT Classes.Number, Classes.Section, StudentClass.ID
FROM 
    StudentClass 
    LEFT JOIN AccessList ON AccessList.ALID = StudentClass.ID
    JOIN Classes ON StudentClass.SLN = Classes.SLN
WHERE ([SLN] = @SLN)" 
    <!--AccessList just gives extra information to the user-->                    
                        UpdateCommand="UPDATE [StudentClass] SET [SLN] = @SLN, [ID] = @ID WHERE [SLN] = @SLN">
                        <DeleteParameters>
                            <asp:Parameter Name="SLN" Type="Int32" />
                        </DeleteParameters>
                        <InsertParameters>
                            <asp:Parameter Name="SLN" Type="String" />
                            <asp:Parameter Name="ID" Type="String" />
                        </InsertParameters>
                        <SelectParameters>
                            <asp:QueryStringParameter Name="ID" QueryStringField="ALID" 
                                Type="String" />
                        </SelectParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="ID" Type="String" />
                            <asp:Parameter Name="SLN" Type="Int32" />
                        </UpdateParameters>
                    </asp:SqlDataSource>

使用上面的示例表的表单示例:

Number Drop Down:
210                ->   select 210     
340  
Section Drop Down:
1
2                  -> select 3
3

依赖于FormView的下拉列表

看看这篇文章:

http://csharpdotnetfreak.blogspot.com/2009/03/populate-dropdown-based-selection-other.html

它解释了如何实现你想要的。

另外,您也可以使用AJAX: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx

我知道怎么做了:我的c#代码现在看起来像这样:

protected void ddlNumber_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddlNumber = FVStudentClass.FindControl("ddlNumber") as DropDownList;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ReinstatementCS"].ConnectionString);
    SqlCommand myCommand = new SqlCommand("SELECT DISTINCT ClassSection FROM Classes WHERE Number = " + ddlNumber.Text);
    myCommand.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(myCommand);
    DataTable dt = new DataTable();
    da.Fill(dt);
    DropDownList ddlSection = FVStudentClass.FindControl("ddlSection") as DropDownList;
    ddlSection.DataSource = dt;
    ddlSection.DataTextField = "ClassSection";
    ddlSection.DataValueField = "ClassSection";
    ddlSection.DataBind();
    ddlSection.Items.Insert(0, "--Select--");
}

和我的。aspx下拉列表:ddlNumber:

<asp:DropDownList ID="ddlNumber" runat="server" DataSourceID="SqlDSClasses"  AutoPostBack="True" DataTextField="Number" DataValueField="Number"   onselectedindexchanged="ddlNumber_SelectedIndexChanged">
</asp:DropDownList>

ddlSection:

<asp:DropDownList ID="ddlSection" runat="server"  AutoPostBack="True"                                                     onselectedindexchanged="ddlSection_SelectedIndexChanged">
</asp:DropDownList>

我结合尼基塔的链接找到了这个http://csharpdotnetfreak.blogspot.com/2009/03/populate-dropdown-based-selection-other.html

和另一页:http://forums.asp.net/t/1617449.aspx(忽略第一个答案中的所有div)