Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ScriptManager in class file?

I Have a common method that displays alert message using page.clientScript. But later on i added update panel. Now this piece of code is not working. So I need to call there scriptmanager, but i get some error message that it is accessible there. Below is my ShowMessage method of common.cs file

 private static void ShowMessage(Page currentPage, string message)
        {
            var sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(message);
            sb.Append("');");
            currentPage.ClientScript.RegisterClientScriptBlock(typeof(Common), "showalert", sb.ToString(), true);
        }

So how do I use this method under update panel


2 Answers

Make use of : ScriptManager.RegisterClientScriptBlock Method

ScriptManager.RegisterClientScriptBlock(
            this,
            typeof(Page),
            "TScript",
            script,
            true);
like image 110
Pranay Rana Avatar answered Jan 27 '26 19:01

Pranay Rana


const string scriptString = "<script type='text/javascript'> alert('message');</script>";
                ClientScriptManager script = Page.ClientScript;
                script.RegisterClientScriptBlock(GetType(), "randomName", scriptString);
like image 42
Kimtho6 Avatar answered Jan 27 '26 20:01

Kimtho6