Title: asp.net textbox.text and jquery.val()
Tags: jquery;asp.net
Question: I'm reading the contents of a an asp.net TextBox on a button click event in the codebehind of a webpage.

This works fine if I type something into the box I can read whats in there via TextBox.Text.

However, if I copy into the input textbox using jquery, setting the contents using val(), I can see the text appear in the box but when the click event fires and I try to read the contents of the textbox it is always blank.  There's only every anything in there if I type it in myself.

The relevant bits of code are: -
The input box

```<asp:TextBox runat="server" ID="deliveryAddress3" CssClass="required radius disabled sugestedAddressTargetCity bcity2" /&gt;
```

Javascript

```var bfields = ['.baddress', '.bcity', '.bcountry', '.bpostcode'];
    var dfields = ['.baddress2', '.bcity2', '.bcountry2', '.bpostcode2'];
for (var i in dfields) {
                    $(dfields[i]).prev('label').hide();
                    $(dfields[i]).val($(bfields[i]).val());
                    $(dfields[i]).attr('disabled', 'disabled');
                    $(dfields[i]).addClass('disabled');
                    $(dfields[i]).attr('disabled', 'disabled');
```

Code in button click method of codebehind: -

```customer.DelTown = deliveryAddress3.Text;
```

Whats going on here is that the customer can copy their address from one set of boxes to another.  If they do this (by clicking a button) the ext shows up in the boxes but in the code behind is blank.  However, if I type something in to those boxes it is available in the code behind.

Comment: dynamically added content from jQuery will not show in page source. I think this is what you are asking. Try to make your question more clear, and maybe add a jsFiddle, with example.

Comment: using attr() worked once I'd removed the adding of the disabled property too.

Comment: If you set the value attribute instead, does that work? Like this: `$("yourTextBox").attr("value", "newValue");`

Comment: It's going to be difficult for us to help without seeing any of your code.

Here is the accepted answer: I started a new ASP.net application and it works if you set the text box's ```value``` attribute using jQuery ```attr()```.  

The script updates the value inside the textbox and the code behind is able to read it too.

Designer HTML (added the below to the default.aspx):

```<asp:TextBox runat="server" ID="MytextBox"&gt;</asp:TextBox&gt;
<asp:Button runat="server" ID="MyButton" Text="Click Me"/&gt;
```

Rendered HTML output:

```<input name="ctl00$MainContent$MytextBox" type="text" value="NewValue" id="MainContent_MytextBox"&gt;
<input type="submit" name="ctl00$MainContent$MyButton" value="Click Me" id="MainContent_MyButton"&gt;
```

Script:

```$("#MainContent_MytextBox").attr("value", "NewValue");
```

Code Behind from default.aspx.cs:

```protected void Page_Load(object sender, EventArgs e)
{
    // Place Debugger here:
    if (MytextBox.Text == "NewValue")
    {
        // hurray
    }
}
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:01/30/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<jquery><asp.net>', 'Title': 'asp.net textbox.text and jquery.val()', 'ViewCount': 4991, 'id': 14603392, 'user_AboutMe': '', 'user_AccountId': 52975.0, 'user_DisplayName': 'Carbonara', 'user_Reputation': 69.0, 'user_Views': 9.0, 'user_id': 158404.0}}