We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

When we create ASP controls at runtime, the fresh instances created are available in the current scope only. In the next page postback they are lost.That means we  loose the controls and they are no longer existing in the page now. Now if we try to find the control's ID then we get error.

There are many ways to solve this problem. Some of them are:
 
      1. We can create them in "OnInit" method as it is always called before Page_Load. So we will be able to get the instances for the controls.
 
      2. If you are creating control in any Button_Click event, then we need to recreate those on every page_Load. For that we can keep the number of controls to be created in Viewstate.
 
.cs file
--------------

protected

{

   {

     CreateDynamicControl();

    }

}

{

  CreateDynamicControl();

}

{

void Page_Load(object sender, EventArgs e)if (IsPostBack)protected void Button1_Click(object sender, EventArgs e)protected void CreateDynamicControl()//Create Controls according to number stored in view state

ViewState[

"CONTROLCOUNT"] = 5;//Store the required number of controls in Viewstate

}

Another suggestion is that while creating more than one dynamic controls, we should give them unique IDs. Otherwise it will create conflict while finding that controls.


Related Tags:

ASP.NET

Author: Anita Bhanja

top

ASP.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login