views:

170

answers:

2

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.

+1  A: 

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.

Kit Menke
I've referred that before and still not clear. Check my comment on next answer.1) ViewState even for dynamic controls?2) I want it to be bound only on Postback - is it ok to check Page.IsPostBack within OnPreRender?
LVS
Yes to both questions. You'll add your textbox and button in CreateChildControls. Then, in OnPreRender, you can check to see IsPostBack (and that your textbox.Text has a value) and then add your grid / databind.
Kit Menke
OK thanks! I am now using my own flag - changing Personalizable values also causes a postback and some values might not always be set in such cases
LVS
A: 

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.

brian brinley
According to http://stackoverflow.com/questions/1648447/why-does-onload-createchildcontrols-order-change-at-postback and many other resources, CreateChildControls() is called before Postback event handling - won't Page.FindControl() bring up the new control instead?I did try and am still not able to get the values.
LVS
after rereading your comment, i haven't used the method that you are using to create your event handler. I have always created my method and bound the click event to that. Below is a code sample of a simple event handler for a dynamically created control.
brian brinley
Button submitButton = (Button)this.FindControl("submit"); submitButton.Click += new EventHandler(SubmitContactForm);and here is the method submitContactForm protected void SubmitContactForm(object sender, EventArgs e) {....} inside the method I used this, in my case it was dynamic form fields that I knew were labels. // get the control name object ctrl = FindControl(controlName); string controlValue = ((TextBox)ctrl).Text;
brian brinley
Thanks for that. But all of a sudden my ways seems to be working now! I removed all code and started again - maybe I missed something else.
LVS