Accessing a User Control's Properties through Object/SQL DataSource Parameters

I have a simple user control that displays the results of a DB table in a GridView.


On the form of the User Control I have GridView1,
in the background .cs I have a public Property UserID

At the moment it works using direct databind in the background .cs file, but If I was add some kind of DataSource Control (Object or SQL) and Bind that to the Gridview, is there a way to connect the Public Property using some kind parameter tags?  I could just copy the UserID to a Label (During FormLoad) and then using the ControlParameter tags and reference the label.

0
wbochar
5/18/2007 3:40:18 PM
asp.net.custom-server-controls 3498 articles. 0 followers. Follow

7 Replies
1060 Views

Similar Articles

[PageSpeed] 58

On the Learn tab on the asp.net site you will find many excellent data tutorials http://www.asp.net/learn/dataaccess/default.aspx?tabid=63 including one answering your question better than I can do :)

This should be a good one http://www.asp.net/learn/dataaccess/tutorial05vb.aspx?tabid=63


WebControls I have written
0
geirk
5/21/2007 10:18:58 PM

I'm aware of how to do it that way; I've read that page in the past. What I am getting at is slightly different.

If you want to poll the control that the gridview/datasource combonation is currently inside?

This is just a visual representation

<UserControl:X runat="server" MyParameter="1">

 <Label id="label1" runat="server" text="2" />
<GridView id="GridView1" runat="server" datasource="sqldatasource1" />
<sqldatasource control id="sqldatasource1" />

</UserControl:X>

 If I was to open the sqldatasource inside the User Control and try to add a parameter to it, if i chose a control.. it would only allow me to use a control ont he usercontrols form like Label1 or Gridview etc. But I want to access the parent container's MyParameter value as part of the datasource parameters.

0
wbochar
5/22/2007 2:42:31 AM

In that case just define MyParameter as a public property on the UserControl and set the data source parameter in server side code.


WebControls I have written
0
geirk
5/22/2007 6:59:13 AM

Not to sound ungrateful, but you should really read the post before answering. Your suggestion is what is currently been used to make it work (as I originally stated in the first post), I was asking if there was a way to reference the public property of the containing control without having to go to the code behind. 

0
wbochar
5/22/2007 11:52:35 AM

I was thinking that using "this.property" as a reference would work; but it doesn't.

0
wbochar
5/22/2007 12:40:37 PM

It can be done using a HiddenField and a server side DataBind() call.

"MyUserControlParameter" is the name of the User control public field or property:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
 SelectCommand="SELECT [ProductID], [ProductName], [SupplierID] FROM [Products] WHERE ([SupplierID] = @SupplierID)">
 <SelectParameters>
  <asp:ControlParameter ControlID="HiddenField1"  PropertyName="Value" DefaultValue="1" Name="SupplierID" Type="Int32" />  
 </SelectParameters>
</asp:SqlDataSource>
<asp:HiddenField ID="HiddenField1" runat="server" Value="<%#this.MyUserControlParameter %>" />

 


WebControls I have written
0
geirk
5/22/2007 5:13:51 PM

Thanks.

0
wbochar
5/22/2007 11:58:59 PM
Reply: