0
votes

I am initializing a GridView, text box and a button via code to a Webpart in CreateChildControls()

The above controls are declared as class variables but initialized only later.

Next, I've given the handler for button click. The handler function is supposed to work as a search - perform some operations on the content entered in the textbox, load the results in the Gridview, display the Gridview

When I type something in the text box and hit the button, the same controls load again and the content entered in the text box is lost. I've tried ViewState() and ViewState() but to no avail. The grid doesn't show because my logic skips attempting to bind it since a proper search string was not available.

My questions: 1) Where/how can I get the values postback from the textbox? 2) Will it make sense to populate the GridView in PreRender() or will Event Handling happen after PreRender()?

EDIT: Seems like the event handler is not getting called.

Dim btnClickHandler = New EventHandler(AddressOf SetSearchParameter)
AddHandler srchBtn.Click, btnClickHandler

is correct?

EDIT: I redid all code from 0. Its working now.

2
The handler for button click is also done inside CreateChildControls() - Vid L

2 Answers

1
votes

This always confuses me too. I always reference this question in order to get it straight (also the control execution lifecycle).

  1. You shouldn't need to manage viewstate, that should be taken care of automatically by your controls.

  2. I would suggest using the OnPreRender to populate your grid. Your controls should have their values populated by the ViewState by then.

0
votes

you should be able to reference the control values in the event handler for the button click, depending on how to build them static/dynamic. (textbox)Page.FindControl('controlname').value in your event handler for the button click.

er... this.findcontrol.

im doing this from memory so syntax might be off.