Click here to Skip to main content
15,569,696 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi to all

i want to show alert in static function in web method.

here is my code. code is compied successfully but it now showing the alert. Please help !

    public  static  void insertvalues(string[] valu)
            score = int.Parse(valu[1]) + int.Parse(valu[3]) + int.Parse(valu[4]) + int.Parse(valu[5]) + int.Parse(valu[6]);
            string script ="<script type=\"text/javascript\">"+string.Format("alert('{0}');", score)+";</script>";;
            Page page = HttpContext.Current.CurrentHandler as Page;

            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true);

        catch (Exception ex)

            throw ex;

[edit]"Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Updated 4-Feb-12 1:13am
Martin Arapovic 4-Feb-12 6:21am    
Why you are doing that? That does not make any sense. Web method cant show alert. It must do some program logic and return some value if that is needed. :) Maybe I'm wrong but you need to change your concept. You need to return score value to the client and then he will do something with score value...
Sergey Alexandrovich Kryukov 4-Feb-12 18:09pm    
Sergey Alexandrovich Kryukov 4-Feb-12 18:09pm    
Static?! Why on Earth? You cannot just say "I want", you need to explain it.

1 solution

I'm not too surprised - I don't think you can display an alert from a static method, as it doesn't know which session to use: it's static, and doesn't have a Session.
I suspect that HttpContext.Current.CurrentHandler is returning null for just that reason.
Share this answer
balongi 4-Feb-12 6:17am    
so what the alternative. i have to do it with static

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900