0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Micah
  • 1
  • 2
  • possible duplicate of [Strange behavior using HTML 'readonly="readonly"' vs. JavaScript 'element.readOnly = true;'](http://stackoverflow.com/questions/1060518/strange-behavior-using-html-readonly-readonly-vs-javascript-element-readon) – pseudocoder Oct 10 '14 at 21:52

4 Answers4

1

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.

Mark Fitzpatrick
  • 1,624
  • 1
  • 11
  • 8
1

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.

Community
  • 1
  • 1
Aaron Hawkins
  • 2,611
  • 1
  • 20
  • 24
0

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;'

Community
  • 1
  • 1
pseudocoder
  • 4,314
  • 2
  • 25
  • 40
0

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"); }
Lalit Raghav
  • 59
  • 1
  • 11