ASP.NET Custom Server Management Console (GridView), DataBinding and Control Lifecycle

I have created a custom server control (Inherited from GridView).

On the page, the GridView is a DataBound for the DataSet, so at design time I have no idea what columns will be present in my GridView.

Now what I want to do is add a textbox to each cell for the title row of the GridView and these textboxes will control the filtering of the columns. (text boxes are added using the GridView's OnRowCreated method).

So far so good, text boxes appear and filtering works.

The problem is that after every postback, the text value of the text boxes is lost. From my experiments, this seems to be due to the fact that I am adding textboxes too late to the page / control lifecycle.

How do you solve this type of problem where I will need to create and add textboxes early in the lifecycle (e.g. GridView OnInit), but adding textboxes depends on the information obtained later in the lifecycle

+1


source to share


3 answers


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



+1


source


You don't need to worry about the values ​​of the text box, just their ID and when you create them; (... hmmm ... or maybe viewstate) will take care of the rest while you create and "deliver" a page with the same number of text fields and their corresponding (unique (!)) IDs.



You can do this: Page_Init and Page_Load ... Page_Init is somewhat recommended, but it depends on your needs.

0


source


There is a page that would be helpful: 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 if this is the first page render time.
  • Create or recreate dynamic controls .
  • Dynamic setting of the main page.
  • Set the Theme property dynamically.
  • Reading or setting values ​​for profile properties.
0


source







All Articles