locked
IF ELSE question RRS feed

  • Question

  • User-696496326 posted

    I have a drop down list:

    <asp:DropDownList id="Colour" runat="server">
    <asp:ListItem>Green</asp:ListItem>
    <asp:ListItem>Red</asp:ListItem>
    <asp:ListItem>Blue</asp:ListItem>
    </asp:DropDownList>

    I'm doing a calculation such as

    lbl2.Text = CInt((Size.Text)) + CInt((Colour.Text))


    Now obviously you can't add 5 plus Green together.

    What I want is for each colour to be represented by a number.

    I thought I could do this with an IF ELSE such as


    If (Colour.Text = "Green") Then
                
                Colour.Text = "3"
                
            End If


    but this just gives the error

    Input string was not in a correct format.


    What is wrong?

    Thanks

    Friday, March 19, 2010 5:34 AM

Answers

  • User-1360095595 posted

    Give your LIstItem a "Value" and you can assign numbers to these values.  In the code, you would cast the string "5" to an int and do calculations as you see fit.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2010 9:20 AM
  • User-696496326 posted

    So in my ListItem I do:


    <asp:ListItem Value="3">Green</asp:ListItem>


    yes?


    How do I get the value to calculate it though?


    Thanks



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2010 9:34 AM
  • User-1360095595 posted

    You can handle the SelectedIndexChanged event of the DDL (set your DDL to autopostback="true" and in the handler, you can read the selectedvalue via YourDdlId.SelectedValue.  This will return "5", which you can cast to an int.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2010 9:36 AM
  • User-952121411 posted

    Here is the code to extract the value in the 'SelectedIndexChanged' event:

    ...1st the source:

        <asp:ListBox ID="ListBox1" runat="server">      
            <asp:ListItem Value="3" Text="Red" />
            <asp:ListItem Value="3" Text="Blue" /> 
            <asp:ListItem Value="3" Text="Green" />       
        </asp:ListBox>


     

    ...and now the code behind (you can generate the event automatically by double clicking the control in the designer, or by selecting the event from the dropdowns in the code editor):

        Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
    
            Dim MyColorVal As Integer = Integer.Parse(Me.ListBox1.SelectedValue)
    
            'Now you can do the calculation for the label
            lbl2.Text = Integer.Parse(Size.Text) + MyColorVal
    
        End Sub


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2010 9:45 AM

All replies

  • User-1360095595 posted

    Give your LIstItem a "Value" and you can assign numbers to these values.  In the code, you would cast the string "5" to an int and do calculations as you see fit.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2010 9:20 AM
  • User-696496326 posted

    So in my ListItem I do:


    <asp:ListItem Value="3">Green</asp:ListItem>


    yes?


    How do I get the value to calculate it though?


    Thanks



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2010 9:34 AM
  • User-1360095595 posted

    You can handle the SelectedIndexChanged event of the DDL (set your DDL to autopostback="true" and in the handler, you can read the selectedvalue via YourDdlId.SelectedValue.  This will return "5", which you can cast to an int.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2010 9:36 AM
  • User-952121411 posted

    Here is the code to extract the value in the 'SelectedIndexChanged' event:

    ...1st the source:

        <asp:ListBox ID="ListBox1" runat="server">      
            <asp:ListItem Value="3" Text="Red" />
            <asp:ListItem Value="3" Text="Blue" /> 
            <asp:ListItem Value="3" Text="Green" />       
        </asp:ListBox>


     

    ...and now the code behind (you can generate the event automatically by double clicking the control in the designer, or by selecting the event from the dropdowns in the code editor):

        Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
    
            Dim MyColorVal As Integer = Integer.Parse(Me.ListBox1.SelectedValue)
    
            'Now you can do the calculation for the label
            lbl2.Text = Integer.Parse(Size.Text) + MyColorVal
    
        End Sub


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2010 9:45 AM
  • User-1636183269 posted

    Hello,

    Atconway is correct, you have to set value instead of Text and on postback fetch value from value. It will work.

    Friday, March 19, 2010 9:58 AM