[英]Call asp.net function from javascript function
I want to call asp.net function from a javascript function passing string value. 我想从传递字符串值的JavaScript函数中调用asp.net函数。
this is the asp.net function : 这是asp.net函数:
[System.Web.Services.WebMethod]
public static string InsertData(string ID)
{
string source = "Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=30";
using(SqlConnection con = new SqlConnection(source))
{
using (SqlCommand cmd = new SqlCommand("Insert into Book (Name) values(@Name)", con)) {
con.Open();
cmd.Parameters.AddWithValue("@Name", ID);
cmd.ExecuteNonQuery();
return "True";
}
}
}
So i want to call this function from javascript function which passes the string value "ID" to the the asp.net function. 所以我想从javascript函数中调用该函数,该函数将字符串值“ ID”传递给asp.net函数。
this is the javascript function i use 这是我使用的javascript函数
function CallMethod() {
PageMethods.InsertData("hello", CallSuccess, CallError);
}
function CallSuccess(res) {
alert(res);
}
function CallError() {
alert('Errorrr');
}
and i call it from here 我从这里叫它
<body>
<header>
</header>
<div class="table" id="div1" > </div>
<form id="Form1" runat="server">
<asp:Button id="b1" Text="Submit" runat="server" onclientclick="CallMethod();return false;"/>
<asp:ScriptManager enablepagemethods="true" id="ScriptManager1" runat="server"></asp:ScriptManager>
</form>
</body>
so i have a button and onClick i want to add "Hello" Row to my table but nothing happens and CallError function calls 所以我有一个按钮和onClick我想向我的表添加“ Hello”行,但是什么也没有发生,并且CallError函数调用
You can call web method from ajax call in javascript . 您可以从javascript中的ajax调用中调用Web方法。 you need to set url parameter values to function you want to call and you can pass value in the data prameter in json formate. 您需要将url参数值设置为要调用的函数,然后可以在json formate中的数据参数中传递值。 like this data:"{ParamterName:'VALUE'}
像这样的data:"{ParamterName:'VALUE'}
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: "YourPage.aspx/YouPageMethod",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert('Method Called Sucess fully');
},
error: function (result) {
alert("error " + result);
}
});
});
</script>
OR 要么
you can call using PageMethod Eample 您可以使用PageMethod Eample进行调用
<script type="text/javascript">
function callInsert() {
PageMethods.InsertDate(id, OnSucceeded, OnFailed);
}
function OnSucceeded(response) {
alert(response);
}
function OnFailed(error) {
alert(error);
}
/* call this javascript function */
callInsert();
</script>
You will need to include Jquery first in your page. 您首先需要在页面中包含Jquery。 Then you need to add the following Javascript code. 然后,您需要添加以下Javascript代码。
<script type="text/javascript">
var id ="5"; //your id will be stored here
$(document).ready(function () {
$.ajax({
type: "POST",
url: "YourPage.aspx/InsertData" + "&?id=" ,
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert('Success');
},
error: function (xhr, request, status) {
alert(xhr.responseText);
}
});
});
</script>
You need to have scriptManager in your .aspx page 您需要在.aspx页中具有scriptManager
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
After that you can call the method using 之后,您可以使用
<script type="text/javascript">
function func(){
PageMethods.InsertData(id,success_msg,failure_msg);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.