set value of listItem in DropDownList - c#

if (currentMap.EditMap != null)
{
ddlEditMapGroupName.SelectedIndex = ddlEditMapGroupName.Items.IndexOf(ddlEditMapGroupName.Items.FindByText(currentMap.EditMap));
}
sets the value of the drop down box to the stored value...but if currentMap.Edit map is null how can I set the value to the text of the listItem?
<asp:DropDownList ID="ddlEditMapGroupName" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="Select Group" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>

If I understand your question...
It sounds like you've got a dropdown list that already has a bunch of listitems in it (?).
And you're trying to set the selected item to match another control on the page.
One strategy I use (for certain cases) is to put a record into whichever table you're pulling your dropdownlist data from that says, simply 'Not Selected'. Then, make sure that whatever you're using to query data for your dropdownlist pulls that 'Not Selected' record into the list.
With that, you can simply add an 'else' clause to your control statement there to set the selected value to 'Not Selected' when appropriate.

if (currentMap.EditMap != null)
{
ddlEditMapGroupName.SelectedIndex = ddlEditMapGroupName.Items.IndexOf(ddlEditMapGroupName.Items.FindByText(currentMap.EditMap));
}
else
{
ddlEditMapGroupName.SelectedValue = "0";
}
<asp:DropDownList ID="ddlEditMapGroupName" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="Select Group" Value="0" Selected="True"></asp:ListItem>
</asp:DropDownList>
You could simply set the SelectedValue property to the default value which in your case is Select Group and its value is 0...
This should be done once your DropDownList is already bind to a data source.

Related

Can't two ListItem objects have same value property?

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.

DropDownList holds one more value

I have dropdownlist which has items that text and value property of the item are set to primary key. Yes, I show primary key with text property and querying with value property.
I want also to get selected item's other property such as name without querying dataset that binds to dropdownlist or querying database.
How can I do that?
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="ItemID" Value="ItemID"></asp:ListItem> // I want get item's name
</asp:DropDownList>
DropDownList Aspx Markup :
<asp:DropDownList ID="ddlDropDown" runat="server">
<asp:ListItem Text="ItemID" Value="ItemID" ThirdValue="ItemName" />
</asp:DropDownList>
Retrieve value like this :
ListItem item = ddlDropDown.Items.FindByValue("ItemID");
string value = item.Attributes["ThirdValue"];
If you want to get only one property such as name, you might want to do that
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="ItemID" Value="ItemName"></asp:ListItem>
</asp:DropDownList>
You can query with Text property which is primary key ItemID and get Value property which is ItemName with
SelectedItem.Value

DropDownList inside EditItemTemplate

I am using a dropdown within an EditTemplate as such:
<EditItemTemplate>
<asp:DropDownList ID="ddlBusinessType" runat="server" DataSourceID="BusinessTypeSource" DataTextField="Value" DataValueField="Value" AppendDataBoundItems="true" Text='<%# Bind("BusinessType") %>'>
<asp:ListItem>Please Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
The DataSource has the following value:
Personal
Professional
The problem that I am running into is that the field that I am binding has a blank value.
As a blank value is not in the DataSource I get the following error message:
'ddlBusinessType' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
Not sure how to fix this. When I do my binding, if the value is not in the datasource, I like to default it to 'Please Select'
Depends on the circumstances, but one option I'll use often times is as follows:
1) In the lookup table, I'll have an entry titled 'Not Selected',
with a primary key value of 0 (or whatever).
2) In the transaction table, update all of the null values for that field to 0 (or whatever the value is for your 'Not Selected' option). Also, set the default value of that field to 0.
This is nice for a couple of reasons -- one, no special mark-up required (so you know it will always be consistent) -- and two, wherever else you use this data, you'll have access to the 'not selected' output.
It should be like:
<EditItemTemplate>
<asp:DropDownList ID="ddlBusinessType" runat="server"
DataSourceID="BusinessTypeSource"
DataTextField="BusinessType" DataValueField="Value" AppendDataBoundItems="true" >
<asp:ListItem Value="-1">Please Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
1) No need to set Text as you already bounded the text field.
2) You need to set a value for default item you have added in markup.
You are trying to select an item before binding process. Capture Binding event and set de selected value on this place.

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