Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

If you have used SqlDataSource control with the GridView, FormView or the DetailsView and updating the data in the database using the UpdateCommand and UpdateParameters of the SqlDataSource control then you might be familiar with this error. The error "Could not find control 'xxx' in ControlParameter 'xxx'" is thrown when the ASP.NET is not able to find the requested control defined in the Update ControlParameters.

Check out the code below which will throw the above exception:

<asp:DetailsView DataKeyNames="UserID" AllowPaging="true" ID="dvUsers" runat="server" DataSourceID="SqlDataSource1" AutoGenerateRows="false">

<Fields>

<asp:BoundField HeaderText="UserID" DataField="UserID" />

<asp:TemplateField HeaderText="User Name">

<EditItemTemplate>

<asp:TextBox ID="txtUserName" runat="server" Text = '<%# GetUserName() %>' />

</EditItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="No of Pets">

<ItemTemplate>

<asp:Label ID="lblNoOfPets" runat="server" Text = '<%# Eval("NoOfPets") %>' />

</ItemTemplate>

</asp:TemplateField>

<asp:CommandField ShowEditButton="true" />

</Fields>

</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

SelectCommand="SELECT * FROM Users" ConnectionString='<%$ ConnectionStrings:ConnectionString %>'

UpdateCommand="UPDATE Users SET UserName = @UserName, DateCreated = GETDATE() WHERE UserID = @UserID">

<UpdateParameters>

<asp:ControlParameter ControlID = "txtUserName" Name="UserName" Type="String" PropertyName="Text" />

<asp:Parameter Name="UserID" Type="Int32" />

</UpdateParameters>

</asp:SqlDataSource>

And now, here is the code which does not throw the error. All you need to do is to refer to your control with its full name.

<UpdateParameters>

<asp:ControlParameter ControlID = "gvUsers$txtUserName" Name="UserName" Type="String" PropertyName="Text" />

<asp:Parameter Name="UserID" Type="Int32" />

</UpdateParameters>

You can easily find the control's full name when you open the page in the browser and you can see the control simply view source and you will see it.

Hope this tip helps!

 

Posted on Sunday, August 27, 2006 4:22 PM | Back to top


Comments on this post: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."

# re: Solving the error &quot;Could not find control 'xxx' in ControlParameter 'xxx'.&quot;
Requesting Gravatar...
Thank you for fixing this problem for me. I assumed that you would have to explicity point to the control, but I wouldn't have known how without your tutorial.

I just need to put the validation in now.

