Determining selected node in OnInit

M

Mike Collins

In my web application, I have a treeview. Based on which node is clicked, I
am loading user controls. Currently, I am loading my controls in the
SelectedNodeChanged event, but I want to load the user controls in OnInit so
I can have them in the ViewState, but the treeview is not created yet. Is
this possible, or do I need to keep loading my user controls after the OnInit
event?

Example of what I am doing now:
protected void tvAccountSetup_SelectedNodeChanged(object sender, EventArgs e)
{
AddControl(@"UserControls\" + tvAccountSetup.ID.Substring(2) + @"\" +
tvAccountSetup.SelectedValue + ".ascx");
}

private void AddControl(string control)
{
//In case the page cannot be found, with this code in a try catch, it will
not error.
try
{
PlaceHolder1.Controls.Clear();
Control c = Page.LoadControl(control);
PlaceHolder1.Controls.Add(c);
}
catch
{
}
 
G

Gaurav Vaish \(a.k.a. MasterGaurav\)

am loading user controls. Currently, I am loading my controls in the
SelectedNodeChanged event, but I want to load the user controls in OnInit
so
I can have them in the ViewState, but the treeview is not created yet. Is

Why would you need that?
The events have been structured in such a manner that you don't need to do
all that juggling.

Is there any specific reason as to why you need to "load" the controls
during "OnInit"?
btw, you don't need to "load" them in OnInit to participate in ViewState.

They must be added in the method AddChildControls().


HTH.
 
M

Mike Collins

Why would you need that?

I thought that when you dynamically load controls, you were supposed to do
that in OnInit so the control would be part of the pages ViewState. This way
if the page gets refreshed, the control will not disappear. I will look into
the AddChildControls method. Thanks.
 
M

Mike Collins

Thanks...I'll look into that.

Eliyahu Goldin said:
Request.Form collection is available in OnInit event. Set a breakpoint on
the event, look into the Request.Form and see if it contains any info that
you can use.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Mike Collins said:
In my web application, I have a treeview. Based on which node is clicked,
I
am loading user controls. Currently, I am loading my controls in the
SelectedNodeChanged event, but I want to load the user controls in OnInit
so
I can have them in the ViewState, but the treeview is not created yet. Is
this possible, or do I need to keep loading my user controls after the
OnInit
event?

Example of what I am doing now:
protected void tvAccountSetup_SelectedNodeChanged(object sender, EventArgs
e)
{
AddControl(@"UserControls\" + tvAccountSetup.ID.Substring(2) + @"\" +
tvAccountSetup.SelectedValue + ".ascx");
}

private void AddControl(string control)
{
//In case the page cannot be found, with this code in a try catch, it will
not error.
try
{
PlaceHolder1.Controls.Clear();
Control c = Page.LoadControl(control);
PlaceHolder1.Controls.Add(c);
}
catch
{
}
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top