2014-07-24 44 views
0

我嘗試了一種方法來完成此項工作,但我無法做到這一點。我有一個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> 
&nbsp;<asp:Button ID="btnGo" runat="server" Text="Go" /> 
&nbsp; 
<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> 
+0

DropDownList1同時具有數據源相同字段的文本和值,而是在select語句中創建一個別名並分配相應的字段名稱s到下拉列表。 –

回答

0

首先確保您的下拉框中有AutoPostBackonselectedindexchangedDataValueField屬性設置如下圖所示:

<「ASP:DropDownList的ID = 「DropDownList1」 RUNAT = 「server」DataSourceID =「SqlDataSource2」DataTextField =「description」AutoPostBack =「True」 DataValueField =「ItemNo」 DataTextField = 「ITEMNAME」 onselectedindexchanged = 「DropDownList1_SelectedIndexChanged」>

然後設置其使用從DropDownList作爲其Select Parameter的值的ObjectDataSource。將您的GridView綁定到ObjectDataSource

最後在DropDownList1_SelectedIndexChanged方法執行以下操作:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    ObjectDataSource1.DataBind(); 
    GridView1.DataBind(); 
} 

它僅採用了ObjectDataSource1或需要被稱爲GridView1,斜面召回它的時刻。

0

我也有這種情況,這是我如何解決

我束縛我的GridView的數據源,並使用SqlDataSource嚮導 的幫助下建立查詢時,配置數據源使用第一個選項「指定自定義SQL語句或存儲過程「,因爲你正在使用多個表 現在點擊下一步,在窗口上方第一個窗格使用查詢生成器 右鍵單擊並添加表 選擇從表中的列按您的要求

現在在查詢窗格寫在最後的新行

WHERE([列] = @列)

現在OK,然後單擊下一步

新的窗口將出現 選擇參數源 和控制ID點擊下一步,你是好去

希望這會有所幫助

相關問題