绑定下拉列表到下拉列表到detailsview净c#

本文关键字:下拉列表 detailsview 绑定 | 更新日期: 2023-09-27 17:53:32

我试图将下拉列表绑定到页面中的Detailsview控件,其中第二个下拉列表的结果取决于第一个。我可以绑定一个单一的下拉列表到Detailsview,但如果我添加另一个下拉列表,其中的结果取决于第一个细节视图不会显示任何东西,除非你刷新页面。

那么,如果第二个下拉菜单被选中,是否有任何方法可以自动发布detailsview的结果?我目前正在研究这个,但我不能得到它。

下面是我的示例代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="LibrarySystem.Member.Search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <h3>
        Search</h3>
    <p>
        Choose a category
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="categoryDataSource" DataTextField="name" 
            DataValueField="categoryid">
        </asp:DropDownList>
        <asp:SqlDataSource ID="categoryDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
            SelectCommand="SELECT categoryid, name FROM dbo.TblCategory">
        </asp:SqlDataSource>
    </p>
    <p>
        Choose a title
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
            DataSourceID="bookDataSource" DataTextField="booktitle" DataValueField="bookid">
        </asp:DropDownList>
        <asp:SqlDataSource ID="bookDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
            SelectCommand="SELECT [categoryid], [booktitle], [bookid] FROM [TblBooks] WHERE ([categoryid] = @categoryid)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="categoryid" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </p>
    <p>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            CellPadding="4" DataKeyNames="bookid" DataSourceID="bookdetailsDataSource" 
            ForeColor="#333333" GridLines="None">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
            <RowStyle BackColor="#EFF3FB" />
            <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <Fields>
                <asp:BoundField DataField="bookid" HeaderText="ID/ISBN" ReadOnly="True" 
                    SortExpression="bookid" />
                <asp:BoundField DataField="booktitle" HeaderText="Title" 
                    SortExpression="booktitle" />
                <asp:BoundField DataField="lastname" HeaderText="Author" 
                    SortExpression="lastname" />
                <asp:BoundField DataField="firstname" HeaderText="" 
                    SortExpression="firstname" />
                <asp:BoundField DataField="description" HeaderText="Description" 
                    SortExpression="description" />
                <asp:BoundField DataField="name" HeaderText="Category" SortExpression="name" />
                <asp:BoundField DataField="dateadded" HeaderText="Date added" 
                    SortExpression="dateadded" />
                <asp:BoundField DataField="quantity" HeaderText="No. of copies" 
                    SortExpression="quantity" />
                <asp:BoundField DataField="EmployeeID" HeaderText="Reserved by" 
                    SortExpression="EmployeeID" />
                <asp:BoundField DataField="reservedate" HeaderText="Reserved date" 
                    SortExpression="reservedate" />
                <asp:BoundField DataField="statusname" HeaderText="Status" 
                    SortExpression="statusname" />
            </Fields>
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:DetailsView>

任何帮助都将非常感激;)

Thanks in advance

绑定下拉列表到下拉列表到detailsview净c#

你必须把你的Dependent下拉数据源放在父模板字段中。它将看起来像…

 <asp:TemplateField HeaderText="Category">
      <InsertItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="categoryDataSource"
                        DataTextField="name" DataValueField="categoryid">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="categoryDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
                        SelectCommand="SELECT categoryid, name FROM dbo.TblCategory"></asp:SqlDataSource>
                    <asp:SqlDataSource ID="bookDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
                        SelectCommand="SELECT [categoryid], [booktitle], [bookid] FROM [TblBooks] WHERE ([categoryid] = @categoryid)">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="DropDownList1" Name="categoryid" PropertyName="SelectedValue"
                                Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </InsertItemTemplate>
            </asp:TemplateField>
     <asp:TemplateField HeaderText="Title">
                <InsertItemTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="bookDataSource"
                        DataTextField="booktitle" DataValueField="bookid">
                    </asp:DropDownList>
                </InsertItemTemplate>
            </asp:TemplateField>