views:

273

answers:

1

I am trying to create a popup which will be used to select a month/year for a textbox. I have kind of got it working but however when I try and read from the textbox when I Submit the form it returns an empty string. However visually on the page I can see the result in there when I click the Done button which can be seen in the screenshot.

http://i27.tinypic.com/2eduttx.png - is a screenshot of the popup

I have wrapped the whole textbox/popup inside a Web User Control

Here is the code of the control

Code Behind

ASP Page

and then read from the Textbox on the button click event with the following

((TextBox)puymcStartDate.FindControl("txtDate")).Text

Any suggestions of how to fix the problem?

+1  A: 

You may need to read the form posted value rather than the value from the view state. I have the following methods in my code to handle this.

The below code just grabs the values in the request headers (on post back) and sets/updates the controls. The problem is that when using the ASP.NET Ajax controls, it doesn't register an update on the control, so the viewstate isn't modified (I think). Anyways, this works for me.

protected void btnDone_Click(object sender, EventArgs e)
{
    LoadPostBackData();
    // do your other stuff
}

// loads the values posted to the page via form postback to the actual controls
private void LoadPostBackData()
{
    LoadPostBackDataItem(this.txtYear);
    LoadPostBackDataItem(this.txtDate);
    // put other items here if needed
}

// loads the values posted to the page via form postback to the actual controls
private void LoadPostBackDataItem(TextBox control)
{
    string controlId = control.ClientID.Replace("_", "$");
    string postedValue = Request.Params[controlId];
    if (!string.IsNullOrEmpty(postedValue))
    {
        control.Text = postedValue;
    }
    else
    {
        control.Text = null; // string.Empty;
    }
}
Jim W
Thank you so much!
Malachi