簡體   English   中英

將下拉列表綁定到下拉列表到詳細信息視圖 ASP.NET C#

[英]Binding dropdownlist to dropdownlist to detailsview ASP.NET C#

我正在嘗試將 Dropdownlist 綁定到頁面中的 Detailsview 控件,其中第二個 Dropdownlist 的結果取決於第一個。 我可以將單個 Dropdownlist 綁定到 Detailsview,但如果我添加另一個 Dropdownlist,其結果取決於第一個,除非刷新頁面,否則詳細信息視圖不會顯示任何內容。

那么,如果選擇了第二個下拉列表,有什么方法可以自動發布詳細信息視圖的結果? 我目前正在研究這個,但我無法得到它。

這是我的示例代碼

<%@ 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>

任何幫助將非常感激;)

提前致謝

您必須將您的 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM