How to call window.alert(“message”); from C#?

后端 未结 10 2319
春和景丽
春和景丽 2021-02-07 05:18

I have my own exception based on some condition and want to raise an alert when control comes in this catch block

catch (ApplicationException ex)
{
    //want t         


        
10条回答
  •  失恋的感觉
    2021-02-07 05:51

    You can use the following extension method from any web page or nested user control:

    static class Extensions
    {
        public static void ShowAlert(this Control control, string message)
        {
            if (!control.Page.ClientScript.IsClientScriptBlockRegistered("PopupScript"))
            {
                var script = String.Format("", message);
                control.Page.ClientScript.RegisterClientScriptBlock(control.Page.GetType(), "PopupScript", script);
            }
        }
    }
    

    like this:

    class YourPage : Page
    {
        private void YourMethod()
        {
            try
            {
                // do stuff
            }
            catch(Exception ex)
            {
                this.ShowAlert(ex.Message);
            }
        }
    }
    

提交回复
热议问题