DropDownList holds one more value - c#

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

Related

Put a DropDownList instead of TextBox in DetailsView control

I have an EntityDataSource control that is bind to an edmx file.
I create a detailsview and set it's DataSource property to entitydatasource control.
now I want to put a DropDownList instead of TextBox for "no_region_code" in Inserting mode.
Here is what I found from internet but it does not insert the contents of dropdownlist to the table when i click on insert button of detailsview.
<asp:TemplateField HeaderText="no_region_code" SortExpression="no_region_code">
<InsertItemTemplate>
<asp:DropDownList ID="drp_region_list" runat="server" DataMember="no_region_code">
<asp:ListItem Value="41">tabriz</asp:ListItem>
<asp:ListItem Value="21">tehran</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
How can I put a dropdownlist instead of these textboxes?
You didn't bind any property on that DropDownList. Try something like this
<asp:DropDownList ID="drp_region_list" runat="server" SelectedValue='<%# Bind("no_region_code")%>' DataMember="no_region_code">
<asp:ListItem Value="41">tabriz</asp:ListItem>
<asp:ListItem Value="21">tehran</asp:ListItem>
</asp:DropDownList>
EDIT : bind represent a way to link your property from the code behind (or model) to an asp control. This binding is bidirectional. For example, if you already have a value for no_region_code property in the code behind, the dropdown will already have that value selected on the view. Google asp.net data binding and you'll find a lot of examples and some more in depth explanations.

set value of listItem in DropDownList

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.

First record in dropdown list will not update the other dropdownbox

I have a dropdown box that is populated from results from a SQL query. The selected value from dropdownlist1 successfully populates the lbAuthors dropdown list. During testing I realized that the first record from dropdownlist1 never updates into the lbAuthors dropdownlist. Here is an example: if I have three authors name in the 2nd dropdown box (Frost, Kipling, Poe) the first name - Frost - does not update into the first dropdown box. Kipling or Poe do - but not Frost.
My question is - What do I need to include in my event to allow Frost (or whatever the first record is) to update into the first dropdown box? –
Code-behind:
protected void update_SelectedItem(object sender, EventArgs e)
{
lbAuthorList.Items.Clear();
lbAuthorList.Items.Add(new ListItem(DropDownList1.SelectedItem.Text, DropDownList1.SelectedItem.Text));
lbAuthorList.Items.FindByValue(DropDownList1.SelectedItem.Text).Selected = true;
}
Markup:
<asp:DropDownList runat="server"
ID="lbAuthors"
style="float:left;"
DataSourceID="odsAuthorList"
DataTextField="DisplayAuthorName" DataValueField="AuthorID"
onselectedindexchanged="lbUserList_SelectedIndexChanged"
AppendDataBoundItems="True" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1"
runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
</asp:DropDownList>
That is because when you select the first item, selcted index does not change. You need to insert a dummy item like this::
<asp:DropDownList ID="DropDownList1" runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
<asp:ListItem Text="--Select One--" Value="-1"></asp:ListItem>
</asp:DropDownList>
This issue rises because u wrote code for dropdownlist changed but initially first value selected at that time dropdownlist changed event not fired. If u choose second and then choose first one it will work fine.
Just add one dummy variable in dropdownlist item..
dropdownlist1.Items.Add("--Select--");

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