RadControls for ASP.NET AJAX
By default the AJAX Panel ajaxifies all controls placed inside. If you want to exclude a control from ajaxifying you can use one of the following approaches:
ScriptManager RegisterPostBackControl method.
An easy solution can be implemented through the ScriptManager's RegisterPostBackControl method. An example follows:
CopyASPX
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
<asp:Button runat="server" ID="Button1" Text="Ajax" OnClick="Button_Click" />
<asp:Button runat="server" ID="Button2" Text="Postback" OnClick="Button_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
</telerik:RadAjaxPanel>
CopyC#
protected void Page_Load(object sender, EventArgs e)
{
RadScriptManager1.RegisterPostBackControl(Button2);
}
protected void Button_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
CopyVB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
RadScriptManager1.RegisterPostBackControl(Button2)
End Sub
Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = DateTime.Now.ToLongTimeString()
End Sub Note |
|---|
Note that this approach is not applicable when the page is ajaxified with RadAjaxManager. |
Disable AJAX via the OnRequestStart event (RadAjaxPanel, RadAjaxManager)
Use OnRequestStart client-side event handler to determine the AJAX initiator and disable AJAX for the current request. The event is fired on each request so on the next one the same check will be performed. A similar implementation is often used in case of exporting from AJAXified Grid:
Export RadGrid content to Excel/Word/CSV/PDF with Ajax enabled
Note |
|---|
Note that all controls added in the UpdatedControls collection in the AJAX Manager ajax settings would perform callback instead of postback. To exclude them from ajaxifying use the above approach.
|
Cancel the AJAX request on InitializeRequest event
This approach is suitable for canceling the ajax request in wide range of scenarios.
Unlike the OnReqestStart event, the InitializeRequest will be triggered for both Telerik (RadAjaxManager, RadAjaxPanel) and
ASP.NET (UpdatePanel) ajax controls.
CopyASPX
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initRequest);
function initRequest(sender, args)
{
if (args.get_postBackElement().id.indexOf("CONTROL_ID") != -1)
{
args.set_cancel(true); //stop async request
sender._form["__EVENTTARGET"].value = args.get_postBackElement().id.replace(/\_/g, "$");
sender._form["__EVENTARGUMENT"].value = "";
sender._form.submit();
return;
}
}
</script>
Please note that you have to replace the CONTROL_ID string with the actual name of the control that triggers the AJAX request.
Add post back function manually
-
Implement the realPostBack function into your aspx/ascx file
- Add a custom OnClick attribute to the control (Button in this case).
CopyASPX
<telerik:RadCodeBlock ID="codeblock1" runat="server">
<script type="text/javascript">
function realPostBack(eventTarget, eventArgument)
{
$find("<%= RadAjaxPanel1.ClientID %>").__doPostBack(eventTarget, eventArgument);
}
</script>
</telerik:RadCodeBlock>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" Text="PostBack"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" Text="Ajax"></asp:LinkButton>
</telerik:RadAjaxPanel>And in the code-behind (Page_Load event handler):
CopyC#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LinkButton1.Attributes.Add("onclick", string.Format("realPostBack(\"{0}\", \"\"); return false;", LinkButton1.UniqueID));
}
}
CopyVB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
LinkButton1.Attributes.Add("onclick", String.Format(, LinkButton1.UniqueID))
End If
End Sub
If you do not have a control that registers the __doPostBack function on the page you should add the following line in the Page_Load as well:
CopyC#
this.Page.ClientScript.GetPostBackEventReference(this, "");
CopyVB.NET
Me.Page.ClientScript.GetPostBackEventReference(Me, "")
This will come handy when you want a single control to perform post-back for your scenario or you want to upload files from control in RadAjaxPanel.
Exclude Dynamically Loaded Controls
In case you are loading the user control dynamically, the code if (!Page.IsPostBack) in the example above in its Page_Load event handler is never executed. You can check for the attached OnClick event instead of the IsPostBack. For example:
CopyC#
protected void Page_Load(object sender, EventArgs e)
{
bool isOnClickAttached = false;
IEnumerator keys = this.CheckBox1.Attributes.Keys.GetEnumerator();
while (keys.MoveNext())
{
if (keys.Current.Equals("onClick"))
{
isOnClickAttached = true;
break;
}
}
if (!isOnClickAttached)
this.CheckBox1.Attributes.Add("onClick", string.Format("realPostBack(\"{0}\", \"\"); return false;", this.CheckBox1.UniqueID));
}
CopyVB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim isOnClickAttached As Boolean = False
Dim keys As IEnumerator = Me.CheckBox1.Attributes.Keys.GetEnumerator
While keys.MoveNext
If keys.Current.Equals("onClick") Then
isOnClickAttached = True
End If
End While
If Not isOnClickAttached Then
Me.CheckBox1.Attributes.Add("onClick", String.Format("realPostBack(\""{0}\"", \""\""); return false;", Me.CheckBox1.UniqueID))
End If
End Sub
See Also