在jQuery中的onclick方法调用服务器端方法 [英] Call server-side method in the onclick method in jQuery
问题描述
我在后端使用C#编码的一种方法。现在,我想打电话给它使用jQuery这是在设计部分按钮onclick事件。
搜索结果
请帮我解决这个问题。搜索结果
看看下面code为例,搜索
.aspx页面中搜索
$(#提交按钮)。点击(函数(){
//调用方法BindData();
});
结果
.aspx.cs页结果
公共无效BindData()
{
// code绑定DataList控件
}
如果我正确理解你想要的东西,像这样
$(#提交按钮)。点击(函数(){
$阿贾克斯({
键入:POST,
网址:的Index.aspx / BindData
的contentType:应用/ JSON的;字符集= UTF-8,
数据:JSON.stringify({}),
数据类型:JSON
});
});
和让你的方法静态的,并添加属性<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute%28v=vs.110%29.aspx\"相对=nofollow>的WebMethod 这样的:
[的WebMethod]
公共静态无效BindData()
{
// code
}
但你将无法更改服务器控件的状态
I have one method in backend coding using c#. Now, I want to call it on onClick event of button using jQuery which is in design part.
Please help me to solve the problem.
Check out below code for example.
.aspx Page
$("#SubmitButton").click(function() {
// Call method BindData();
});
.aspx.cs Page
public void BindData()
{
// Code to bind datalist
}
if i understand right you want something like this
$("#SubmitButton").click(function() {
$.ajax({
type: "POST",
url: "index.aspx/BindData",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ }),
dataType: "json",
});
});
and make your method static and add attribute WebMethod like:
[WebMethod]
public static void BindData()
{
// Code
}
but you will not be able to change the state of server controls
这篇关于在jQuery中的onclick方法调用服务器端方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!