ASP.NET control in one content area needs to reference a control in another content area

Posted by harrije on Stack Overflow See other posts from Stack Overflow or by harrije
Published on 2010-06-03T20:24:43Z Indexed on 2010/06/03 20:44 UTC
Read the original article Hit count: 293

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about masterpage