问题
I've tried a thousand ways to make this work and I can't. I have a DropDownList bound to the data in a column from an .mdf database. I also have a GridView below it that is bound using the Query Builder to columns from 4 different tables in that same database.
I know to do this you have to add a WHERE clause to the query string that looks something like this -- WHERE ([column] = @column) -- And then you have to put a SelectParameters in the HTML code. But I don't know how to write it correctly.
I keep getting errors when I run the page in the browser. Errors like "Column Ambiguous", "Column doesn't exist", "Must declare scalar variable @column", etc. I think it may have something to do with the fact that three of the tables have a column named description and 2 of them have aliases.
Here's the 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>
回答1:
First ensure your drop down box has AutoPostBack
, onselectedindexchanged
and DataValueField
properties set like below:
<'asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="description" AutoPostBack="True" DataValueField="ItemNo" DataTextField="ItemName" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
Then set an ObjectDataSource
which uses the value from the DropDownList
as its Select Parameter
. Bind your GridView
to the ObjectDataSource
.
Finally in your DropDownList1_SelectedIndexChanged
method do the following:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ObjectDataSource1.DataBind();
GridView1.DataBind();
}
Its only either the ObjectDataSource1 or the GridView1 that needs to be called, cant recall which at the moment.
回答2:
i also have this scenario this is how i solved
i bound my Gridview with data source and build query with the help of the SqlDataSource Wizard when you configure data source use first option "specify custom SQl statement or stored procedure " because you are using multiple table now click next and use query builder on Top first pane of window right click and add your table choose column from your table as per your requirement
now in query pane write this in new line at the end
WHERE ([column]=@column)
now ok and click next
new window will appear select parameter source and control id click next and you are good to go
hope this will help
来源:https://stackoverflow.com/questions/24945265/make-an-asp-net-gridview-change-based-on-a-dropdownlist-selection