|
Subject:
|
Passing a DataSource to a user control
|
|
Posted By:
|
hpox
|
Post Date:
|
12/22/2006 4:19:21 PM
|
Is it possible to have a generic user control (productListing.ascx) that contains only a GridView and no ObjectDataSource linked to it?
What we want is to dynamically set the ObjectDataSource instead of statically declaring it in the .ascx file.
For example, in the Search.aspx page:
Search Results:
<uc1:ProductListing ID="pl" runat="server" />
We want to, somehow, pass the DataSource (List<Product>) to the productListing at this level.
So we can have another page, Highlights.aspx:
<h2>Our 10 best sellers:</h2>
<uc1:ProductListing ID="pl" runat="server" />
<h2>Check out the latest articles:</h2>
<uc1:ArticleListing ID="al" runat="server" />
The ObjectDataSource of both cases do not come from the same place.
|
|
Reply By:
|
hpox
|
Reply Date:
|
1/8/2007 10:04:43 AM
|
(I know I'm replying to my own question)
Yes, it is possible. In the code-behind file, do something like this:
GridView gridView = ((GridView)produitListing.FindControl("gridView"));
gridView.DataSource = myDataSource;
gridView.DataBind();
|