| Main ASP .NET Tutorial Index |
Scott Mitchell June 2006 Download the ASPNET_Data_Tutorial_5_CS.exe sample code. Contents of Tutorial 5 (Visual C#)
Introduction IntroductionIn the last tutorial we looked at displaying data
with the GridView, DetailsView, and FormView
controls bound to an ObjectDataSource control that
invoked the The ObjectDataSource can be used to invoke methods that expect input parameters, but in order to do so we must specify where the values for these parameters come from. The parameter values can be hard-coded or can come from a variety of dynamic sources, including: querystring values, Session variables, the property value of a Web control on the page, or others. For this tutorial let's start by illustrating how
to use a parameter set to a hard-coded value.
Specifically, we'll look at adding a DetailsView to
the page that displays information about a specific
product, namely Chef Anton's Gumbo Mix, which has a
Using a Hard-Coded Parameter ValueFor the first example, start by adding a
DetailsView control to the
Figure 1. Add an ObjectDataSource to the Page This will automatically start the
ObjectDataSource control's Choose Data Source
wizard. Select the
Figure 2. Select the Since we want to display information about a
particular product we want to use the
Figure 3. Choose the Since the method we selected includes a
parameter, there's one more screen for the wizard,
where we're asked to define the value to be used for
the parameter. The list on the left shows all of the
parameters for the selected method. For
Figure 4. A Hard-Coded Parameter
Value of 5 Will Be Used for the After completing the Configure Data Source
wizard, the ObjectDataSource control's declarative
markup includes a <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetProductByProductID" TypeName="ProductsBLL">
<SelectParameters>
<asp:Parameter DefaultValue="5" Name="productID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
Note If you're following along on your own computer the declarative markup you see at this point may include values for the When visiting this page, the data Web control
will invoke the ObjectDataSource's
Figure 5. Information About Chef Anton's Gumbo Mix are Displayed Setting the Parameter Value to the Property Value of a Web ControlThe ObjectDataSource's parameter values can also
be set based on the value of a Web control on the
page. To illustrate this, let's have a GridView that
lists all of the suppliers that are located in a
country specified by the user. To accomplish this
start by adding a TextBox to the page into which the
user can enter a country name. Set this TextBox
control's
Figure 6. Add a TextBox to the
Page with Next, add a GridView to the page and, from the
smart tag, choose to add a new ObjectDataSource.
Since we want to display supplier information select
the
Figure 7. Choose the Since the
Figure 8. Set the Parameter
Value to the The ObjectDataSource's declarative markup differs
slightly from our first example, using a
ControlParameter instead of the standard <asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
SelectMethod="GetSuppliersByCountry" TypeName="SuppliersBLL">
<SelectParameters>
<asp:ControlParameter ControlID="CountryName" Name="country" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
When visiting the page for the first time the
Once the visitor enters in a country, however,
and clicks the Show Suppliers button to cause a
postback, the ObjectDataSource's
Figure 9. Those Suppliers from Canada Are Shown Showing All Suppliers By DefaultRather than show none of the suppliers when first
viewing the page we may want to show all
suppliers at first, allowing the user to pare down
the list by entering a country name in the TextBox.
When the TextBox is empty, the SELECT SupplierID, CompanyName, Address, City, Country, Phone FROM Suppliers WHERE Country = @Country The expression To return all suppliers when the country
TextBox is empty, we can augment the Change the public Northwind.SuppliersDataTable GetSuppliersByCountry(string country)
{
if (string.IsNullOrEmpty(country))
return GetSuppliers();
else
return Adapter.GetSuppliersByCountry(country);
}
With this change the
Figure 10. All Suppliers are Now Shown by Default SummaryIn order to use methods with input parameters, we
need to specify the values for the parameters in the
ObjectDataSource's The examples we looked at in this tutorial illustrated how to use declarative parameter values. However, there may be times when we need to use a parameter source that's not available, such as the current date and time, or, if our site was using Membership, the User ID of the visitor. For such scenarios we can set the parameter values programmatically prior to the ObjectDataSource invoking its underlying object's method. We'll see how to accomplish this in the next tutorial. Happy Programming!
About the Author
Scott Mitchell, author of six ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies since 1998. Scott works as an independent consultant, trainer, and writer, recently completing his latest book, Sams Teach Yourself ASP.NET 2.0 in 24 Hours. He can be reached at mitchell@4guysfromrolla.com or via his blog, which can be found at http://ScottOnWriting.NET. Special Thanks To...This tutorial series was reviewed by many helpful reviewers. Lead reviewer for this tutorial was Hilton Giesenow. Interested in reviewing my upcoming MSDN articles? If so, drop me a line at mitchell@4GuysFromRolla.com. |
||||










