RadGrid for ASP.NET

Making postback/AJAX request from javascript in user control/webform/MasterPage Send comments on this topic.
AJAX > What you should have in mind > Making postback/AJAX request from javascript in user control/webform/MasterPage

Glossary Item Box

Making postback/AJAX request from user control

Generally UserControl does not implement IPostBackEventHandler. That is why, to achieve your goal you need to implement the following interface when needed. Here is the bare bone logic:

C# Copy Code
public class WebUserControl1 : System.Web.UI.UserControl, IPostBackEventHandler
  {
     
public void RaisePostBackEvent(string eventArgument)
     {
     }
}
VB.NET Copy Code
Public Class WebUserControl1
    Inherits System.Web.UI.UserControl
    Implements IPostBackEventHandler

    Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent

    End Sub
End Class


If you want to raise post-back event using __doPostBack() or RadGridClientObject.AjaxRequest() (i.e. in Telerik RadGrid in AJAX mode) you should use for eventTarget the UserControl.UniqueID. Below are two basic examples:

Javascript [C#] Copy Code
window["<%= RadGrid1.ClientID %>"].AjaxRequest("<%= this.UniqueID %>", "MyArgument");

 

Javascript [VB.NET] Copy Code
window["<%= RadGrid1.ClientID %>"].AjaxRequest("<%= Me.UniqueID %>", "MyArgument")


Here is some info about these JavaScript functions: 

 

__doPostBack(eventTarget, eventArgument)

or

RadGridClientObject.AjaxRequest(eventTarget, eventArgument)

eventTarget The control which should raise postback event. If this control is directly on the HtmlForm, you can use the control's UniqueID or ClientID. Otherwise if your control is in a INamingContainer, you must use the control's UniqueID. For UserControl you should use always the user control's UniqueID
eventArgument This is optional argument for the event, used for passing some data on the server (which can be processed further)




Grid in Master page and RaisePostBackEvent handling

The approach is pretty much the same as with the user control - here is a sample scenario (note that we wired the OnRowDblClick client event of the grid to call the RadGridClientObject.AjaxRequest()method):


MasterPage.master

ASPX Copy Code
<head runat="server">
   
<title>Untitled Page</title>
<script type="text/javascript">
function RowDblClick(index)
{
   grid.AjaxRequest("
<%= this.UniqueID %>", "Rebind");
}
</script>
</
head>
<
body>
   
<form id="form1" runat="server">
       
<div>
           
<script type="text/javascript">
            
var grid;
    
            function GridCreated()
             {
               grid = this;    
            }         
           
</script>     
           
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
               
<rad:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1"
                
runat= "server" AutoGenerateColumns="True">
                   
<MasterTableView DataSourceID="AccessDataSource1">
                   
</MasterTableView>
                   
<ClientSettings>
                       
<ClientEvents OnGridCreated="GridCreated" OnRowDblClick="RowDblClick"></ClientEvents>
                   
</ClientSettings>
               
</rad:RadGrid>
   
<asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Nwind.mdb"
    
SelectCommand= "SELECT TOP 10 CustomerID, CompanyName, ContactName, ContactTitle,
    
Address, PostalCode FROM Customers" runat="server"></asp:AccessDataSource>
  
</asp:ContentPlaceHolder>

 

MasterPage.master.cs

C# Copy Code
public partial class MasterPage : System.Web.UI.MasterPage, IPostBackEventHandler
{
 
public void RaisePostBackEvent(string eventArgument)
 {
   
switch (eventArgument)
   {
       
case "Rebind":
       RadGrid1.Rebind();
      
break;
   }
 }
}
VB.NET Copy Code
Public Class MasterPage
  Inherits System.Web.UI.MasterPage
  Implements IPostBackEventHandler
  Public Sub RaisePostBackEvent(ByVal eventArgument As String)
    Select Case (eventArgument)
      Case "Rebind"
        RadGrid1.Rebind
     End Select
  End Sub
End Class


The same logic is applicable when your grid instance resides in an asp:Content control (under .NET 2.0) related to ContentPlaceHolder of MasterPage (asp: Content is INamingContainer). 

As before, you should pass the UniqueID of your grid object as first parameter for the RadGridClientObject.AjaxRequest() method.
In the example below we attach script (which calls the RadGridClientObject.AjaxRequest() method) to checkbox with ID chkIsClosed in the checkbox PreRender handler:

ASPX Copy Code
<script type="text/javascript">
function PerformRequest()
{
 window["
<%= RadGrid1.ClientID %>"].AjaxRequest("<%= RadGrid1.UniqueID %>", "MyArgument");
}
</script>
<
asp:Content ContentPlaceHolderID="ContentPlaceHolder1" ID="Holder1" runat="server">
   
<asp:CheckBox ID="chkIsClosed" runat="server" OnPreRender="chkIsClosed_PreRender" />
</
asp:Content>
<
asp:Content ContentPlaceHolderID = "ContentPlaceHolder2" ID="Holder2" runat="server">
   
<rad:radgrid id="RadGrid1" datasourceid="AccessDataSource1" runat="server"
   
EnableAJAX="true">
 
<MasterTableView DataSourceID="AccessDataSource1"></MasterTableView>
   
</rad:radgrid>
<asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Nwind.mdb"
 
SelectCommand="SELECT TOP 10 CustomerID, CompanyName, ContactName, ContactTitle,
 
Address, PostalCode FROM Customers" runat="server"></asp:AccessDataSource>
</asp:Content>

And in the code-behind:

C# Copy Code
protected void chkIsClosed_PreRender(object source, System.EventArgs e)
{
  (source
as CheckBox).Attributes("onclick") = "PerformRequest()";
}
VB.NET Copy Code
protected Sub chkIsClosed_PreRender(ByVal source as Object, ByVal e as System.EventArgs)

  CType(source, CheckBox).Attributes("onclick") = "PerformRequest()"

End Sub


Raising postback event from WebForm (overriding the default RaisePostBackEventHandler)

With a standard web form (aspx page) you can override the default RaisePostBackEventHandler and call its base.RaisePostBackEvent(source, eventArgument) method:

C# Copy Code
protected override void RaisePostBackEvent(IPostBackEventHandler source, String eventArgument)
{
 
base.RaisePostBackEvent(source, eventArgument);
  
switch(eventArgument)
  {
   
case "Rebind":
    RadGrid1.Rebind();
    
break;
  }
}
VB.NET Copy Code
Protected Overrides Sub RaisePostBackEvent(ByVal source As IPostBackEventHandler, ByVal eventArgument As String)
 MyBase.RaisePostBackEvent(source, eventArgument)
 Select Case (eventArgument)
  Case "Rebind"
   RadGrid1.Rebind
 End Select
End Sub


There are cases when you may want to update the information inside the grid by triggering a callback externally from other control on the page. Generally, this is a simple task with the integrated AJAX mechanism of Telerik RadGrid. You need to make a call to the RadGrid1.AjaxRequest() method (passing the necessary information as parameters to it) and then override the RaisePostBackEvent method to apply the changes. Here is a sample implementation:


ASPX/ASCX Copy Code
<rad:radgrid id="RadGrid1" runat="server"
       
EnableAJAX= "True" AllowSorting="True" AllowPaging="True">
</
rad:radgrid>
    
<br />
    
<input type=button value="Refresh" onclick='window["<%= RadGrid1.ClientID %>"].AjaxRequest( "<%= RadGrid1.UniqueID%>", "Rebind")' />
In the code-behind: 
VB.NET Copy Code
Protected Overrides Sub RaisePostBackEvent(ByVal source As IPostBackEventHandler, ByVal eventArgument As String)
    MyBase.RaisePostBackEvent(source, eventArgument)
    Select Case eventArgument
         Case "Rebind"
            RadGrid1.Rebind()
     End Select
End Sub
C# Copy Code
protected override void RaisePostBackEvent(IPostBackEventHandler source, String eventArgument)
{
 
base.RaisePostBackEvent(source, eventArgument);
  
switch(eventArgument)
  {
   
case "Rebind":
    RadGrid1.Rebind();
    
break;
  }
}