The DataSourceID of 'gv2' must be the ID of a control of type IDataSource. A control with ID 'objDs1' could not be found.

i have a radiobutton list and gridview and a object data source at runtime i'm changing the object data source by clicking on the radiobutton list selected index changed
and here is my source code....

       

 <asp:RadioButtonList runat="server" ID="rblView" RepeatDirection="Horizontal" RepeatLayout="Flow" AutoPostBack="True" OnSelectedIndexChanged="rblView_SelectedIndexChanged">
                            <asp:ListItem Selected="True" Value="0">My Visits</asp:ListItem>
                            <asp:ListItem Value="1">Group Visits</asp:ListItem>
                        </asp:RadioButtonList>


                        <asp:ObjectDataSource runat="server" EnableCaching="false" ID="ods" TypeName="daab.common"
                        SelectMethod="MyVisits_Get">
                        <SelectParameters>
                            <asp:Parameter Name="UserId" Direction="Input" Type="string" />
                        </SelectParameters>
                        </asp:ObjectDataSource>

 <asp:GridView runat="server" ID="gv1" PagerSettings-Mode="numeric"
                                AutoGenerateColumns="false" CssClass="gridView"  
                                DataSourceID="ods"
                                               
                                Width="100%" AllowPaging="true" AllowSorting="true" PageSize="10"
                                Cellpadding="2"
                                gridlines="Horizontal" 
                                HeaderStyle-CssClass="header"
                                RowStyle-CssClass="item"
                                AlternatingRowStyle-CssClass="altitem" >  
                            <Columns>

 

protected void rblView_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.rblView.SelectedValue == "1")
        { 
        ObjectDataSource objDs = new ObjectDataSource("daab.common", "MyGroupVisits_Get");
        objDs.SelectParameters.Add("UserId", UserId);
        objDs.SelectParameters.Add("GroupVisits", GroupVisits);
        objDs.ID = "objDs1";
        objDs.Select();
        this.Controls.Add(objDs);   
        this.gv2.DataSourceID = "objDs1"; 
        }
    }

when the selectindexchange for GroupVisit which works fine but when i click on MyVisits then i get the below error

The DataSourceID of 'gv2' must be the ID of a control of type IDataSource.  A control with ID 'objDs1' could not be found.


Its all about coding!
0
nisarkhan
6/11/2008 1:39:09 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

1 Replies
1211 Views

Similar Articles

[PageSpeed] 49
Get it on Google Play
Get it on Apple App Store

Hi nisarkhan,

When you use the DataSourceID property, the ID you supply is expected to refer to a control existing on the page.  You have created an ObjectDataSource in the code-behind, but this particular ObjectDataSource does not actually exist on the page.  You could add it to the page by saying form1.Controls.Add(objDs), but I would suggest instead setting the DataSource property directly to the ObjectDataSource you have created and nulling out the DataSourceID in your SelectedIndexChange event: 

protected void rblView_SelectedIndexChanged(object sender, EventArgs e)
{
  if (this.rblView.SelectedValue == "1")
  {
    Object DataSource objDs ...
    ...
    this.gv2.DataSourceID = string.Empty;
    this.gv2.DataSource = objDs;
    this.gv2.DataBind();
  }
}

 Hope this helps!

~ mellamokb

Edit: It seems you do have "this.Controls.Add(objDs);" in your code.  Maybe try using form1.Controls.Add(...) or whatever your form name is per my example above.  I think when you add it to the Page, it doesn't get recognized the same way as when you add it to the form, i.e., you will get errors like "Control must be placed in a form with runat="server" ".

0
mellamokb
6/11/2008 4:07:30 AM
Reply:

Web resources about - The DataSourceID of 'gv2' must be the ID of a control of type IDataSource. A control with ID 'objDs1' could not be found. - asp.net.presentation-controls

Resources last updated: 12/5/2015 8:42:39 AM