Codeman27
Codeman27

Reputation: 13

JQuery Set Value of ASP Textbox and have the .Text property return that value

Ok, I've searched for a while but still haven't found anything that fixes my issue.

I am updating a readonly Textbox in ASP.NET with JQuery after the selection of radio buttons. The value shows up and everything seems to work from an HTML standpoint but Address.Text in the code behind returns nothing.

Also, I've posted all my attempts in the JavaScript section, I know I should only need one of these to update what I'm trying to update.

HTML:

<asp:TextBox class="form-control form-inline" runat="server" ID="Address" placeholder="Populates based off account type" readonly="true"/>

JavaScript:

$(document).ready(function () {
    $('input[type=radio]').click(function () {       
        $('#Address').attr("value", $(this).val());
        $('#Address').attr("Text", $(this).val())
        $('#Address').val($(this).val());
        $('#<%= Address.ClientID %>').val($(this).val());
        $('#<%= Address.ClientID %>').text($(this).val());
        $('#AccountType').val($(this).val());
    });     
});

ASP.NET

ClientScript.RegisterStartupScript(GetType(), "myalert", "alert('Address: " + Address.Text + "' );", true);

Thanks in advance to anyone that can help me out.

Upvotes: 1

Views: 1108

Answers (1)

kman
kman

Reputation: 2257

When you set the ReadOnly property to true of an ASP.NET TextBox, it basically assumes the value can't be changed clientside and won't post changes back. Try setting it readonly like:

textBox.Attributes.Add("readonly", "readonly");

Additional SO post for reference.

Upvotes: 1

Related Questions