我嘗試了一種方法來完成此項工作,但我無法做到這一點。我有一個DropDownList綁定到.mdf數據庫列中的數據。我還在它下面有一個GridView,它使用Query Builder綁定到來自同一數據庫中4個不同表的列。根據DropDownList選擇進行ASP.NET GridView更改
我知道要做到這一點,你必須添加一個WHERE子句的查詢字符串,看起來像這樣 - WHERE([column] = @column) - 然後你必須把一個SelectParameters在HTML代碼。但我不知道如何正確書寫。
當我在瀏覽器中運行頁面時,我總是收到錯誤。像「列不明確」,「列不存在」,「必須聲明標量變量@列」等錯誤。我認爲這可能與三個表有一列名爲description和2他們有別名。
這裏的HTML:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="ByCategory.aspx.vb" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label2" runat="server" Text="Select a category:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="description" DataValueField="description"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [description] FROM [category]"></asp:SqlDataSource>
<asp:Button ID="btnGo" runat="server" Text="Go" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource3" ForeColor="#333333" GridLines="None" Visible="False">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="whenCreated" HeaderText="whenCreated" SortExpression="whenCreated" />
<asp:BoundField DataField="description" HeaderText="description" SortExpression="description" />
<asp:BoundField DataField="firstName" HeaderText="firstName" SortExpression="firstName" />
<asp:BoundField DataField="lastName" HeaderText="lastName" SortExpression="lastName" />
<asp:BoundField DataField="Expr1" HeaderText="Expr1" SortExpression="Expr1" />
<asp:BoundField DataField="Expr2" HeaderText="Expr2" SortExpression="Expr2" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT bugEntry.whenCreated, bugEntry.description, employee.firstName, employee.lastName, category.description AS Expr1, status.description AS Expr2 FROM bugEntry INNER JOIN category ON bugEntry.cat_id = category.cat_id INNER JOIN status ON bugEntry.status_id = status.status_id CROSS JOIN employee">
</asp:SqlDataSource>
</asp:Content>
DropDownList1同時具有數據源相同字段的文本和值,而是在select語句中創建一個別名並分配相應的字段名稱s到下拉列表。 –