ClientScript.RegisterStartupScript Problem

Last post 11-12-2009 2:04 AM by feroze1. 6 replies.

Sort Posts:

  • ClientScript.RegisterStartupScript Problem

    11-08-2009, 12:22 PM

    Hello,

    I am using the following code to execute javascript from codebehind

    string strJscript = "<script language='javascript'>setName('div1');</script>";
    ClientScript.RegisterStartupScript(typeof(string), "strJscript1", strJscript);


    Which is working fine, when i open the page first time.

    But when i click on submit button, this script executes but doesnt display any result.

    Help me pls.


  • Re: ClientScript.RegisterStartupScript Problem

    11-08-2009, 4:51 PM

    if you register the script on postback, and don't do any redirects or server transfers, it should work fine.

    you can view source after clicking the submit button and the page re-renders to see if it appeared as desired.


    bruce (sqlwork.com)
  • Re: ClientScript.RegisterStartupScript Problem

    11-08-2009, 10:40 PM

    Also please check script already registered or not else it will stop working.

  • Re: ClientScript.RegisterStartupScript Problem

    11-09-2009, 12:36 AM

    Well I have the above code on page_load, it works fine when page is loaded

    Hence when I click submit, the same code executes again but doesnt display the result.

    if I use like If(!IspostBack) , even then i dont get the desired result.

    Is there any other way to execute javascript function from codebehind?


  • Re: ClientScript.RegisterStartupScript Problem

    11-12-2009, 1:28 AM
    Answer

    Hi,

    You could use RegisterStartupScript() or RegisterClientScriptBlock() to execute javascript function from codebehind, and there also have some different. For more details, please refer:http://msdn.microsoft.com/en-us/library/aa479390.aspx

    The another way is like that:

    <%@ Page Language="C#" %>
    
    <script runat="server"> 
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Attributes.Add("onclick", 
               "javascript:alert('ALERT ALERT!!!')");
        }
    </script>
    
    <html  >
    <head runat="server">
        <title>Using JavaScript</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button id="Button1" runat="server" Font-Bold="True" 
             Font-Names="Verdana" Font-Size="Larger" 
             Text="Click Me!"></asp:Button>
        </div>
        </form>
    </body>
    </html>


     

    Roy Tian.
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: ClientScript.RegisterStartupScript Problem

    11-12-2009, 1:40 AM
    • Participant
      1,869 point Participant
    • nimish_soft
    • Member since 10-22-2009, 1:23 PM
    • India
    • Posts 372

    Song-Tian - MSFT:

    Hi,

    You could use RegisterStartupScript() or RegisterClientScriptBlock() to execute javascript function from codebehind, and there also have some different. For more details, please refer:http://msdn.microsoft.com/en-us/library/aa479390.aspx

    The another way is like that:

    1. <%@ Page Language="C#" %>  
    2.   
    3. <script runat="server">   
    4.     protected void Page_Load(object sender, EventArgs e)  
    5.     {  
    6.         Button1.Attributes.Add("onclick",   
    7.            "javascript:alert('ALERT ALERT!!!')");  
    8.     }  
    9. </script>  
    10.   
    11. <html  >  
    12. <head runat="server">  
    13.     <title>Using JavaScript</title>  
    14. </head>  
    15. <body>  
    16.     <form id="form1" runat="server">  
    17.     <div>  
    18.         <asp:Button id="Button1" runat="server" Font-Bold="True"   
    19.          Font-Names="Verdana" Font-Size="Larger"   
    20.          Text="Click Me!"></asp:Button>  
    21.     </div>  
    22.     </form>  
    23. </body>  
    24. </html>  


     


    i use the same way to add a js on asp:button

    - Nimish Garg
    Software Developer
    IndiaMART InterMESH Limited, Noida

    Blog: http://nimishgarg.blogspot.com/

    Plz do click "Mark as Answer" on the post that helped you. This will also give you point and help readers to know which post solved your issue and make their search easy.
  • Re: ClientScript.RegisterStartupScript Problem

    11-12-2009, 2:04 AM
    • Member
      712 point Member
    • feroze1
    • Member since 03-30-2007, 9:42 AM
    • Lahore
    • Posts 189

    try this i will help you....

    private string getjQueryCode(string jsCodetoRun)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("$(document).ready(function() {");
            sb.AppendLine(jsCodetoRun);
            sb.AppendLine(" });");
    
            return sb.ToString();
        }
    
        private void runjQueryCode(string jsCodetoRun)
        {
    
            ScriptManager requestSM = ScriptManager.GetCurrent(this);
            if (requestSM != null && requestSM.IsInAsyncPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(this,
                                                        typeof(Page),
                                                        Guid.NewGuid().ToString(),
                                                        getjQueryCode(jsCodetoRun),
                                                        true);
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(typeof(Page),
                                                       Guid.NewGuid().ToString(),
                                                       getjQueryCode(jsCodetoRun),
                                                       true);
            }
        }


    call runjQueryCode funtion where you want with param


    runjQueryCode("setName('div1');");


    * you must add JQuery reference


    Please "Mark As Answer" if helpfull..
    Feroze Ahmed
Page 1 of 1 (7 items)