The DataSourceID of '' must be the ID of a control of type IDataSource

I have a grid view with dropdown list. I have to be able to edit it by clicking on ‘Edit’ link button. Dropdown at this point should be populated with data.

1. I need to display one of the values of the dropdown in the grid (read only). This value is what is coming to populate entire row of the grid view. Even I do not have an error on initial load of grid view and while debugging I see a data coming to populate grid view row and a dropdown, the column is empty. How can I populate a value of the grid view column of the row as a selected field of the dropdown?

2. I am getting an error when link button ‘Edit’ clicked:

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

Here is my grid view:

  <asp:gridview runat="server" id="grdGoalsView" width="100%" onpageindexchanged="PageIndexChanging" pagesize="15" selectedindex="0"allowpaging="True"allowsorting="True"datakeynames="GroupID"AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"AutoGenerateSelectButton="True"  emptydatatext="No data available." autogeneratecolumns="False"                               OnSelectedIndexChanged="grdGoalsView_SelectedIndexChanged" OnRowCancelingEdit="grdGoalsView_RowCancelingEdit" OnRowDeleted="grdGoalsView_RowDeleted" OnRowDeleting="grdGoalsView_RowDeleting" OnRowEditing="grdGoalsView_RowEditing" OnRowUpdated="grdGoalsView_RowUpdated" OnRowUpdating="grdGoalsView_RowUpdating" OnSorted="grdGoalsView_Sorted" OnSorting="grdGoalsView_Sorting" OnRowDataBound="grdGoalsView_RowDataBound" OnDataBinding="grdGoalsView_DataBinding"> <columns>     <asp:boundfield  HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="GroupID" Visible = False headertext="Group ID" SortExpression="GroupID"  /><asp:TemplateField HeaderText="Group Name" HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center>     <EditItemTemplate>asp:DropDownList runat=server DataSourceID="ddlGroupNames" />  </EditItemTemplate></asp:TemplateField>                                    <asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="month01" headertext="01"/><asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="month02" headertext="02"/><asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="month03" headertext="03"/><asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="month04" headertext="04"/></columns></asp:gridview>   Here is the code behind protected void Page_Load(object sender, EventArgs e)    {         if (!(Page.IsPostBack))        {            loadControls();        }}  protected void loadControls()    {        List<CashFlowGoal> cashFlowGoal = GetGoalData();        grdGoalsView.DataSource = (object)cashFlowGoal;        grdGoalsView.DataBind();} protected void grdGoalsView_RowEditing(object sender, GridViewEditEventArgs e)    {        // Set new EditIndex        grdGoalsView.EditIndex = e.NewEditIndex;        // Re-bind data        List<CashFlowGoal> CFGoal = GetGoalData();        grdGoalsView.DataSource = (object)CFGoal;        grdGoalsView.DataBind(); //I am getting the error here    } protected void grdGoalsView_DataBinding(object sender, EventArgs e)    {        DropDownList ddl = (DropDownList)grdGoalsView.FindControl("ddlGroupNames");        ddl = new DropDownList();        List<CashFlowProgramName> cashFlowGroupNames = GetGroupNames();        ddl.DataSource = (object)cashFlowGroupNames;        ddl.DataTextField = "CFProgramName";        ddl.DataValueField = "CFProgramID";        ddl.DataBind();    } Can you please tell me what am I missing and what am I doing wrong?

 

0
nata100
4/20/2009 7:14:44 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

10 Replies
5003 Views

Similar Articles

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

I see three things which would be causing your error:

You are missing the opening < in this line of code

<EditItemTemplate>asp:DropDownList runat=server DataSourceID="ddlGroupNames" />

Your dropdownlist above does not have an ID so you cannot access it. 

If you want to set a datasource for the dropdownlist use the DataSource property vs the DataSourceId property.


View Brenden Kehren's profile on LinkedIn
Remember to mark as answer if this post answered your question.
0
b471code3
4/20/2009 8:01:01 PM

nata100:
<EditItemTemplate>asp:DropDownList runat=server DataSourceID="ddlGroupNames" />  </EditItemTemplate>
 

It should the

ID="ddlGroupNames".

By setting DataSourceID, you are actually setting the data source to the control with the specified ID. So, use ID here.

