Advanced ObjectDataSource Techniques - Part 2

Advanced ObjectDataSource Techniques - Part 2
Previous Page
The Data Access Layer can now be wired to a gridview.

1.  Build the project to generate the DAL objects and create a new form.
 
2.  Create the Country dropdown list by entering this code in the aspx source.
Choose a country: <asp:DropDownList ID="ddlCountry" runat="server"
    DataSourceID="sdsCountry"
    DataTextField="country" DataValueField="country"  AutoPostBack="true"
    onselectedindexchanged="ddlCountry_SelectedIndexChanged" >
</asp:DropDownList>
<asp:SqlDataSource ID="sdsCountry" runat="server"
    ConnectionString="<%$ ConnectionStrings:myConnection %>"
    SelectCommand="select distinct country from customers order by country">
</asp:SqlDataSource>
 
3. 

a. Select the aspx design view.

b. Drag and drop an
    ObjectDataSource onto a web
    form.  Select "Configure Data
    Source." 

c. Select the CustomerOrders
    TableAdapter and select
    "Next".

Note, the DAL must be in the App_Code folder or it will not appear.

d. On "Define Data Methods",
    select
    GetCustOrdersByCountrySort. 

e. Select "none" on the Update, Insert, and Delete tabs.

f. Select "Next"

 
 
4.  a. Define the "Country"
    parameter.  Define it as a
    control and reference the
    ddlCountry dropdown.

b. Don't define sortExpression, 
    startRowIndex, or 
    maximumRows.

c. Select "Finish"
 
5.  Add  
- EnablePaging="True"
- SelectCountMethod=
  "GetCustOrdersByCountryCount"
- SortParameterName
   ="sortExpression" 
to the generated code. 

Remove the sortExpression, startRowIndex, and maximumRows parameters.  They will be managed by the gridview.
< asp:ObjectDataSource ID="odsCustOrderst" runat="server"
EnablePaging="True" SelectCountMethod="GetCustOrdersByCountryCount" SortParameterName ="sortExpression" SelectMethod="GetCustOrdersByCountry" TypeName="CustomerOrderTableAdapters.CustomerOrdersTableAdapter">
<SelectParameters>
   <asp:ControlParameter ControlID="ddlCountry"
    
Name="Country" PropertyName="SelectedValue" Type="String" />
   <asp:Parameter Name="sortExpression" Type="String" />
   <asp:Parameter Name="startRowIndex" Type="Int32" />
   <asp:Parameter Name="maximumRows" Type="Int32" /></SelectParameters>
</asp:ObjectDataSource>
 
6.  Drag and drop a gridview onto the form.
a. Select the newly created
    ObjectDataSource as the Data
    Source.
b. Select Enable Paging and
    Enable Sorting
 
7.  Add PageSize to the gridview.  
 
8.  Add a dropdown listbox event to handle when a new country is selected.
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
    this.gvCustOrders.PageIndex = 0;
    this.odsCustOrders.Select();
    this.gvCustOrders.DataBind();
}
 
9.  You are done!  Run the app and the gridview will display the data based on the selected country..
 
Previous Page
 Contact Us     Contact Us     Links     Links      ©2012 GeekPhilosopher.com - All rights reserved
Powered by www.ezjooz.com