Does SelectedItem.Text work in an if statement?

I have a number of drop-down lists that when changed is a function call that converts the total value from binary to decimal and hexadecimal. Then I have an if statement that checks that all lists have been changed. My If statement does not work correctly, and also, if I select 0 from the list, the item switches to --Select -.

Here is a drop down list

<asp:DropDownList ID="r1" runat="server" OnSelectedIndexChanged="convert" AutoPostBack="true" >
    <asp:ListItem Text="--Select--" Value="0" />
    <asp:ListItem Text="0" Value="0" />
    <asp:ListItem Text="1" Value="1" />
</asp:DropDownList>

Here is the if statement

if (r1.SelectedItem.Text != "--Select--" && 
    r2.SelectedItem.Text != "--Select--" &&      
    r3.SelectedItem.Text != "--Select--" && 
    r4.SelectedItem.Text != "--Select--" && 
    r5.SelectedItem.Text != "--Select--" && 
    r6.SelectedItem.Text != "--Select--" && 
    r7.SelectedItem.Text != "--Select--" && 
    r8.SelectedItem.Text != "--Select--" && 
    g1.SelectedItem.Text != "--Select--" && 
    g2.SelectedItem.Text != "--Select--" && 
    g3.SelectedItem.Text != "--Select--" && 
    g4.SelectedItem.Text != "--Select--" && 
    g5.SelectedItem.Text != "--Select--" && 
    g6.SelectedItem.Text != "--Select--" && 
    g7.SelectedItem.Text != "--Select--" && 
    g8.SelectedItem.Text != "--Select--" && 
    b1.SelectedItem.Text != "--Select--" && 
    b2.SelectedItem.Text != "--Select--" && 
    b3.SelectedItem.Text != "--Select--" && 
    b4.SelectedItem.Text != "--Select--" && 
    b5.SelectedItem.Text != "--Select--" && 
    b6.SelectedItem.Text != "--Select--" && 
    b7.SelectedItem.Text != "--Select--" && 
    b8.SelectedItem.Text != "--Select--")
{
    cBox.Attributes["InnerHtml"] = "test";
}
else
{
    cBox.Attributes["InnerHtml"] = "world";
}
+3
source share
3 answers
if (Convert.toString(r1.SelectedItem.Text) != "--Select--")
{
    cBox.Attributes["InnerHtml"] = "test";
}
else
{
    cBox.Attributes["InnerHtml"] = "world";
}

You can do it with leftovers

0
source

As you added "--Select--"at the beginning dropdownlist, you can use the property DropDownList.SelectedIndexinstead DropDownList.SelectedItem. According to MSDN,

DropDownList.SelectedIndex - DropDownList. - 0, .

if . Extract Method, :

private bool AreAllProperValuesSelected()
{
    return
        (r1.SelectedIndex != 0) &&
        (r2.SelectedIndex != 0) &&
        (r3.SelectedIndex != 0) &&
        (r4.SelectedIndex != 0) &&
        (r5.SelectedIndex != 0) &&
        (r6.SelectedIndex != 0) &&
        (r7.SelectedIndex != 0) &&
        (r8.SelectedIndex != 0) &&
        (g1.SelectedIndex != 0) &&
        (g2.SelectedIndex != 0) &&
        (g3.SelectedIndex != 0) &&
        (g4.SelectedIndex != 0) &&
        (g5.SelectedIndex != 0) &&
        (g6.SelectedIndex != 0) &&
        (g7.SelectedIndex != 0) &&
        (g8.SelectedIndex != 0) &&
        (b1.SelectedIndex != 0) &&
        (b2.SelectedIndex != 0) &&
        (b3.SelectedIndex != 0) &&
        (b4.SelectedIndex != 0) &&
        (b5.SelectedIndex != 0) &&
        (b6.SelectedIndex != 0) &&
        (b7.SelectedIndex != 0) &&
        (b8.SelectedIndex != 0);
}

, :

private bool AreAllProperValuesSelected()
{
    var list = new List<DropDownList>
                   {
                       r1,
                       r2,
                       r3,
                       r4,
                       r5,
                       r6,
                       r7,
                       r8,
                       g1,
                       g2,
                       g3,
                       g4,
                       g5,
                       g6,
                       g7,
                       g8,
                       b1,
                       b2,
                       b3,
                       b4,
                       b5,
                       b6,
                       b7,
                       b8
                   };

    return list.TrueForAll(item => item.SelectedIndex != 0);
}

if, :

if (AreAllProperValuesSelected())
{
    cBox.Attributes["InnerHtml"] = "test";
}
else
{
    cBox.Attributes["InnerHtml"] = "world";
}
0

try it

if (r1.Text != "--Select--" && 
r2.Text != "--Select--" &&      
r3.Text != "--Select--" && 
r4.Text != "--Select--" && 
r5.Text != "--Select--" && 
r6.Text != "--Select--" && 
r7.Text != "--Select--" && 
r8.Text != "--Select--" && 
g1.Text != "--Select--" && 
g2.Text != "--Select--" && 
g3.Text != "--Select--" && 
g4.Text != "--Select--" && 
g5.Text != "--Select--" && 
g6.Text != "--Select--" && 
g7.Text != "--Select--" && 
g8.Text != "--Select--" && 
b1.Text != "--Select--" && 
b2.Text != "--Select--" && 
b3.Text != "--Select--" && 
b4.Text != "--Select--" && 
b5.Text != "--Select--" && 
b6.Text != "--Select--" && 
b7.Text != "--Select--" && 
b8.Text != "--Select--")
{
    cBox.Attributes["InnerHtml"] = "test";
}
else
{
    cBox.Attributes["InnerHtml"] = "world";
}
0
source

All Articles