Thanks again!
Left by =['] on Sep 14, 2006 2:12 AM

# re: Solving the error &quot;Could not find control 'xxx' in ControlParameter 'xxx'.&quot;
Requesting Gravatar...
me again,

Further to this tutorial's help - do you have any pointers as to how I can validate the fields in a detailsview update?

I can validate the dropdown list i have added to the edititemtemplate, but by default the detailsview doesn't provide any validation.
Left by =['] on Sep 14, 2006 2:18 AM

# re: Solving the error &quot;Could not find control 'xxx' in ControlParameter 'xxx'.&quot;
Requesting Gravatar...
Hi,
I've also experiencd this problem, but the GridViewID$TextBoxID didn't work.

However it does work when I specify GridViewID$ctl04$TextBoxID for example, i.e., using the full ID created.

Obviously this breaks as soon as I edit any row other then ct104!
This is nuts!

Any help would be appreciated.
Thanks
Left by fawkesey on Oct 01, 2006 8:05 AM

# re: Solving the error &quot;Could not find control 'xxx' in ControlParameter 'xxx'.&quot;
Requesting Gravatar...
GridViewID$TextBoxID didn't work in my situation either.

I am using a master page. In my case I had to use, ctl00$ContentPlaceHolder1$GridViewID$TextBoxID

Left by Rich Alger on Oct 05, 2006 2:02 PM

# re: Solving the error &quot;Could not find control 'xxx' in ControlParameter 'xxx'.&quot;
Requesting Gravatar...
Not exactly sure yet how to solve this but when I look on my control I see ClientID is "gvUsers$ctl11$ddlLevel" but the ID="ddlLevel" still not sure why I can't just type ddlLevel. I need to fix this!!
Left by Dan on Oct 20, 2006 12:40 PM

# re: Solving the error &quot;Could not find control 'xxx' in ControlParameter 'xxx'.&quot;
Requesting Gravatar...
Yes, Microsoft's love affair with uniquely naming everything like sometimes really p***es me off. Hopefully the next beta release will automatically retrieve the unique ID generated so that we only have to specify the name of the control as it appears at design time.
Left by David Keaveny on Nov 05, 2006 3:36 PM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Great.
BTW: How you found this solution?
Left by Lavitar on May 09, 2007 3:32 PM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Great, it helped me out. Thanks.
Left by odzi on Aug 26, 2007 9:26 AM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
To seamlessly solve this problem, in the MasterPage override the FindControl to do a recursive search. This works great, and then allows the Control Parameters to be configured without any manual machinations....
Left by Chad Price on Sep 03, 2007 2:29 PM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Here's the solution to recursively finding the solution.

public static System.Web.UI.Control FindControl(string controlID, System.Web.UI.Control control)
{
if (control.ID == controlID)
return control;
else
{
foreach (System.Web.UI.Control ctrl in control.Controls)
{
System.Web.UI.Control searchedControl = FindControl(controlID, ctrl);
if (searchedControl != null)
return searchedControl;
}
return null;
}
}
Left by Tahir Khan on Nov 15, 2007 2:37 PM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Another way I've found to get around this issue with data source objects looking for controls inside the context of a DetailsView or GridView control is to actually place the data source control inside the item/edit item template that has the controls you wish to reference. This might not be ideal for all situations, but it certainly works.
Left by Jesse Flint on Nov 28, 2007 12:20 PM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Thanks for the information, this helped me out a lot!
Left by Sam on Dec 04, 2007 2:32 PM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Dude, this totally helped me out!

Thanks!

JL
Left by JL on Dec 17, 2007 1:14 PM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Great Website and I got the answer from your website Thank You!!
Left by Kalaiarasu on Jan 24, 2008 8:25 PM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
The op's suggestion did not work. After looking at Jacob's suggestion above, I was able to get things working by removing the control parameters from the <UpdateParameters> section. Those control parameters had the "Bind" verbiage in them.

Everything started working after removing them. I have to say I find this to be very unintuitive as it took me a couple of hours of searching for the solution for this.

Thanks Jacob
Left by Lee on Feb 14, 2008 8:56 AM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Thank you for writing this up, and thanks to Google for finding this. I spent the last hour trying to figure this out and i was determined to do it declaratively.
Left by Pasha on Feb 15, 2008 5:53 AM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Seems like it can't find the control if it is in a different <div> section....crazy
Left by Chris on Apr 01, 2008 7:53 AM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
@Chad:
"To seamlessly solve this problem, in the MasterPage override the FindControl to do a recursive search. This works great, and then allows the Control Parameters to be configured without any manual machinations...."

Genius. This solves every problem I've ever had with finding controls! You deserve a medal.
Left by Matt on Apr 18, 2008 4:57 AM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Struggled for days with FindControl and programming the Control Parameters in the VB Code as a work around. Your an absolute legend for posting this. Thank you.
Left by Mark on Jun 06, 2008 3:52 AM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Struggled for days with FindControl and programming the Control Parameters in the VB Code as a work around. Your an absolute legend for posting this. Thank you!
Left by Mark S on Jun 06, 2008 3:58 AM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
This was hugely helpful! Thank you!!
Left by PaulM on Aug 30, 2010 5:53 PM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Right click, view source, get the name of the control and take away the beginning prefix.

Example:

<select name="ctl00$Content$dvCharts$ddlLayout" id="ctl00_Content_dvCharts_ddlLayout">

I used Content$dvCharts$ddlLayout
Left by Kyle on Oct 25, 2010 6:50 AM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
This is awesome, for some time I have been using code behind because I was not able to accomplish this, THANK YOU SO MUCH!!
Left by Jair Orozco on Oct 29, 2010 10:22 AM

# re: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."
Requesting Gravatar...
Thank you to Jesse Flint!
Left by Lavender Nguyen on Nov 12, 2010 11:24 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net