2010-06-03 72 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

相關問題