views:

39

answers:

2

In asp.net sometimes a webcontrol needs to reference another webcontrol such as the gridview needs the id of the datasource object that it will bind to.

I have a property of my webcontrol that is a string (the id of the webcontrol I want to reference). How do I access the actual webcontrol based on this id?

+1  A: 
this.FindControl()

http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol(v=VS.80).aspx

cRichter
The FindControl only looks in any given container. But if this the only tool available for this situation, then the solution is to build a recursive method that traverses the page structure and return the control that matches the id provided.
SynBiotik
hmm... but isn't Page.FindControl() searching on the whole page?
cRichter
It's not supposed to:"The FindControl method can be used to access a control whose ID is not available at design time. The method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page. To access controls in a subordinate naming container, call the FindControl method of that container." - http://msdn.microsoft.com/en-us/library/31hxzsdw.aspx
SynBiotik
A: 

Here's a sample of a GridView bound to an ObjectDataSource, with the ObjectDataSource binding to a DropDownList for a parameter. This should get you started.

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    DataSourceID="CustomerObjectDataSource" 
    DataKeyNames="CustomerID"
    AllowPaging="True" 
    AllowSorting="True" AutoGenerateDeleteButton="True" 
    AutoGenerateEditButton="True" AutoGenerateSelectButton="True" 
    onrowdeleted="GridView1_RowDeleted" onrowupdated="GridView1_RowUpdated">
    <Columns>
        ...
    </Columns>
</asp:GridView>

<asp:ObjectDataSource ID="CustomerObjectDataSource" runat="server" 
    EnablePaging="True" 
    MaximumRowsParameterName="totalRows" 
    StartRowIndexParameterName="firstRow" 
    TypeName="Northwind.Business.CustomerSource" 
    DataObjectTypeName="Northwind.Business.CustomerDTO"
    SelectMethod="Load" 
    UpdateMethod="Save" 
    InsertMethod="Insert" 
    DeleteMethod="Delete"
    SelectCountMethod="CustomerCount" 
    SortParameterName="sortExpression">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddlRegion" Name="region" 
            PropertyName="SelectedValue" />
    </SelectParameters>
</asp:ObjectDataSource>    
Cylon Cat
The question that remains is: Does the gridview locate the ObjectDataSource by traversing the page recursively using the FindControl method?
SynBiotik
@SynBiotik, no. The DataSourceID field of the GridView markup identifies the ObjectDataSource by the data source's ID. The binding is explicit (in the example code above, "CustomerObjectDataSource").
Cylon Cat
IHere is the DataSourceId code:public virtual string DataSourceID{ get { object obj2 = this.ViewState["DataSourceID"]; if (obj2 != null) { return (string) obj2; } return string.Empty; } set { if (string.IsNullOrEmpty(value) } this.ViewState["DataSourceID"] = value; this.OnDataPropertyChanged(); }}How does the gridview tie the string to the actual object ?
SynBiotik