Ebikeneser
Ebikeneser

Reputation: 2364

Alert message box

I have been using label text to show different status in an asp.net website.

e.g. "Profile Updates", "Invalid Character" etc, however would prefer to have a pop up message box instead.

This is what I have tried -

string alert = ws.webMethod(TextBox1.Text);
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert(" + alert + ")</SCRIPT>");

However when this is fired, the screen in IE just seems to get bigger, and no message box is presented.

How can I achieve this?

Upvotes: 4

Views: 15384

Answers (4)

Adil
Adil

Reputation: 148120

Use ClientScriptManager.RegisterClientScriptBlock instead of Response.Write

string alert = ws.webMethod(TextBox1.Text);
string script = "<SCRIPT LANGUAGE='JavaScript'>alert(" + alert + ")</SCRIPT>" 
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptBlockName", script );

Upvotes: 3

Pouya
Pouya

Reputation: 1918

you can use alert or you can use jquery plugin like this and this. if use jquery plugin you can custom message box like change backColor or...

Upvotes: 1

Ankit
Ankit

Reputation: 690

I have used this in my project, works fine for me

 ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "MessageBox", "alert('message')", true);

Upvotes: 2

paul
paul

Reputation: 22001

I would not advise doing this, it looks like you are creating a modal dialog on page load, meaning that your page cannot continue processing until a user has clicked OK.

That said, however, your problem is probably a lack of quotes around the alerted text:

System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('" + alert + "')</SCRIPT>");

Upvotes: 4

Related Questions