bizimunda
bizimunda

Reputation: 1035

Asp.net JavaScript OnClick not working

What I'm trying to do is, when I click on the button it should show an alert box. Here is the code.

protected void btnAdd_Click(object sender, EventArgs e)
{
    string script = "<script type=\"text/javascript\">alert('abc 1');
    </script>";
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert 1", 
    script);
    Response.Redirect("~/Index.aspx");

}

It is working perfectly but when I put this same code in another page it does not work.

what are the things I have to look into? any suggestion please?

Upvotes: 1

Views: 64

Answers (3)

vijay rangan
vijay rangan

Reputation: 1

Pass the last Parameter either True or False in ClientScript.RegisterScript and better you pass the alert message in this way.

Like This

 protected void btnAdd_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('abc 1');",true);
        Response.Redirect("~/Index.aspx");
    }

Upvotes: 0

KB1788
KB1788

Reputation: 194

May be you can try this - syntax may be wrong -

  protected void btnAdd_Click(object sender, EventArgs e)
{ 
    Response.Redirect("~/Index.aspx?Issuccess=1");

}

index.aspx page - 

protected void Page_Load()
{
     if(Request.QueryString["Issuccess"] != null && Convert.ToInt32(Request.QueryString["Issuccess"]) == 1)
     {
            string script = "<script type=\"text/javascript\">alert('abc 1');
            </script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert 1", 
            script);
     }
}

Upvotes: 1

vk_muse
vk_muse

Reputation: 614

move your js code to client side

<asp:Button ID="btnAdd" OnClientClick="alert('alert');" OnClick="btnAdd_Click" runat="server"/>

protected void btnAdd_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Index.aspx");

}

Upvotes: 1

Related Questions