Determining selected node in OnInit

  • Thread starter Thread starter Mike Collins
  • Start date Start date
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
{
}
 
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.
 
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.
 
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
{
}
 
Back
Top