如何使用javascript调用在asp.net功能? [英] How to invoke function in asp.net using javascript?
问题描述
我使用jQuery插件tablednd
I'm using the jquery plugin tablednd
$(document).ready(function () {
$('#mygridview').tableDnD();
})
,使我在GridView控件拖动我行。
当我拖着一排我要调用一个函数在asp.net中保存行的位置。但我找不到GridView的一个合适的事件,只有反作用于点击一行。
to make my rows in my gridview draggable. When I've dragged a row I want to invoke a function in asp.net to save the positions of the rows. But I can't find a proper event on gridview that only reacts on clicking on a row.
有在tableDnd一个onDrop方法。
There is a onDrop method in tableDnd.
$(document).ready(function () {
$('#mygridview').tableDnD({
onDrop: function(table, row) {
});
})
但我怎么可能从那里调用asp.net的方法?我读过一些关于Ajax的文章,但我不明白这一点。
But how could I from there invoke a asp.net method? I've read some about ajax post but I don't understand that.
和我也看了关于使用 PageMethods
但它并没有调用我的功能。
And I also read about using PageMethods
but it didn't invoke my function.
问题是我怎么能写的东西,在我的数据库中的表执行更新?
The question is how can I write something that executes an update on a table in my database?
更新:
我解决它使用IPostBackEventHandler方法。
I solved it using the IPostBackEventHandler method.
我不得不延长我的两个用户控制和我的IPostBackEventHandler页
然后添加两个用户控件和页面的公共无效RaisePostBackEvent(字符串eventArgument)函数。最后:
I had to extend both my user control and my page with IPostBackEventHandler and then added the public void RaisePostBackEvent(string eventArgument) function on both the user control and page. And finally:
onDrop: function (table, row) {
__doPostBack('<%= this.UniqueID %>', 'Select');
}
如果有人onDrop了问题,解决的办法是你必须给ID为每一行是这样的:
If someone got problem with onDrop, the solution is you have to give ID to each row like this:
var i = 0;
$("#<%= gridviewID.ClientID %>").find('tr').each(function () {
$(this).attr('id', i++);
});
在tablednd的启动以上。
above the initiation of the tablednd.
谢谢!
推荐答案
有两种方式一般情况下,使用一个AJAX和第二,使用回发。
There are two ways in general, one using AJAX and second, using postbacks.
在AJAX方式:
-
添加的ScriptManager到网页上,类似这样的:
Add ScriptManager to the page, like this:
&LT; ASP:的ScriptManager =服务器
ID =ScriptManager1
的EnablePageMethods =真正的
的EnablePartialRendering =真
/&GT;
请在服务器端的方法被称为公共静态。另外随着System.Web.Services.WebMethod属性标记它,就像这样:
Make the server side method to be called as public static. Also mark it with the System.Web.Services.WebMethod attribute, like this:
[的WebMethod]
公共静态字符串DoServerStuff(字符串参数)
{
返回参数传递:+参数;
}
从客户端调用它,通过PageMethods类,如下:
Call it from the client side, via the PageMethods class, like this:
函数doClientStuff(){
var result = PageMethods.DoServerStuff('test parameter');
alert(result);
}
有关使用jQuery检查做同样的事情的这个的出
For doing the same thing using jQuery check this out
的回传方式:
- 请在页面(包含要调用的方法)实现IPostBackEventHandler接口
-
调用__doPostBack方法在客户端,就像这样:
- Make the page (that contains the method to be called) implement the IPostBackEventHandler interface
Call the __doPostback method on the client side, like this:
函数doClientStuff(){
var result = __doPostBack('<%= this.UniqueID %>', 'Select');
}
实施页
更多关于从客户端的这里
More on the raising postbacks from the client side here
这篇关于如何使用javascript调用在asp.net功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!