It should be....

<asp:DropDownList runat=server ID="ddlGroupNames" />

Also in the DataBinding event, do not use

ddl = new DropDownList();

This is reset your reference. So, remove that line.

Hope it helps.

 


KAMMIE
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
0
kammie
4/20/2009 8:01:21 PM

Thank you for the quick replay. 

I am not sure why opening tag is missing in my posted question - it is present in the actual code. Using the ID instead of data source Id helped the error. Thank you. I still need help for Item #1. How do I populate data in dropdown and make the selected item in dropdown to be is what coming back for this item in the row.
0
nata100
4/20/2009 8:21:19 PM

Without ddl = new DropDownlist(), ddl is null and I can't use it to assign datasource.

0
nata100
4/20/2009 8:23:40 PM

In order for you to access the DDL nested within your gridivew you can do one of two things:

In your TemplateField add a datasource like this:

 

<asp:TemplateField HeaderText="Group" SortExpression="GroupID">
    <ItemStyle HorizontalAlign="center" />
    <ItemTemplate>
        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Name") %>' />                                       
    </ItemTemplate>
    <EditItemTemplate>
        <asp:SqlDataSource ID="Sqldatasource1" runat="server" SelectCommand="SELECT ID, Name FROM MyTable ORDER BY Name></asp:sqldatasource>
        <asp:Dropdownlist ID="DropDownList1" runat="server" DataSourceId="Sqldatasource1" DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("OtherId") %>' />
    </EditItemTemplate>
</asp:TemplateField>

 Since your ddl is nested you will have access to your other bound items from the gridviews datasource. 

Or you can access it in the code behind like this:

Dim ddl as DropDownList = CType(GridView1.FindControl("DropDownList1), DropDownList)
ddl.DataSource = your datasource


View Brenden Kehren's profile on LinkedIn
Remember to mark as answer if this post answered your question.
0
b471code3
4/20/2009 8:29:26 PM

Use RowDataBound event of the GridView and inside that....write like this...

 if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit) || e.Row.RowState == DataControlRowState.Edit))

{

    DropDownList ddl = (DropDownList)e.Row.FindControl("ddlGroupNames");

   ddl.DataSource = --datasource--;

   ddl.DataBind();

}

Hope it helps. 


KAMMIE
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
0
kammie
4/20/2009 8:34:46 PM

I added <ItemTemplate> in addition to <EditItemTemplate> and initial load is showing data. Thanks a lot.

When I click on 'Edit' my dropdown is still empty.

here is the code. There should be something else on RowEditing

<asp:TemplateField HeaderText="Group Name" HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center>

<ItemTemplate>

<asp:Label ID="lblGroupName" runat="server" Text='<%# Bind("GroupName") %>' />

</ItemTemplate>

<EditItemTemplate>

<asp:DropDownList runat=server ID="ddlGroupNames" DataTextField="GroupName" />

protected void grdGoalsView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        // Set new EditIndex
        grdGoalsView.EditIndex = e.NewEditIndex;

        // Re-bind data
        List<CashFlowGoal> CFGoal = GetGoalData();
        grdGoalsView.DataSource = (object)CFGoal;
        grdGoalsView.DataBind();


    } 

 

</EditItemTemplate>

</asp:TemplateField>

0
nata100
4/20/2009 9:29:50 PM

 Your RowEditing event is fine. Do the thing I told you in my previous post. If you have already created your RowDataBound event, then copy the code in that event here so we can take a look.


KAMMIE
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
0
kammie
4/20/2009 9:37:02 PM

here is the code

<asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="GroupID" Visible = False headertext="Group ID" SortExpression="GroupID" />

<asp:TemplateField HeaderText="Group Name" HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center>

<ItemTemplate>

<asp:Label ID="lblGroupName" runat="server" Text='<%# Bind("GroupName") %>' />

</ItemTemplate>

<EditItemTemplate>

<asp:DropDownList runat=server ID="ddlGroupNames" DataTextField="CFProgramName" />

</EditItemTemplate>

</asp:TemplateField>

protected void grdGoalsView_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)

|| e.Row.RowState == DataControlRowState.Edit))

