State not preserved in inner controls of ASCX control

O

Oleg Ogurok

Hi all,

In my page class, I'm loading a user control:

private void Page_Load(object sender, System.EventArgs e)
{
Control c = LoadControl("WebUserControl1.ascx");
Controls.AddAt(0, c);

}

Inside WebUserControl1.ascx I have a hyperlink whose NavigateUrl property is
set at runtime (e.g. in Page_Load). Here's the code form
WebUserControl1.ascx.cs

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
HyperLink1.NavigateUrl = "/SomePage";
}

The problem is that if my page has a control that causes postback (e.g.
Button), the next time the page loads, the hyperlink inside the user control
doesn't retain its NavigateUrl property. I'm wondering why this is
happening.

In my scenario, I can't drop the user control (ASCX) on the page, because
the page is a bare .cs class (base page) without ASPX.

Thanks,
-Oleg.
 
T

Teemu Keiski

Hi,

try such that don't use Controls.AddAt, but instead put a placeholder onto
the Page and add the user control to its Controls collection or optionally
use Controls[1].Controls.Add(c); (By default Form is at this index so this
is same as adding Control to Form's Controls collection)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top