views:

112

answers:

1

I have a master page that divides the main content into two areas. There are two asp:ContentPlaceHolder controls in the body section of the master page with IDs cphMain and cphSideBar respectively.

One of the corresponding content pages has a control in cphMain that needs to refer to a control in cphSideBar. Specifically, a SqlDataSource in cphMain references a TextBox in cphSideBar to use as a parameter in the select command. When the content page loads the following run-time error occurs:

Could not find control 'TextBox1' in ControlParameter 'date_con'. 
Description: An unhandled exception occurred during the execution of the 
current web request. Please review the stack trace for more information about 
the error and where it originated in the code. 
Exception Details: System.InvalidOperationException: Could not find control
'TextBox1' in ControlParameter 'date_con'.
Source Error: An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.  
Stack Trace: 
[InvalidOperationException: Could not find control 'TextBox1' in ControlParameter 'date_con'.]
    System.Web.UI.WebControls.ControlParameter.Evaluate(HttpContext context, Control control) +1753150
    System.Web.UI.WebControls.Parameter.UpdateValue(HttpContext context, Control control) +47
    System.Web.UI.WebControls.ParameterCollection.UpdateValues(HttpContext context, Control control) +114
    System.Web.UI.WebControls.SqlDataSource.LoadCompleteEventHandler(Object sender, EventArgs e) +43
    System.EventHandler.Invoke(Object sender, EventArgs e) +0
    System.Web.UI.Page.OnLoadComplete(EventArgs e) +8698566
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +735

I kinda know what the problem is... ASP.NET does not like the fact that the SqlDataSource and TextBox are in different asp:Content controls within the content page.

As a workaround, I have another TextBox in cphMain with the SqlDataSource which has Visible=False. Then in the Page_Load() event handler the contents of the TextBox in cphSideBar is copied into the contents of the non-visible TextBox in cphMain.

I get the results I want with the work around I've come up with, but it seems like such a hack. I was wondering if there is a better solution I'm missing.

Please advise.

A: 

I believe that you need to reference the controls using their containers.

For example, get a reference to a ContentPlaceHolder:

ContentPlaceHolder myContent = (ContentPlaceHolder)this.Master.FindControl("MyContent");

where MyContent is the ContentPlaceHolderID, not the ID of the ContentPlaceHolder:

<asp:Content ContentPlaceHolderID="MyContent" ID="wrongID" runat="server">

Then, you can use FindControl to locate the child control in the MyContent container:

myContent.FindControl("someControlID").Visible = false;
DOK
In my code behind I can reference any control in any area of the content page. The problem seems to be in the markup. For the SqlDataSource, I decided to change the parameter type from control to none, so at least I don't need to use the non-visible TextBox. This solution is good enough for me.
harrije