469,573 Members | 1,702 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

Accessing the page from dynamically loaded control

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?

Mar 27 '07 #1
1 1338
On Mar 27, 2:12 pm, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
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?

Mar 27 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Earl Teigrob | last post: by
2 posts views Thread by Chris Kettenbach | last post: by
3 posts views Thread by Jordan | last post: by
2 posts views Thread by Vivek Sharma | last post: by
7 posts views Thread by =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.