Micah
Micah

Reputation: 1

Why is one of my aspx TextBox values not transfering to my C# code?

I have an ASPX webform that takes some data and just sends an email. It has worked for years but I just added a new field and I can't get the Text value to transfer to the c# code that writes the email.

The difference is that the new field is populated through jQuery/JSON look-up and it is read-only. The jQuery portion is working fine.

TextBox in question:

<asp:TextBox ID="autoRegion" Width="30" ReadOnly="true" onfocus="this.blur();" TabIndex="-1" CssClass="disabledInput" Value="" runat="server" />    

Working TextBoxes:

<asp:TextBox ID="FName" MaxLength="40" TabIndex="1" runat="server" />
<asp:TextBox ID="LName" MaxLength="40" TabIndex="3" runat="server" />

C# variable for email body content:

objMM.Body += "<table><tr><td>" + 
     LName.Text + "</td><td>" + FName.Text.ToUpper() + "</td><td>" +
     streetAddress1.Text + "</td><td>" + city.Text.ToUpper() + "</td><td>" +
     state.SelectedItem.Text + "</td><td>" + zip.Text + "</td><td>" + autoRegion.Text +
     "</td></tr></table>";

All other fields are working but autoRegion.Text is not.

I have also tried filling a variable with Request.Form["autoRegion"] and using that variable in place of autoRegion.Text but that did not work either.

Any help would be appreciated.

Upvotes: 0

Views: 1358

Answers (4)

Lalit Raghav
Lalit Raghav

Reputation: 59

Your should do some changes

  1. remove read only property in textbox as given below

    1. this propety add on page load as given below protected void Page_Load(object sender, EventArgs e) { autoRegion.Attributes.Add("readonly","readonly"); }

Upvotes: 0

pseudocoder
pseudocoder

Reputation: 4392

Mark Fitzpatrick is right, the problem is you are setting that TextBox to ReadOnly, which means its value is not persisted in ViewState.

Here is a potential workaround: Disabled textbox losses viewstate

Edit: Also Strange behavior using HTML 'readonly="readonly"' vs. JavaScript 'element.readOnly = true;'

Upvotes: 0

Aaron Hawkins
Aaron Hawkins

Reputation: 2691

Check out this link: ASP.NET textbox losing value after postback when value is changed client-side via javascript. It appears that the postback only pushes the ViewState data for a TextBox that is either Disabled or ReadOnly. Because you are loading the value via client-side, there is nothing in the ViewState for your TextBox.

There are several different work-around, but I would attempting the answer to this question:

protected void Page_Load(object sender, EventArgs e)
{
    TextBox1.Attributes.Add("readonly", "readonly");
}

In addition to applying this code, you would need to remove the ReadOnly="true" property from the TextBox on the aspx page.

Upvotes: 1

Mark Fitzpatrick
Mark Fitzpatrick

Reputation: 1624

If I remember correctly, read only items do not have their values posted back to the server since there would be no reason since they cannot be changed.

Upvotes: 1

Related Questions