views:

2594

answers:

3

Hi,

I have created a Custom Server Control (Inherited from GridView).

On the page, the GridView is DataBound to a DataSet, so I do not know at design time what columns will be present in my GridView.

Now, what I want to do is, to add a textbox in every Cell for the GridView Header row, and those textboxes will control column filtering. (textboxes are added using the GridView OnRowCreated method).

So far so good, the textboxes appear, and the filtering is working.

Problem is, after every postback, the Text value of the textboxes is lost. From my experimentations, this seems to be because I'm adding the textboxes too late in the Page/Control lifecycle.

How does one deal with this type of problem, where I would need to create and add the textboxes early in the Lifecycle (like, the GridView's OnInit), but adding the textboxes is dependant on information that is obtained later in the lifecycle?

+1  A: 

Why not store the values in the ViewState and read them back (refill the text boxes) on the postback?

gfrizzle
and apply the filter again
Victor
A: 

You don't have to worry about the textbox's values, just their ID and when you create them; control state(...hmmm...or maybe the viewstate) will take care of the rest, as long as you create and "supply" the page with the same number of textboxes and their respective (unique(!)) ID's.

You can do this is both Page_Init and Page_Load...Page_Init is somewhat recommended, but that depend on your needs.

Morten Bergfall
A: 

There´s a page that would be useful: http://msdn.microsoft.com/en-us/library/ms178472.aspx It specifically says that you need to use the *Pre_Init* event to create yout controls:

PreInit : Use this event for the following:

  • Check the IsPostBack property to determine whether this is the first time the page is being processed.
  • Create or re-create dynamic controls.
  • Set a master page dynamically.
  • Set the Theme property dynamically.
  • Read or set profile property values.
Seiti