Create Alert on server-side using RadWindowManager without rasing the Page_Load event
Environment
Product | RadWindowManager for ASP.NET AJAX |
Language | C#, VB, JavaScript |
Description
Creating an alert on the server-side is usually done by calling the RadWindowManager1.RadAlert() method on server-side
private void RadButton1_Click(object sender, EventArgs e)
{
RadWindowManager1.RadAlert();
}
Private Sub RadButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
RadWindowManager1.RadAlert()
End Sub
However, this approach will raise the Page_Load event. In some cases, there might be a specific logic in the Page Load event, which, if executed twice may produce a different outcome.
Solution
Instead of calling the Server-Side "RadAlert()", register the Client-Side "radalert()" method as a StartupScript.
Make sure to have a RadWindowManager on the Page. The Server-Side "RadAlert()" and Client-Side "radalert()" methods are part of the RadWindowManager APIs.
<telerik:RadWindowManager ID="RadWindowManager1" runat="server"></telerik:RadWindowManager>
Register the StartupScript in the Button Click
private void RadButton1_Click(object sender, EventArgs e)
{
string script = @"function myFunction() {
Sys.Application.remove_load(myFunction);
var oAlert = radalert('Alert Message', null, null, 'Window Title', AlertCallBackFn, null);
};
Sys.Application.add_load(myFunction);";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "name", script, true);
}
Private Sub RadButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim script As String = "function myFunction() {
Sys.Application.remove_load(myFunction);
var oAlert = radalert('Alert Message', null, null, 'Window Title', AlertCallBackFn, null);
};
Sys.Application.add_load(myFunction);"
ScriptManager.RegisterStartupScript(Me.Page, Me.Page.[GetType](), "name", script, True)
End Sub
The CallBack function if defined in the StartUpScript
function AlertCallBackFn(arg) {
/* Optional: Alert again to display the radalert() callback response */
radalert("This is the CallBack response from the RadAlert.");
}