4

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?

Pointy
  • 405,095
  • 59
  • 585
  • 614
Simon Huynh
  • 329
  • 1
  • 3
  • 12
  • 2
    I think your problem is that you do not understand how the client/server relationship works, and in particular how the server tells the client browser to execute redirects. I would suggest taking some time to study how the http protocol and web browsers in general work, then revisit your question. At that point, the problem will be obvious. – asawyer Oct 20 '11 at 12:55
  • Possible duplicate: http://stackoverflow.com/questions/6054281/javascript-alert-after-response-redirect – Polity Oct 20 '11 at 13:00
  • **You can try this:** [http://stackoverflow.com/questions/2199207/message-box-in-asp-net-web-application][1] [1]: http://stackoverflow.com/questions/2199207/message-box-in-asp-net-web-application – Fuat Jan 23 '14 at 12:28

10 Answers10

6

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>");
redec
  • 577
  • 2
  • 13
2

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

ipr101
  • 24,096
  • 8
  • 59
  • 61
1

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;
     }
Harry Sarshogh
  • 2,137
  • 3
  • 25
  • 48
1

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>");
Albert Laure
  • 1,702
  • 5
  • 20
  • 49
1

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.

jdavies
  • 12,784
  • 3
  • 33
  • 33
1
"<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) {
    }
}
1

use

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

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

hope that helped

reven
  • 223
  • 1
  • 3
  • 17
1

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

ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), script, true);
marianosz
  • 1,234
  • 10
  • 8
0

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);
Amit Kumawat
  • 574
  • 5
  • 11
-1

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);
    }
}
a5hk
  • 7,532
  • 3
  • 26
  • 40