How to show message box in asp.net

I am using C # to create a website and I am trying to show a message box. I am trying to use JavaScript for this situation and it starts if I do the following:

Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>"); 

However, if I do this:

 Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>"); Response.Redirect("~/admin.aspx"); 

The message field is not displayed.

Why is this and how can I fix it?

+4
source share
10 answers

By executing Response.Redirect immediately after the 302 redirect is actually sent to the client, so the warning will never be displayed in the user's browser. Instead, try something like this

  Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful');document.location='" + ResolveClientUrl("~/admin.aspx") +"';</script>"); 
+6
source

Your call to Response.Redirect will launch and redirect the browser before the alert user is displayed.

+2
source

The JavaScript written in the response does not work due to the following line:

 Response.Redirect("~/admin.aspx"); 

This redirects the response from the current page to admin.aspx. Any other content recorded in response will not be displayed and executed because the browser is prompted to move to a new location.

+1
source
 "<script language='javascript'>alert(\"" + "Your Message" + "\")</script>"; 

EDIT:

Typically, we use to show the message box in asp.net by writing a generic method with the message as a parameter, as shown below.

 public void UserMsgBox(string sMsg) { try { StringBuilder sb = new StringBuilder(); System.Web.UI.Control oFormObject = null; sMsg = sMsg.Replace("'", "\\'"); sMsg = sMsg.Replace(Strings.Chr(34), "\\" + Strings.Chr(34)); sMsg = sMsg.Replace(Constants.vbCrLf, "\\n"); sMsg = "<script language='javascript'>alert(\"" + sMsg + "\")</script>"; sb = new StringBuilder(); sb.Append(sMsg); foreach (System.Web.UI.Control oFormObject_loopVariable in this.Controls) { oFormObject = oFormObject_loopVariable; if (oFormObject is HtmlForm) { break; // TODO: might not be correct. Was : Exit For } } oFormObject.Controls.AddAt(oFormObject.Controls.Count, new LiteralControl(sb.ToString())); } catch (Exception ex) { } } 
+1
source

use

 ClientScript.RegisterStartupScript(Me.GetType(), "fnCall", "<script language='javascript'>alert('Login Successful! ');</script>") Response.Redirect("~/admin.aspx"); 

hope this helped

+1
source

Also to register a Script from UpdatePanel you should use this:

 ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), script, true); 
+1
source

I also used this method, but the code project suggested better

  protected void Button1_Click(object sender, EventArgs e) { ClientScriptManager CSM = Page.ClientScript; if (!ReturnValue()) { string strconfirm = "<script>if(!window.confirm('Are you sure?')){window.location.href='Default.aspx'}</script>"; CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false); } } bool ReturnValue() { return false; } 
+1
source

Late, but this is the correct answer, since there are no answers below that you checked, you cannot use the redirection of the answer, because it redirects the page before you can show the message box to which you added at the end of the page you should use the location method window:

 Response.Write("<script language='javascript'>window.alert('Login Successful.');window.location='admin.aspx';</script>"); 
+1
source

You can use this code on the .cs page to display a message box if you use the update panel

 ScriptManager.RegisterStartupScript(this, this.GetType(), "myalert", "alert('Type here your message...')", true); 

or

 ScriptManager.RegisterStartupScript(this.ControlID, this.GetType(), "myalert", "alert('Type here your message')", true); 

or this code to display a message box if you are not using the update panel

 ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Type here your message')", true); 
0
source

This is an example program in which you can call your message in the message field. This works great for you. You should try my example

 protected void btnSubmit_Click(object sender, EventArgs e) { try { if (RadioButtonList1.SelectedItem.ToString() == "Sum Of Digit") { string a = tbInput.Text; int sum = 0; for (int i = 0; i < a.Length; i++) { sum = sum + Convert.ToInt32(a[i].ToString()); } lblResult.Text = sum.ToString(); } else if (RadioButtonList1.SelectedItem.ToString() == "InterChange Number") { string interchange = tbInput.Text; string result = ""; int condition = Convert.ToInt32(interchange.ToString()); if (condition <= 99) { result = interchange[interchange.Length - 1].ToString() + interchange[0].ToString(); lblResult.Text = result.ToString(); } else { MyMessageBox("Number Must Be Less Than 99"); } } else if (RadioButtonList1.SelectedItem.ToString() == "Sum Of First n last Digit") { //example } else { MyMessageBox("Not Found"); } } catch (Exception ex) { MyMessageBox(ex.ToString()); } } public void MyMessageBox(string text) { string script = "alert('"+text+"');"; ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScripts", script, true); } } 
-1
source

All Articles