简体   繁体   English

弹出警报消息asp.net

[英]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. 我正在使用一个简单的asp.net条码应用程序,当某些验证不正确时,我需要显示一条弹出消息。

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. 我正在使用VS 2010,使用C#作为背后的代码构建Web应用程序asp.net。

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. 我尝试将IsPostBack设置为false,但这会使情况更糟。

thank you! 谢谢!

One way would be to use the CustomValidator Class ServerValidate event if your error message is always the same. 如果您的错误消息始终相同,则一种方法是使用CustomValidator类 ServerValidate事件。

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 http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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