{

DropDownList ddl = (DropDownList)e.Row.FindControl("ddlGroupNames");

List<CashFlowProgramName> cashFlowGroupNames = GetGroupNames();

ddl.DataSource = (object)cashFlowGroupNames;

ddl.DataTextField = "CFProgramName";

ddl.DataValueField = "CFProgramID";

ddl.DataBind();

}

}

0
nata100
4/21/2009 2:59:04 PM

I think I found the problem – it was bug outside of the scope of this question. Thank you very much to all who helped. You are amazing.

0
nata100
4/21/2009 3:50:51 PM
Reply:

Similar Artilces:

The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'AccessDataSource' could not be found.
what i'm missing that i get the error: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource.  A control with ID 'AccessDataSource' could not be found.      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="NewsId"                  EmptyDataText="There are no data records to display.">          &nb...

The DataSourceID of 'DetailsView1' must be the ID of a control of type IDataSource. A control with ID 'AccessDataSource1' could not be found.
 Anyone help with this? i have tried lots of ways to query this database and create the insert command, including making my own data sets and setting it up that way with my own custom SQL statement.....this did not work, so i though i would use VWD 2008's own tools to create the insert, i have been left with the following error: The DataSourceID of 'DetailsView1' must be the ID of a control of type IDataSource.  A control with ID 'AccessDataSource1' could not be found.   Everything looks ok to me. Code below:   <%@ Page Title="" Lan...

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">                &nb...

