Vivekh
Vivekh

Reputation: 4259

Alert not working what's wrong in this

I write the following to alert but it is not displaying the alert box what's wrong in this

protected void Timer1_Tick(object sender, EventArgs e)
{
    string str = string.Empty;
    str = "Total Count: '" + click + "'";

    Response.Write("<Script>alert('" + str + "')</script>");
    click = 0;
}

Upvotes: 0

Views: 1066

Answers (4)

Dario Javier Lencina
Dario Javier Lencina

Reputation: 11

You have forgotten the '@' and the ';' characters in the alert function:

Replace this line:

Response.Write("<Script>alert('" + str + "')</script>");

with this:

Response.Write(@"<script type='text/javascript'>alert('" + str + "');</script>");

Upvotes: 1

Breno Mansur Rabelo
Breno Mansur Rabelo

Reputation: 153

As the signature of your method is Timer1_Tick, I assume you are using ScriptManager. The correct way to inject javascript with ScriptManager is:

ScriptManager.RegisterClientScriptBlock(this, typeof(this), "alert", "alert('" + str + "');", true)

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 161012

You have nested single quotes, try without:

    str = "Total Count: " + click;
    Response.Write("<Script>alert('" + str + "')</script>");

Upvotes: 2

NakedBrunch
NakedBrunch

Reputation: 49423

You should escape your apostrophes in the following line:

Change:

str = "Total Count: '" + click + "'";

To:

str = "Total Count: \'" + click + "\'";

Upvotes: 4

Related Questions