BrOSs
BrOSs

Reputation: 929

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!

Upvotes: 0

Views: 28548

Answers (3)

Thomas C. G. de Vilhena
Thomas C. G. de Vilhena

Reputation: 14585

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.

Upvotes: 2

Amiram Korach
Amiram Korach

Reputation: 13286

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

Upvotes: 0

Roland
Roland

Reputation: 1010

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

Upvotes: 3

Related Questions