[英]On button click send Ajax call and get value from aspx.cs(code behind) to aspx page and display it in input field in the same page
I am new to AJAX
and have stuck in a problem, I want to send AJAX CALL on button click, while button clicking the value of input field is sent to aspx.cs
(code behind) page and response should be get and I try to show that response to that same input field but it's not working. 我是
AJAX
新手,并且遇到了问题,我想在单击按钮时发送AJAX调用 ,而单击按钮时将输入字段的值发送到aspx.cs
(后面的代码)页面,应该获取响应,并且我尝试显示对该相同输入字段的响应,但不起作用。
ASPX CODE: ASPX代码:
<input type="text" class="wpcf7-text" id="d_id" name="d_id" runat="server"> // this id is sent to the Read_Driver_Account funtion and on response it is sending the driver cell number which i want to display in the driver cell number imput field
<input class="wpcf7-text" id="driver_cell" name="driver_cell" runat="server">
<asp:Button ID="btnn1" OnClick="Read_Driver_Account" runat="server" Text="Get"/>
<script>
$("btnn1").click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/Udate_Driver_Account/", //Udate_Driver_Account.aspx file is placed in VisualStudio/Websites/Fp/Udate_Driver_Account.aspx, where Fp is name of my website.
data: {
id: $(this).val(),
driver_cell: $("#drivercel").val()
},
success: function (result) {
alert('ok');
},
error: function (result) {
alert('error');
}
});
});
</script>
ASPX.CS CODE: ASPX.CS代码:
public string drivercel;
protected void Read_Driver_Account(object sender, EventArgs e)
{
var alldata = d_id.Value;
string driveradres = tokens[7];
string drivercel = tokens[8]; // i want to set this value to the input field id="driver_cell"
}
Note: tokens[8] is a value coming from a live database it's not a static value; 注意:tokens [8]是来自实时数据库的值,它不是静态值。 Also i want to set more than one values to more than one input fields.
我也想为多个输入字段设置多个值。
The JQuery
selector you used should be $("#btnn1")
instead of $("btnn1")
. 您使用的
JQuery
选择器应该是$("#btnn1")
而不是$("btnn1")
。 Then the ajax url should be /pageName/webMethod
like /Udate_Driver_Account.aspx/Read_Driver_Account
. 然后,ajax网址应为
/pageName/webMethod
如/Udate_Driver_Account.aspx/Read_Driver_Account
。
And use JSON.stringify
to send the data as data: JSON.stringify({ id: $(this).val(), driver_cell: $("#drivercel").val() })
. 并使用
JSON.stringify
将数据作为data: JSON.stringify({ id: $(this).val(), driver_cell: $("#drivercel").val() })
发送data: JSON.stringify({ id: $(this).val(), driver_cell: $("#drivercel").val() })
。
Add contentType: "application/json; charset=utf-8"
to send the data as json and dataType: "json"
to get the response data as json. 添加
contentType: "application/json; charset=utf-8"
以将数据作为json发送,而dataType: "json"
以将响应数据作为json发送。
<input class="wpcf7-text" id="driver_cell" name="driver_cell" runat="server">
<asp:Button ID="btnn1" OnClick="Read_Driver_Account" runat="server" Text="Get"/>
<script>
$("#btnn1").click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/Udate_Driver_Account.aspx/Read_Driver_Account", //Udate_Driver_Account.aspx file is placed in VisualStudio/Websites/Fp/Udate_Driver_Account.aspx, where Fp is name of my website.
data: JSON.stringify({
id: $(this).val(),
driver_cell: $("#drivercel").val()
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(result);
// $("#drivercel").val(result.d);
},
error: function (result) {
alert('error');
}
});
});
</script>
Then mark your method with WebMethod
attribute and make it as public static
with parameter name same as the data json above. 然后使用
WebMethod
属性标记您的方法,并使其参数名称与上面的数据json相同,使其成为public static
。
public string drivercel;
[System.Web.Services.WebMethod]
public static string Read_Driver_Account(string id, string driver_cell)
{
string drivercel = tokens[8]; // i want to set this value to the input field id="driver_cell"
return drivercel;
}
Note : As you used the asp:Button
id directly in JQuery
selector, you should add ClientIDMode="Static"
property to the button as below to use the same id in client side (OR add the same in Page
directive). 注意 :当您直接在
JQuery
选择器中使用asp:Button
id时,应按如下所示向按钮添加ClientIDMode="Static"
属性,以在客户端使用相同的ID(或在Page
指令中添加相同的ID)。 Otherwise, the button will have different dynamic id in client side and the JQuery click event won't get fired. 否则,按钮将在客户端具有不同的动态ID,并且不会触发JQuery click事件。
<asp:Button ID="btnn1" ClientIDMode="Static" OnClick="Read_Driver_Account" runat="server" Text="Get"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.