Accessing the page from dynamically loaded control

G

Guest

I have a web form, which load a web control by using the LoadControl
method

void Page_Init(...)
{
Control ctl = LoadControl(Request.QueryString["module"]+".ascx"); //
url = default.aspx?module=control1
Panel1.Controls.Add(ctl);
}

Now, I want to add to the web form a DropDownList control, which
should interact with the web control.

void Page_Load(...)
{
DropDownList ddl = new DropDownList();
ddl.DataSource = ...
ddl.DataBind();

ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(IndexChanged);
}

void IndexChanged(object sender, EventArgs e)
{
Session["MY_KEY"] = ((DropDownList)sender).SelectedItem.Value;
}

and now I want to use an actual value of the DropDownList (or a
session key) in the web control

void Page_Load()
{
Response.Write(Session["MY_KEY"]);
}

The problem is here that when the selected item in the DropDownList
has been changed, the sequence is following:

webform.init/load
webcontrol.load
webform.IndexChanged

And finally the rendered page shows the "old" Session["MY_KEY"]
because the IndexChanged has been executed after the contol is loaded.

So, how do I get the actual value of the DropDownList inside the web
control?
 
G

Guest

I have a web form, which load a web control by using the LoadControl
method

void Page_Init(...)
{
Control ctl = LoadControl(Request.QueryString["module"]+".ascx"); //
url = default.aspx?module=control1
Panel1.Controls.Add(ctl);

}

Now, I want to add to the web form a DropDownList control, which
should interact with the web control.

void Page_Load(...)
{
DropDownList ddl = new DropDownList();
ddl.DataSource = ...
ddl.DataBind();

ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(IndexChanged);

}

void IndexChanged(object sender, EventArgs e)
{
Session["MY_KEY"] = ((DropDownList)sender).SelectedItem.Value;

}

and now I want to use an actual value of the DropDownList (or a
session key) in the web control

void Page_Load()
{
Response.Write(Session["MY_KEY"]);

}

The problem is here that when the selected item in the DropDownList
has been changed, the sequence is following:

webform.init/load
webcontrol.load
webform.IndexChanged

And finally the rendered page shows the "old" Session["MY_KEY"]
because the IndexChanged has been executed after the contol is loaded.

So, how do I get the actual value of the DropDownList inside the web
control?

Well, found one possible solution to use the Page_PreRender in a web
control (instead the Page_Load).

void Page_PreRender()
{
Response.Write(Session["MY_KEY"]);
}

Any other ideas?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top