简体   繁体   中英

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. But now I want to call c# server side events using AJAX Calls.

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.

Thanks in advance.

ASPX Code:

<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:

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). These fields should contain the relevant object/event arg data. 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. Any eventing within the grid would need to be handled via JS though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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