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>");
}
}
The compiler will read your else if statements as nested. It would look like this:
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: