如何在 javascript 函数中调用服务器端函数
How to call a Server Side Function in a javascript Function
我有一个按钮,点击后会执行此操作:
protected void Button6_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"myScript", "AnotherFunction();", true);
}
我还有另一个服务器端函数是这样的:
public void Delete()
{
//Delete Code
}
点击按钮后,现在会转到这个javascript
函数:
现在我想做的是,在服务器端调用 Delete()
函数。这是我的 javascript 函数,到目前为止我已经尝试过
function (isConfirm)
{
if (isConfirm)
{
//CALL DELETE FUNCTION HERE Delete();
swal("Deleted!", "Your imaginary file has been deleted.", "success");
}
else
{
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
如何调用该服务器端函数?有什么想法吗?
我会向页面添加 ScriptManager 并启用 PageMethods;
代码在:
在 ASPX 中:
<asp:ScriptManager runat="server" EnablePageMethods="true" />
在Javascript中:
<script>
PageMethods.Delete();
</script>
在ASPX.cs中:
[System.Web.Services.WebMethod]
public static void Delete()
{
//Delete Code
}
您可以通过两种方式实现这一目标。 Ajax/Web JS中的服务或触发按钮点击。最简单的是触发按钮点击。使用以下代码。
aspx:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" ClientIDMode="Static" style="display:none;"/>
c#:
protected void Button1_Click(object sender, EventArgs e)
{
Delete();
}
JS:
document.getElementById('Button1').click();
// jquery
$("#Button1").click();
但是如果您不想回发您的页面,请使用 Ajax。
在Ajax简单,你需要添加一个网页说data.aspx,在后端class of data.aspx你将添加以下c#
Ajax。 C#
[WebMethod]
public static int DoSomething(int Id)
{
return 1;
}
现在您可以从 JS 中调用它了:
$.ajax({
url: APP_PAGE_RELATIVE_PATH + "Data.aspx/DoSomething",
data: "{'Id':5}",
type: "POST",
cache: false,
headers: { "cache-control": "no-cache" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Do Something
},
error: function (xhr, status, error) {
//DebugAlert("Error: " + xhr.responseText);
}
});
我有一个按钮,点击后会执行此操作:
protected void Button6_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"myScript", "AnotherFunction();", true);
}
我还有另一个服务器端函数是这样的:
public void Delete()
{
//Delete Code
}
点击按钮后,现在会转到这个javascript
函数:
现在我想做的是,在服务器端调用 Delete()
函数。这是我的 javascript 函数,到目前为止我已经尝试过
function (isConfirm)
{
if (isConfirm)
{
//CALL DELETE FUNCTION HERE Delete();
swal("Deleted!", "Your imaginary file has been deleted.", "success");
}
else
{
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
如何调用该服务器端函数?有什么想法吗?
我会向页面添加 ScriptManager 并启用 PageMethods;
代码在:
在 ASPX 中:
<asp:ScriptManager runat="server" EnablePageMethods="true" />
在Javascript中:
<script>
PageMethods.Delete();
</script>
在ASPX.cs中:
[System.Web.Services.WebMethod]
public static void Delete()
{
//Delete Code
}
您可以通过两种方式实现这一目标。 Ajax/Web JS中的服务或触发按钮点击。最简单的是触发按钮点击。使用以下代码。
aspx:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" ClientIDMode="Static" style="display:none;"/>
c#:
protected void Button1_Click(object sender, EventArgs e)
{
Delete();
}
JS:
document.getElementById('Button1').click();
// jquery
$("#Button1").click();
但是如果您不想回发您的页面,请使用 Ajax。 在Ajax简单,你需要添加一个网页说data.aspx,在后端class of data.aspx你将添加以下c#
Ajax。 C#
[WebMethod]
public static int DoSomething(int Id)
{
return 1;
}
现在您可以从 JS 中调用它了:
$.ajax({
url: APP_PAGE_RELATIVE_PATH + "Data.aspx/DoSomething",
data: "{'Id':5}",
type: "POST",
cache: false,
headers: { "cache-control": "no-cache" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Do Something
},
error: function (xhr, status, error) {
//DebugAlert("Error: " + xhr.responseText);
}
});