Tuesday, September 30, 2008
by
Vladimir Enchev
|
I've made small example how to use RadGrid for ASP.NET AJAX in Microsoft ASP.NET MVC:
Â
The key here is to inherit from RadGrid and call explicitly desired grid commands. Let's say you want to edit particular record:
1) Create a template column and add this to the ItemTemplate:
<%# Html.ActionLink("Edit", "RadGridCommand", new { ControlID = MyGrid1.ID, CommandName = "Edit", CommandArgument = Container.ItemIndexHierarchical }) %>
Â
2) Now handle this in your controller:
public ActionResult RadGridCommand(string ControlID, string CommandName, string CommandArgument)
{
   ViewData["ControlID"] = ControlID;
   ViewData["CommandName"]= CommandName;
   ViewData["CommandArgument"] = CommandArgument;
   return View("Index");
}
Â
3) Override OnPreRender in inherited class and call explicitly the grid RaisePostBackEvent() method:
protected override void OnPreRender(EventArgs e)
{
   base.OnPreRender(e);
   if (ViewData["ControlID"] != null && ViewData["ControlID"].ToString() == ID &&
       ViewData["CommandName"] != null)
   {
       RaisePostBackEvent(String.Format("FireCommand:{0};{1};{2}", MasterTableView.UniqueID, (string)ViewData["CommandName"], (string)ViewData["CommandArgument"]));
   }
}
For other useful techniques please refer to the attached source code.
Enjoy!
[Download]