Share via


get textBox value using ajax

Question

Wednesday, October 30, 2013 5:30 AM

Hi

Hi if m using runat="Server" value come undefind

  <input type="text" id="txtID"  runat="server"   />-

and m using without runnat server   <input type="text" id="txtID"   /> value get correctly

$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'PayRollAdd.aspx/InsertMethod',

data: "{'txtid':'" + $('#txtID').val() + "'}",
async: false,
success: function (response) {
$('#btnSubmit').prop('disabled', false);
$('#btnSubmit').prop('value', 'Add New');

var result = response.d.split(",");
$('#txtID').val('');
$('#txtID').val(result[3]);
id = result[3];
if (result[0] == "Saved Sucessfully") {

}
},
error: function ()
{ console.log('there is some error'); }
});

how to get value  using runnat server in my textbox**<input type="text" id="txtID"  runat="server"   />-**

thanks

All replies (3)

Wednesday, October 30, 2013 5:34 AM ✅Answered

Hi

Try the following:

$('#<%=txtID.ClientID%>').val()

instead of

$('#txtID').val()

Wednesday, October 30, 2013 5:35 AM ✅Answered

The control ID for HTML markup that is generated by ASP.NET can be different with the one you set in the runat="server" control.

Either run your page and go to View Source and see what id it has or use the ClientID property as

data: "{'txtid':'" + $('#<%=txtID.ClientID%>').val() + "'}",


Wednesday, October 30, 2013 5:36 AM

You don't use ajax for that, your server-side controls only have values you can interrogate when you are doing a postback.  An ajax call isn't a postback.  If you want to find out what text is in the box;

var text = $("#<%=txtID.ClientID%>").val();