Using a DropDownList to trigger a load in a GridView is such a great feature its a shame I had to go to a beta version book to find it.
Goal: 1 DropDownList of States filtering 1 GridView of Customers
1.) Add a DataSource control to handle data ( I chose SQL Server ):
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CustomersConnectionString %>"
SelectCommand="SELECT DISTINC State FROM [Customers] ORDERBY State"
EnableCaching="True" CacheDuration="300" />
Note : The connection string is from my web.config
Note : Cache because the data doesnt usually change [Just a group of States]
2.) Add a DropDownList control let's say full of States:
<asp:dropdownlist id="statesDDL" DataSourceID="SqlDataSource1"
DataTextField="State" AutoPostBack="True" runat="server" />
Note: AutoPostBack="True" to trigger the post back on change
3.) Add a Second DataSource Control with cool attributes:
<asp:sqldatasource id="SqlDataSource2" connectionstring="<%$"ConnectionStrings:CustomersConnectionString %
>"
SelectCommand="SELECT CustName,CustAddress,CustCity,CustPhone FROM [Customers]
WHERE State=@State">
<SelectParameters>
<asp:ControlParameter Name="State" ControlID="statesDDL" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
4.) Add a GridView which will have data filtered by State:
<asp:GridView id="grid1" DataSourceID="SqlDataSource2" runat="server" />
( Dont ya just love the gridView code. )
1 Comments:
Great, straightforward!
Thanks.
Friday, September 07, 2007 7:45:00 PM
Post a Comment
<< Home