0

I've got a custom ASP.Net web user control I've built; done this enough times before but this one is misbehaving and I can't spot why.

Where the calling page includes the user control directly in its markup, all is well and the control behaves as expected.

However if the page adds this particular control dynamically (to a placeholder in the master page, which is what's calling this whole thing) the elements within it stay firmly NULL - nothing from the user control gets written to the client at all, including static content within the user control.

Where might I be going wrong?

4

4 に答える 4

2

並べ替えられた回答 - ID は重要ではなく、なくてもかまいませんが、そうする場合

Control a = new Control;

それはうまくいきません

Control a = (Control)Page.LoadControl("~/Folder/Control.ascx");

夜遅くに忘れていたことが機能します:-)

于 2010-07-23T00:33:18.953 に答える
1

ユーザーコントロールを動的にロードしているとき...

ASP.NET Web サイト プロジェクトの場合、次のように動作します。

Control a = new Control;

ASP.NET Web アプリケーション プロジェクトの場合、次のように動作します。

Control a = (Control)Page.LoadControl("~/Folder/Control.ascx");
于 2012-01-04T17:44:45.203 に答える
0

ユーザー コントロールを動的に追加する場合、追加されたコントロールごとに一意の ID を生成する必要があります。

例えば:

Control selWebControl = (Control)Page.LoadControl("~/DL/Templates/FileLibrary.ascx");
selWebControl.ID = "UC" + "_" + dfRow.ID;
于 2010-07-01T00:56:23.643 に答える
0

ページ上にある Controls コレクションに追加していることを確認し、ページのライフサイクルの適切なタイミングでそれを行っていることを確認してください。私は CreateChildControls をオーバーライドするのが好きです。

したがって、Page.Controls.Add(myNewControl) または PlaceHolder1.Controls.Add(myNewControl) を実行していることを確認してください。

コントロールを動的に作成してページに追加するコードを示した場合に役立つ場合があります

于 2010-07-01T05:27:40.813 に答える