Last post Mar 19, 2010 09:58 AM by Das.Sandeep
Mar 19, 2010 05:34 AM|DavWhite2010|LINK
I have a drop down list:
<asp:DropDownList id="Colour" runat="server">
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"
but this just gives the error
Input string was not in a correct format.
What is wrong?
Mar 19, 2010 09:20 AM|MetalAsp.Net|LINK
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.
Mar 19, 2010 09:34 AM|DavWhite2010|LINK
So in my ListItem I do:
How do I get the value to calculate it though?
Mar 19, 2010 09:36 AM|MetalAsp.Net|LINK
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.
Mar 19, 2010 09:45 AM|atconway|LINK
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" />
...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
Mar 19, 2010 09:58 AM|Das.Sandeep|LINK
Atconway is correct, you have to set value instead of Text and on postback fetch value from value. It will work.