首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FormView中的依赖DropDownList

FormView中的依赖DropDownList
EN

Stack Overflow用户
提问于 2013-05-22 04:28:13
回答 2查看 2.1K关注 0票数 0

我正尝试在一个窗体视图中创建两个下拉列表,其中第二个下拉列表中显示的值取决于第一个下拉列表。第一个下拉列表包含down.The表"Classes“中包含的类号,第二个下拉列表包含同一个SQL表"Classes”中的类节。我希望能够选择一个班级号,并且只弹出与该班级号对应的部分。

示例Classes表:

代码语言:javascript
复制
Number:   Section:   SLN:  
210       1          A-1  
210       2          A-2  
210       3          A-3  
340       1          B-1  
340       7          B-7 

我现在已经有了第一个正确设置号码的列表。

代码语言:javascript
复制
   <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

到目前为止,我已经尝试使用

代码语言:javascript
复制
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>

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

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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>

使用上面的示例表的运行中的表单示例:

代码语言:javascript
复制
Number Drop Down:
210                ->   select 210     
340  

Section Drop Down:
1
2                  -> select 3
3
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-22 05:49:38

看看这篇文章:

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

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

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

票数 2
EN

Stack Overflow用户

发布于 2013-05-23 04:13:16

我知道如何做到这一点:我的C#代码现在如下所示:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<asp:DropDownList ID="ddlNumber" runat="server" DataSourceID="SqlDSClasses"  AutoPostBack="True" DataTextField="Number" DataValueField="Number"   onselectedindexchanged="ddlNumber_SelectedIndexChanged">
</asp:DropDownList>

ddlSection:

代码语言:javascript
复制
<asp:DropDownList ID="ddlSection" runat="server"  AutoPostBack="True"                                                     onselectedindexchanged="ddlSection_SelectedIndexChanged">
</asp:DropDownList>

我是结合Nikita http://csharpdotnetfreak.blogspot.com/2009/03/populate-dropdown-based-selection-other.html的链接发现的。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16678930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档