Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, December 14, 2015

how to show message box in asp.net

how to show message box in asp.net


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("");    

However if instead I do this:

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

The message box doesn't get shown.

Why is this and how can I fix it?

Answer by ipr101 for how to show message box in asp.net


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

Answer by jdavies for how to show message box in asp.net


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.

Answer by Sai Kalyan Kumar Akshinthala for how to show message box in asp.net


"";  

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 = "";          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) {      }  }  

Answer by reven for how to show message box in asp.net


use

ClientScript.RegisterStartupScript(Me.GetType(), "fnCall", "")     Response.Redirect("~/admin.aspx");   

hope that helped

Answer by redec for how to show message box in asp.net


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("");  

Answer by marianosz for how to show message box in asp.net


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

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

Answer by Ali Sarshogh for how to show message box in asp.net


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 = "";          CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false);      }  }       bool ReturnValue()       {         return false;       }  

Answer by User 6675636b20796f7521 for how to show message box in asp.net


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("");  

Answer by Saqib Aslam Ganatra for how to show message box in asp.net


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

Answer by Amit Kumawat for how to show message box in asp.net


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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.