How do fix the error The DataSourceID of 'DataGrid1' must be the ID of a control of type IDataSource. A control with ID 'SqlMedTable' could not be found.
How do you fix the error: The DataSourceID of 'DataGrid1' must be the ID of a control of type IDataSource.  A control with ID 'SqlMedTable' could not be found.Here is the code.protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) {MedicalClassLibrary.Connection c = new MedicalClassLibrary.Connection(); c.GetMedicalTable();DataGrid1.AutoGenerateColumns = true; DataGrid1.DataSource = c.DS;InputDiv.Visible = false;Label1.Visible = false; txtRx.Focus(); } } Here is the class medhod:public void GetMedicalTable() {string medTable = "SELECT ...

The DataSourceID of 'ddlDevice' must be the ID of a control of type IDataSource. 'dsMain' is not an IDataSource.
I seem to be having trouble binding the objects in my page to the datasource. The subject line contains the error message that I keep receiving.  I am trying to bind a dropdownlist with my datasource.  I noticed that if I click on the droplist, go to the properties window and try to select a datasource; nothing is listed. Post the code for filling dsMainNC... How did you setup the datasrouce? I think you didnt use the wizard to connect into database. You will not see any datasrouce in the properties window till u use the wizard to (server explorer) create the databa...

DataSourceID of 'controlname' must be the ID of a control of type IDataSource.
When I use the following code, I get this error:The DataSourceID of 'PSAnswersGridView' must be the ID of a control of type IDataSource.  A control with ID 'Key_Trait_Analysis_ObjectDataSource' could not be found.I am trying to define an objectdatasource in code-behind and bind it to a gridview:Partial Class Admin_StuffInherits System.Web.UI.PageProtected Key_Trait_Analysis_ObjectDataSource As ObjectDataSourceProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadIf Not Page.IsPostBack Then...  Key_Trait_Analysis_ObjectDataSource...

Trying to load user control Dynamcially but getting error " Control 'ctl02_GridView1' of type 'GridView' must be placed inside a form tag with runat=server"
I am tired of this error and now writing a post.  My Web application has two pages. Page one has drop down list which redirect the page to another page . where I am going to load the usercontrol dynamically according to the choice of the user.     protected void ddlViewReport_SelectedIndexChanged(object sender, EventArgs e)  { if (ddlViewReport.SelectedIndex == 1) { Session["ReportType"] = ddlViewReport.SelectedIndex; Response.Redirect("RepReportingPage.aspx"); int crosspagevalue = ddlViewReport.SelectedIndex; } if (ddlViewReport.SelectedIndex == 2) {...

Extender controls of type 'AjaxControlToolkit.DragPanelExtender' cannot extend controls of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.
Hi, I'm getting this error when trying to port an old Atlas Web App to use the latest bits - any help would be nice as to what controls I CAN extend - so if I can't use a DIV what then?: Server Error in '/GRintranets' Application. Extender control 'DragPanelExtender1' cannot extend 'divPaginaHTML'. Extender controls of type 'AjaxControlToolkit.DragPanelExtender' cannot extend controls of type 'System.Web.UI.HtmlControls.HtmlGenericControl'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more informati...

Control 'WzdResults_GridView2' of type 'GridView' must be placed
inside a form tag with runat=server. " That is the error message I get when I try to export my gridview data in excel. Ok, here are the requirements: I have wizard control which has 4 steps, each presenting data in gridview after running some queries on SQL server based on dates given by user. I get data in the gridview fine. Now, I want to generate a report, either using reportviewer or placing that data in excel spreadsheet. I tried with reportviewer. In windows form it works fine, but I could not get it running in web application. Below is the code for reportviwer: ReportViewer1.Pro...

3.1 Parser Error: Could not load type or Multiple controls with the same ID 'ctr' were found. FindControl requires that controls have unique IDs.
I have about ten cusom modules on my local machine and everything worked for about two weeks. Today I created new skin object and uploaded it and now none of my modules work. I get either error Parser Error: Could not load type ..." or Multiple controls with the same ID 'ctr' were found. FindControl requires that controls have unique IDs depending on which page I look at. None of these pages used skin with a new skin object. Btw, I stripped skin object down to noting, no controls, no code and no changes to custom modules have been made. I wonder if  skin object update screwed ...

DefaultButton of 'Panel1' must be the ID of a control of type IButtonControl
I have a Login Control inside a Panel that is inside a LoginView Control. LoginView Control (LoginView2)       |_  Panel (Panel1 )                 |_  Login Control (Login2)                                  |_    imageButton (LoginButton) I want to set the Panel.DefaultButton to the In...

The DefaultButton of 'Panel1' must be the ID of a control of type IButtonControl.
Hi, I am getting the following error when I set the default button of the panel in codebehind. The DefaultButton of 'Panel1' must be the ID of a control of type IButtonControl. This is what I am doing.  In aspx <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  <asp:Panel ID="Panel1" runat="server"> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/> ................... ................... ...

Control 'Datagrid1__ctl1__ctl1' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server.
Hi all..    I am doing paging in datagrid control. This is my code. <asp:datagrid id="dgRest" CssClass="text" CellSpacing="2" CellPadding="2" BorderWidth="4px" AllowPaging ="True" PagerStyle-Mode="NumericPages" PageSize="5" AllowCustomPaging="False" runat="server" BorderStyle="Solid" OnPageIndexChanged ="dgRest_pageIndexChange" BorderColor="#B5DEF7" Width="100%" AutoGenerateColumns="False">   I don't want allowCutomPaging. So, I am putting as AllowCustomPaging="False". Whenever I am adding this attribute, I am facing the following eror.Control 'Datagr...

Control 'TextBox1' of type 'TextBox' must be placed inside a form tag with runat=server
I keep getting this error message when I execute a test example that I am following along with in the webmatrix tour. I don't see a way around it since I am going verbatim with the example. Any ideas? Do exactly what it says.<form runat="server">   <asp:TextBox runat="server" id="TextBox1"/></form>Make sure the form has the runat="server" tag.HTH, rchern.Churned Ajax ToolkitPlease choose "Mark as Answer" for the posts that lead you to a solution. There are several instances of "runat" so I am still a bit perplexed. Here is the code, does this he...

Web resources about - The DataSourceID of '' must be the ID of a control of type IDataSource - asp.net.presentation-controls

<asp:DetailsView ID="grd_empDetails" runat="server" DataSourceID="empDataSource - Pastebin.com
Pastebin launched a little side project called HostCabi.net , check it out ;-) Don't like ads?

Sending Real-Time Sensor Data to Clients Using SignalR
... they're interested in: (Note: I call them Datasources ) public const string GroupLabelPrefix = "Datasource_"; public void Register(int datasourceId) ...

Bugtraq: Multiple vulnerabilities in McAfee ePO 4.6.6
Classification: NON SENSITIVE INFORMATION RELEASABLE TO THE PUBLIC Multiple vulnerabilities in McAfee ePO 4.6.6 Affected Product:McAfee ePO 4.6.6 ...

Resources last updated: 2/3/2016 3:49:35 PM