简体   繁体   中英

popup alert message asp.net

I'm working with a simple asp.net barcode application, I need to display a popup message when some validation is not the right one.

The thing is that my message is working only after I push the "submit" button two times. The first time just the page is reload, and if I push the button again, the popup do appear!

EDIT: I just forget to add some details. I'm using VS 2010, building a Web Application asp.net with C# as code behind.

public partial class Barcode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            Validate();

            if (IsValid)
            // 
            {
                string kemet = kemetTextBox.Text;
                string sud = sudTextBox.Text;

                if (kemet.Length == 14 && sud.Length == 28) // SOME VALIDATION CONTROL
                {

                    if (kemet.Substring(1) == sud.Substring(0, 13) && kemet != "" && sud != "")
                    {

                        //resultLabel.Text = "HIGH VOLUME<br/>";
                        redImage.Visible = false;
                        greenImage.Visible = true;

                    }
                    if (kemet.Substring(1) != sud.Substring(0, 13) && kemet != null && sud != null)
                    {

                        //  resultLabel.Text = "LOW VOLUME<br/>" + kemetEd + sudEd;
                        greenImage.Visible = false;
                        redImage.Visible = true;
                    }
                }
                else
                    Button1.Attributes.Add("onClick", "javascript:alert('Message Here');"); // HERE WOULD BE THE ERROR MSG 

I try to make the IsPostBack false, but that just made it worse.

thank you!

One way would be to use the CustomValidator Class ServerValidate event if your error message is always the same.

If not, use something like this:

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), Guid.NewGuid().ToString(), "alert('my message')", true);

Try replacing this:

Button1.Attributes.Add("onClick", "javascript:alert('Message Here');");

By this:

RegisterDOMReadyScript("alert message", "alert('Message Here');");

Which uses the following helper methods:

public void RegisterDOMReadyScript(string key, string script)
{
    string enclosed = EncloseOnDOMReadyEvent(script);
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), key, enclosed, true);
}

private string EncloseOnDOMReadyEvent(string str)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("function r(f){/in/.test(document.readyState)?setTimeout('r('+f+')',9):f()} r(function(){")
        .Append(str)
        .Append("});");
    return sb.ToString();
}

This will make sure your message will only be displayed after the document is ready , preventing ugly formatting issues.

You assign the error message only to the button click event that did not happen yet, so only after the page is loaded and the button is clicked again you see the error.

In order to do that you need to register the script in the page:

ClientScript.RegisterStartupScript(Page.GetType(), "SomeKey", "alert('Message Here');", true);

http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

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