0

I am in the process of creating an asp.net web application in C# using Visual Studio.

In an old post I was asking about popup message boxes in asp.net and was told webforms don't have them. I was given a quick fix by using: Response.Write("alert('Hello world');");. This works well enough when using it just once as a login confirmation upon opening a new page. The problem I am having is that I have a bunch of if statements within a button click event and only the first script alert works (if (poundRadBtn.Checked)). Would someone be able to explain to me why the other three don't work and if there is a fix or an alternative? Thanks in advance!

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>");
            }
    }
3
  • There doesn't seem to be any difference between the first and others. Have you debugged to make sure that the other Response.Write lines are actually being hit?
    – johna
    Dec 14, 2016 at 23:09
  • The other response.writes aren't being hit. I tested by adding labels and trying to set text. I just don't get it, the code is copy and pasted for each radio button so if one works, why don't they all?
    – ACostea
    Dec 15, 2016 at 11:04
  • If the other response.writes aren't b3eing hit then the problem must be with the if lines. Have you stepped through each line while debugging and checked the values while debugging in your if lines?
    – johna
    Dec 15, 2016 at 19:48

1 Answer 1

0

The compiler will read your else if statements as nested. It would look like this:

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

That's why if the first if fails no other if statements are caught.

Rewriting it with empty else statements is one way to fix this:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.