2010-06-03 68 views
0

我有一个主页面,它将主内容分为两个区域。在母版页的正文部分有两个asp:ContentPlaceHolder控件,分别使用ID cphMain和cphSideBar。一个内容区域中的ASP.NET控件需要引用另一个内容区域中的控件

其中一个相应的内容页面在cphMain中有一个控件,需要引用cphSideBar中的控件。具体来说,cphMain中的SqlDataSource引用cphSideBar中的TextBox作为select命令中的参数。当内容页面加载发生以下运行时错误:

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 

我有点知道是什么问题... ASP.NET不喜欢的事实,SqlDataSource和文本框在不同的ASP:内容控制在内容页面内。

作为一种解决方法,我有另一个文本框在cphMain与SqlDataSource有Visible = False。然后在Page_Load()事件处理程序中,将cphSideBar中的TextBox内容复制到cphMain中不可见的TextBox的内容中。

我得到了我想要的结果我想到的结果,但它似乎是这样的黑客攻击。我想知道是否有更好的解决方案,我错过了。

请指教。

回答

0

我相信你需要引用控件使用它们的容器。

例如,得到一个ContentPlaceHolder参考:

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

其中MyContentContentPlaceHolderID,而不是ContentPlaceHolderID

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

然后,您可以使用的FindControl找到MyContent容器中的子控件:

myContent.FindControl("someControlID").Visible = false; 
+0

在我的代码后面,我可以在内容页面的任何区域引用任何控件。问题似乎在标记中。对于SqlDataSource,我决定将参数类型从控件更改为无,所以至少我不需要使用不可见的文本框。这个解决方案对我来说已经足够了。 – harrije 2010-06-04 10:09:13

相关问题