【问题标题】:ASP.NET control in one content area needs to reference a control in another content area一个内容区域中的 ASP.NET 控件需要引用另一个内容区域中的控件
【发布时间】:2010-06-03 20:24:43
【问题描述】:

我有一个将主要内容分为两个区域的母版页。母版页正文部分有两个 asp:ContentPlaceHolder 控件,ID 分别为 cphMain 和 cphSideBar。

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

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 和 TextBox 在内容页面中的不同 asp:Content 控件中的事实。

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

通过我想出的解决方法,我得到了我想要的结果,但这似乎是一个 hack。我想知道我是否缺少更好的解决方案。

请指教。

【问题讨论】:

    标签: asp.net master-pages


    【解决方案1】:

    我认为您需要使用它们的容器引用控件。

    例如,获取对 ContentPlaceHolder 的引用:

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

    其中MyContentContentPlaceHolderID,而不是ContentPlaceHolderID

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

    然后,您可以使用 FindControlMyContent 容器中定位子控件:

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

    【讨论】:

    • 在我后面的代码中,我可以引用内容页面任何区域中的任何控件。问题似乎出在标记中。对于 SqlDataSource,我决定将参数类型从 control 改为 none,这样至少我不需要使用不可见的 TextBox。这个解决方案对我来说已经足够了。
    猜你喜欢
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多