Can't two ListItem objects have same value property? - c#

My Code looks like
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="100">A</asp:ListItem>
<asp:ListItem Value="100">B</asp:ListItem>
</asp:ListBox>
Now when I try to select B, It by default selects A always. B is never selected. What is the reason for this behavior ?

The <asp:ListBox> renders as a <select> with <option>s in HTML. The value is persisted across postbacks, not the text. So it can't tell which value you wanted when there are multiple options with the same value, so it assumed the first. It's best practice to use unique option values.

It's recommended to use different values for 2 or more items. No two items can have the same values.
Set AutoPostBack="True" and you'll find that no matter how many items you add in the ListBox, it will always select the 0th index of that particular value.
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True">
<asp:ListItem Value="101">D</asp:ListItem>
<asp:ListItem Value="100">A</asp:ListItem>
<asp:ListItem Value="100">B</asp:ListItem>
<asp:ListItem Value="102">E</asp:ListItem>
<asp:ListItem Value="100">C</asp:ListItem>
</asp:ListBox>
Like, selecting B or C will always select A; where A being the 0th index for that particular value 100.

Related

How to Avoid default Selected from dropdown list

In my current code we have a drop down lists similar like below which is a mandatory field earlier so we need to select the value before submitting the form but now there is no need mandatory so removed the Mandatory check condition and current code as below.
<asp:dropdownlist id="ddlTest" Runat="server" TabIndex="01">
<asp:ListItem Selected="True" ">Select</asp:ListItem>
<asp:ListItem Value="N" >No</asp:ListItem>
<asp:ListItem Value="Y">Yes</asp:ListItem>
</asp:dropdownlist>
prmddlTest.Value = Format_SQL(ddlTest.SelectedItem.Value)
cmdSaveData.Parameters.Add(prmddlTest)
Function Format_SQL(ByVal sInput) As Object
If sInput = "" Or sInput = "-1" Then
Return DBNull.Value
Else
Return sInput
End If
End Function
I am getting error during submit the form because of default "Select" If I select No or Yes value going fine. How to avoid "Select" in code during submission.
Thanks in Advance
Try this one:
<asp:dropdownlist id="ddlTest" Runat="server" TabIndex="01">
<asp:ListItem Text="" Selected="True"></asp:ListItem>
<asp:ListItem Value="N" >No</asp:ListItem>
<asp:ListItem Value="Y">Yes</asp:ListItem>
</asp:dropdownlist>

How to make an asp dropdown list item unselectable

I have written following code to display drop down list :
<asp:DropDownList AutoPostBack="True" ID="ddlCities" runat="server" class="form-control input-sm" placeholder="" TabIndex="5">
<asp:ListItem>Select City</asp:ListItem>
<asp:ListItem value="3">Ahmedabad (All) ---------------</asp:ListItem>
<asp:ListItem value="3_3004"> Ahmedabad East</asp:ListItem>
<asp:ListItem value="3_3005"> Ahmedabad West</asp:ListItem>
<asp:ListItem value="3_3006"> Ahmedabad-Bopal and Surroundings</asp:ListItem>
<asp:ListItem value="3_3007"> Ahmedabad-Gandhinagar</asp:ListItem>
<asp:ListItem value="3_3008"> Ahmedabad-Sabarmati and Surroundings</asp:ListItem>
<asp:ListItem value="3_3009"> Ahmedabad-SG Highway and Surroundings</asp:ListItem>
</asp:DropDownList>
I want to make following item inside dropdown unselectable :
<asp:ListItem value="3">Ahmedabad (All) ---------------</asp:ListItem>
Note : I CAN'T USE OPTGROUP!!!!
and i don't want to hide it. it will be shown in dropdown, but user can't select this item.
I have tried adding 'disabled' attribute, but it hides that item.
i have also tried :
ddlCities.Items[1].Attributes.Add("Style", "cursor:not-allowed");
It doesn't allow cursor, but still user can select this item, is there any other way to make this particular item unselectable??
Even though you said that disabled attribute hides the element, you are wrong.
Disabled attribute is exactly what you should use for this:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">First</asp:ListItem>
<asp:ListItem Value="2" disabled="disabled">Second</asp:ListItem>
<asp:ListItem Value="3">First</asp:ListItem>
</asp:DropDownList>
Result:
I think you're looking for something like this. Please see if it can help you.
[Update]
just looked back to your post and understand you cannot use OPTGROUP. May i know the reason? as your requirement needs this option.

How can I multiply values from 2 dropdown lists in asp.net and display the results in a text box?

I have a lot of dropdown lists I am building for an insurance app. Each list has a number of options given a multiplier value. My question is how can I multiply these values from separate dropdown lists to give myself a final multiplier value in a textbox? I want to use whatever value is picked from each dropdown list.
The lists are formatted as follows
<asp:DropDownList id="points" runat="server">
<asp:ListItem Value="1.00">0</asp:ListItem>
<asp:ListItem Value="1.05">1</asp:ListItem>
<asp:ListItem Value="1.10">2</asp:ListItem>
<asp:ListItem Value="1.18">3</asp:ListItem>
<asp:ListItem Value="1.25">4</asp:ListItem>
<asp:ListItem Value="1.32">5</asp:ListItem>
<asp:ListItem Value="1.40">6</asp:ListItem>
<asp:ListItem Value="1.47">7</asp:ListItem>
<asp:ListItem Value="1.55">8</asp:ListItem>
<asp:ListItem Value="1.62">9</asp:ListItem>
<asp:ListItem Value="1.70">10</asp:ListItem>
<asp:ListItem Value="1.77">11</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="OtherPolicy" runat="server">
<asp:ListItem value="1.20">Yes</asp:ListItem>
<asp:ListItem value="1.00">No</asp:ListItem>
</asp:DropDownList>
I have only been coding for about 2 weeks so hopefully somebody can point me in the right direction. I have not found anything similar in the search here. Thanks guys!
It's important to remember that the values that are going to be stored within your DropDownLists will be strings, so the first step will be converting them to numerical values, which can be done in many ways, but for example purposes, you can use the Convert.ToDecimal() method :
// Convert your Points to a decimal
var pointsValue = Convert.ToDecimal(points.SelectedValue);
// Then convert your selection from your other DropDownList
var policyValue = Convert.ToDecimal(OtherPolicy.SelectedValue);
// Now that you have both of these, you can multiply them to retrieve your result
var result = pointsValue * policyValue;
// Store your result in another TextBox
YourResultTextBox.Text = result.ToString();
If you have more DropDownLists or other elements that would need to be part of this calculation, you would just need to ensure that you parse each of their values and then include them in your calculation.

Change blank selection in drop down list?

I have a databound dropdown list on my page. The first value in the selection list is blank. Is there a way that I can change it so that the first selection says "Unassigned" instead of blank? I've tried the following, but it didn't work:
// Insert 'Unassigned' value for artist dropdown
ddlArtists.Items.Insert(0, "Unassigned");
After inserting the above code, the list still appears unchanged, with the first selection value being a blank. Any pointers would be great! Thank you!
EDIT: Here is the code for the dropdown:
<asp:DropDownList ID="ddlArtists" runat="server" Width="130px" TabIndex="5"
OnSelectedIndexChanged="ddlArtists_SelectedIndexChanged"
DataSourceID="sqldsArtist" DataTextField="Name" DataValueField="ID"
OnDataBound="ddl_DataBound"
AutoPostBack="True">
You don't need to do it on the CodeBehind. Just do it like that:
<asp:DropDownList ID="ddlArtists" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Unassigned" Value="0" Selected="true" />
</asp:DropDownList>
The AppendDataBoundItems property defines whether the contents of the DropDownList should be cleared out before data binding.
Don't forget to check for a PostBack when you're data binding it, to avoid duplicates.
Set the SelectedIndex property of your DropDownList to 0.

Add a blank value for the first entry of a DropDownList

I have a DropDownList that is data bounds to a SQL Server Data Source. As it is, the first entry from the data source is the first entry selected by default. In order to select this entry (and have the DropDownList_SelectedIndexChange() function be called), I have to select the second option, and then the first option again.
Is there a way to enter an invalid item into the DropDownList as the first item, sort of as a "default value"?
Set AppendDataBoundItems to true on the drop-down list and then add your default item to the list
<asp:DropDownList ID="ddlOne" runat="server" AppendDataBoundItems="True"
DataSourceID="" DataTextField="" DataValueField="">
<asp:ListItem Value="">Please Select</asp:ListItem>
</asp:DropDownList>
This will result in all your data-bound values being added to the dropdown after the ones you have specified in the markup above
By "SQL Server Data Source" I assume you mean a declarative SQLDataSource in the page markup, so that there's no actual C# code in question here? If so, there are two things you'd need to do.
First, add a default value to the DropDownList. Second, set the property AppendDataBoundItems to true:
<asp:DropDownList ID="someID" runat="server" DataSourceID="someDataSource" DataTextField="name" DataValueField="id" AppendDataBoundItems="true">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>

Categories