Saturday, March 24, 2012

Need Help - UserControl Completely disappears

I'm developing an ASP.NET Ajax application in VS2005. The problem is describe bellow

Scenario
=============
I have a ASPX page with a UpdatePanel( say it is UpdatePanel_ 1) and a Button(say it is UpdatePanel_ 1_Button). Under the UpdatePanel_ 1 I have a Panel(say it is BodyPanel_1) to load different control as require.

On UpdatePanel_ 1_Button click event, it will load different UserControl in the BodyPanel_1 and then call UpdatePanel_ 1.Update() method.

Now say I have a UserControl( say it is UserControl_ 1) have a TextBox and a Button(say it is UserControl_ 1_Button). On click on the Button(UserControl_ 1_Button), add a simple text(say - "You Click Me") in the UserControl_ 1's TextBox.

Issue
==========
If I load the UserControl(UserControl_ 1) in PageLoad event it works fine.

But when I load the UserControl_ 1 on the page UpdatePanel_ 1_Button click event, it load the UserControl_ 1 but in that UserControl when I click on the UserControl' s ( The UserControl_ 1_Button ) Button the UserControl Completely disappears ( The UserControl_ 1 get vanish from the page ).

In the design mode I can drag the user control (UserControl_ 1) and at run time can do visible and invisible but it's not the solution as I like to load different controls in the same page base on some logic.

Following is my Button Click event function.
protected void UpdatePanel_ 1_Button_Click(object sender, EventArgs e)
{
//get the panel
Panel bodyPanel = (Panel)this. Page.FindControl ("BodyPanel_1");
//load the usercontrol
Control myControl = this.LoadControl( "UserControl_ 1");
myControl.ID = "45646546";
//add the usercontrol
bodyPanel.Controls. Add(myControl) ;
//update the UpdatePanel
UpdatePanel_ 1.Update();
}

The above function loads the UserControl( The UserControl_ 1), but when you click on the Button(UserControl_ 1_Button) under the UserControl, it disappears. But when I load the same UserControl in the Page Load every thing works fine.

I understand that when I load the UserControl dynamically at run time it's not register the UserControl' s events.

Please let me know your suggestion.

Thanks

Hi,

It's necessary to add the control in Page_load everytime. Please read this FAQ:

1. Why do I have to recreate dynamic controls every time? /Why dynamic controls are disappeared on PostBack?


Whenever a request comes, a new instance of the page that isbeing requested is created to serve the request even it's a PostBack. Allcontrols on the page are reinitialized, and there state can be restored fromthe ViewState in a later phase.

The dynamic controls have to be recreated again and added tothe control hierarchy. Otherwise, they won't exist in the page.

Please be careful with when to create dynamic controls. Inorder to keep their state, they have to be created before the LoadViewStatephase. Page_Init as well as Page_Load methods are options available.

For more information about this topic, please refer to thisarticle:

Creating Dynamic Data Entry User Interfaces[http://msdn2.microsoft.com/en-us/library/aa479330.aspx ]

Hope this helps.

No comments:

Post a Comment