Firing Javascript function After Postback

5 posts, 0 answers
  1. LEVEFAUDE
    LEVEFAUDE avatar
    38 posts
    Member since:
    Apr 2011

    Posted 20 Oct 2011 Link to this post

    Hello

    I want to execute a javascript function, after a button click

    It's easy to execute javascript and after postback  on the same button, but not postback and after javascript !

    Please help me !

    Anne

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Oct 2011 Link to this post

    Hello Levefaude,

    You can try the following approaches to execute javascript after a button click.

    1. C#:

    protected void btn_Click(object sender, EventArgs e)
    {
      string radconfirmscript = "<script language='javascript'>function f(){test(); Sys.Application.remove_load(f) ;}; Sys.Application.add_load (f);</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "radconfirm", radconfirmscript);
    }

    JS:
    <script type="text/javascript">
    function test()
    {
     alert("Hi");
    }
    </script>

    2. C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
       Response.Write("<script language=JavaScript> alert('Hello'); </script>");
    }

    3. C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
       string s = "<script language=JavaScript>alert('Hello');</script>";
       Page.ClientScript.RegisterStartupScript(this.GetType(), "test", s);
    }

    Thanks,
    Shinu.

  3. DevCraft Release
  4. LEVEFAUDE
    LEVEFAUDE avatar
    38 posts
    Member since:
    Apr 2011

    Posted 21 Oct 2011 Link to this post

    Thank you, but it does not work with an a little bit complex procedure

    I am surprised that telerik is not planned an afterpostback feature for the controls!!!!
    Anthem, free library, had planned everything!

    Thank you and have a good day

    Anne

  5. Slav
    Admin
    Slav avatar
    1236 posts
    Member since:
    Sep 2012

    Posted 25 Oct 2011 Link to this post

    Hi Levefaude,

    I would also recommend using the RegisterStartupScript method of the ScriptManager control in order to register a client script that will be executed after the postback. This method can be used as shown below:

    Page Markup:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="Scriptmanager1" runat="server" />
        <div>
            <telerik:RadButton runat="server" ID="RadButton" Text="Click"
                onclick="RadButton_Click"></telerik:RadButton>
        </div>
        </form>
    </body>
    </html>

    Code-Behind:
    protected void RadButton_Click(object sender, EventArgs e)
    {
        string script = "alert('Hello');";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "testScript", script, true);
    }

    The afterpostback feature that you are suggesting is indeed interesting. It will be brought to the attention of our developers and it will be considered for implementation, depending on its popularity. I have logged this request as a PITS Issue. You can check its status by following this link: Public URL.

    Feel free to contact us again if you encounter more difficulties.

    All the best,
    Slav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now

  6. LEVEFAUDE
    LEVEFAUDE avatar
    38 posts
    Member since:
    Apr 2011

    Posted 27 Oct 2011 Link to this post

    Yesss ! Thank you, that works very well

    Very good day, rainy in France

    Anne

Back to Top
DevCraft Release