Response.Write脚本警告框在asp.net Web表单中无法正常工作

时间:2016-12-14 18:41:46

标签: c# asp.net webforms response.write

我正在使用Visual Studio在C#中创建一个asp.net Web应用程序。

在一篇旧帖子中我询问了asp.net中的弹出消息框,并被告知webforms没有它们。我通过使用:Response.Write(“alert('Hello world');”);给了我快速修复。这在打开新页面时仅使用一次作为登录确认就足够了。我遇到的问题是我在按钮单击事件中有一堆if语句,只有第一个脚本警报有效(if(poundRadBtn.Checked))。有人能够向我解释为什么其他三个不起作用,是否有修复或替代?提前谢谢!

protected void submitPayBtn_Click(object sender, EventArgs e)
    {
        if (poundRadBtn.Checked)
            if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
            {
                Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
            }
        else if (usdolRadBtn.Checked)
            if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
            {
                Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
            }
        else if (ozdolRadBtn.Checked)
            if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
            {
                Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
            }
        else if (ozdolRadBtn.Checked)
            if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
            {
                Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
            }
    }

1 个答案:

答案 0 :(得分:0)

编译器会将您的else if语句读为嵌套。它看起来像这样:

            if (poundRadBtn.Checked)
                if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
                {
                    Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                }
                else if (usdolRadBtn.Checked)
                    if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
                    {
                        Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                    }
                    else if (ozdolRadBtn.Checked)
                        if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
                        {
                            Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                        }
                        else if (ozdolRadBtn.Checked)
                            if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
                            {
                                Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                            }

这就是为什么如果第一个如果失败则没有其他if语句被捕获。

用空的else语句重写它是解决这个问题的一种方法:

                if (poundRadBtn.Checked)
                if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" ||
                    cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" ||
                    billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" ||
                    billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
                {
                    Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                }
                else
                { }
            else if (usdolRadBtn.Checked)
                    if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
                    {
                        Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                    }
                else { }
            else if (ozdolRadBtn.Checked)
                if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" ||
                    cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" ||
                    billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" ||
                    billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
                {
                    Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                }
                else
                { }
            else if (ozdolRadBtn.Checked)
                if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" ||
                    cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" ||
                    billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" ||
                    billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
                {
                    Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
                }
                else
                { }
            }