Treeview and custom user controls problem in asp.net

J

joseph

Hi,

I have created 2 custom user controls, one is gridview search list and the
other is login control. I use a webform, which has a treeview menu one side,
to load one of the user control when user click each one of the treeview
node. The treeview has 2 nodes, one is 'Search List', another is 'Login'. I
can load the user control on the treeview's SelectedNodeChanged event, but
there is the weird thing. When I click on the Login node, it shows the Login
Control, but when I click on the submit button, the login control will
disappear. This happen to the gridview control too. Another way is if I
clink on the same node the second time, the user control will disappear.
Below is my code:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadUserControl();
}
}

protected void TreeView1_SelectedNodeChanged(object sender,
EventArgs e)
{
TreeView tv = (TreeView)sender;
Session["TreeNode"] = tv.SelectedNode.Text.ToLower();
LoadUserControl();
}

protected void LoadUserControl()
{
PanelSearchList.Controls.Clear();
Control SearchListControl = null;
switch (Session["TreeNode"].ToString().ToLower().Trim())
{
case "login":
SearchListControl = LoadControl("LoginUserControl.ascx");
break;
default:
SearchListControl = LoadControl("SearchControl.ascx");
break;
}
PanelSearchList.Controls.Add(SearchListControl);
}

Can anyone tell me how to solve this problem?

Thanks in advance.

Joseph
 
G

Gregory A. Beamer

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadUserControl();
}
}

When you dynamically load controls, you end up having to make sure they
are repainted. the pattern you have here is a bit dangerous.

If you want an easy pattern, place all of the controls on the form and
then hide them when they are not valid. If you have to, load the control
(s) into Panel(s) and hide the irrelevant panels, based on user input.

When you do this, ASP.NET is in full control of the viewstate and you
don't have to worry about dynamically loading bits.

If this is not an option, you should really learn the Page lifecycle and
event model, as much of the dynamic work should really lake place before
load.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

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

Top