简体   繁体   中英

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("<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?

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

在向用户显示alert之前,您的Response.Redirect呼叫将触发并重定向浏览器。

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

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

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.

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

use

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

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

hope that helped

另外,要从UpdatePanel注册脚本,您应该使用:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM