how to show message box in asp.net

匿名 (未验证) 提交于 2019-12-03 00:53:01

问题:

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

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

However if instead I do this:

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

The message box doesn't get shown.

Why is this and how can I fix it?

回答1:

By doing a Response.Redirect right after you're actually sending a 302 redirect to the client, so the alert is never actually being rendered in the user's browser. Instead, try something like this

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


回答2:

Your Response.Redirect call will fire and redirect the browser before the alert has been shown to the user.



回答3:

The JavaScript written to the Response is not running because of the following line:

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

This is redirecting the response from the current page to Admin.aspx. Any further content written to the response will not be rendered and executed because the browser is being instructed to navigate to the new location.



回答4:

use

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

hope that helped



回答5:

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

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


回答6:

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

EDIT:

Generally, we use to show a message box in asp.net by writing a common method with message as parameter to it like follows.

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) {     } } 


回答7:

I also used this way but code project in better way offered

 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;      } 


回答8:

This is late but this is the right answer as there are no answers below that you have checked, you cannot use response redirect as it will redirect the page before you can show the message box in which is appended in the end of the page. you should use the window location method:

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


回答9:

You can use this code in your .cs page to show message box if you are using 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 show message box if you are not using update panel

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


回答10:

This is an example program in which you can call your own message into message box .It's work great for you . You must 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);     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!