简体   繁体   English

我们如何对诸如RowCommand,RowDeleting之类的gridview事件进行ajax调用

[英]How can we do ajax calls for gridview events like RowCommand, RowDeleting

I'm working on a asp.net web application where all events are generated in .ASPX and code behind code is also written in C# and both are working fine. 我正在一个asp.net Web应用程序上,其中所有事件都是在.ASPX中生成的,并且代码背后的代码也都是用C#编写的,两者都可以正常工作。 But now I want to call c# server side events using AJAX Calls. 但是现在我想使用AJAX调用来调用c#服务器端事件。

I know how to create and call webmethod using AJAX calls but don't know how to handle (object sender, GridViewRowEventArgs e) these parameters through AJAX. 我知道如何使用AJAX调用来创建和调用webmethod,但不知道如何通过AJAX处理(object sender, GridViewRowEventArgs e)这些参数。

Thanks in advance. 提前致谢。

ASPX Code: ASPX代码:

<asp:GridView ID="grd" runat="server"
 AutoGenerateColumns="false" 
 OnRowCommand="grd_RowCommand">
 <Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" runat="server" CommandName="AddRow" Text="AddRow">
 </asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

C# Code: C#代码:

protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddRow")
{
 //Do Something
}
}

Controls that don't normally postback to the server naturally use some ASP.NET JS methods to trigger the postback, which sets two hidden field values: __EVENTTARGET and __EVENTARGUMENT (see this post with some brief details on it in another context). 通常不回发到服务器的控件自然会使用某些ASP.NET JS方法来触发回发,这会设置两个隐藏字段值:__EVENTTARGET和__EVENTARGUMENT(请参阅此文章,并在其他上下文中有一些简短的详细信息 )。 These fields should contain the relevant object/event arg data. 这些字段应包含相关的对象/事件arg数据。 So your solution would likely need to be able to work with that, invoke the page request, and parse out the response to replace the grid, and maybe that will work. 因此,您的解决方案可能需要能够使用该功能,调用页面请求并解析出响应以替换网格,这也许会起作用。 However, I think that might have issues. 但是,我认为这可能会有问题。

Since you are using web forms, I would recommend using an UpdatePanel preferrably, or consider using this technique to dynamically loading a grid from an AJAX response. 由于您使用的是Web表单,因此建议您最好使用UpdatePanel,或者考虑使用此技术从AJAX响应中动态加载网格。 Any eventing within the grid would need to be handled via JS though. 网格中的任何事件都需要通过JS处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM