Change blank selection in drop down list? - c#

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.

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.

'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

I'm using TemplateField in GridView to implement edit/delete from database.
I'm Querying Data with the SqlDataSource Control.
when I edit the Table from page I get following error:
'DropDownList1' has a SelectedValue which is invalid because it does
not exist in the list of items. Parameter name: value
This is due to the data from dB is not matching any of the DropDownList Country name.
I understand the problem from this question (solution is not given there!)
I think when I insert data to dB, it automatically adds redundant spaces to data(like name of country)
So, one solution is to remove the spaces (Trim) from String so that value get matched in one of the DropDownList items.
because <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("Country")%>'
doesn't match any list item of DropDownList
but
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("Country").ToString().Trim()%>'>
matches the list item of DropDownList.
Now I want to update the data back to the database but instead of country name null value get stored.
(I think it is the limitation of Eval())
I can update the data back to the dB using Bind()
but Bind doesn't support ToString().Trim() so I can't trim the string to match one of the items from DropDownList.
How can I update back the data to dB?
my raw code
"So, one solution is to remove the spaces (Trim) from String so that
value get matched in DropDownList fields."
I'm a little confused as to why this would make a difference.
Could you show us the raw HTML which this DropDownList control has created, and which row you're attempting to edit.
To view the raw HTML..
Open your webpage in Chrome
Hit F12 to show Developer Options
Now, right-click on the Drop Down List on your webpage, and select "Inspect Element"
In the Elements tab of Chrome's Developer window, you'll see a select element, which ASP.Net has created for you. Expand this element, to see the option elements.
Have a look down the value attributes for each of these.
You should see a more complicated version of this...
<select id="listOfPeople">
<option value=""></option>
<option value="10">Mike</option>
<option value="17">Geoffery</option>
<option value="18">Edward</option>
</select>
... and the error you're seeing suggests that when you click on one of the rows, its value isn't matching one of the values listed in the options.
Update
After looking at your .aspx file, I see that you display a list of Country names, but none have a Value:
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("Country").ToString().Trim()%>'>
<asp:ListItem>Select Country</asp:ListItem>
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>UK</asp:ListItem>
<asp:ListItem>China</asp:ListItem>
<asp:ListItem>North Korea</asp:ListItem>
<asp:ListItem>Kazakhstan</asp:ListItem>
</asp:DropDownList>
Shouldn't they look like this...
<asp:ListItem Value="North Korea">North Korea</asp:ListItem>
...rather than this...?
<asp:ListItem>North Korea</asp:ListItem>
(Again - use Chrome to check whether your drop down list of country names do have a Value in them. If they don't, this will explain the error you're seeing.)
Update 2
I'm stealing the following tip from one of the many other StackOverflow questions which have asked this same question:
Try adding the following into your edittemplate to see the current value:
<asp:Label ID="lblCountryName" runat="server" Text='<% #Bind("Country") %>'></asp:Label>
Taken from here
This should tell you the Value it's trying to look for, and cannot find, in the list of option items, created by your DropDownList control.
After scratching my head for several hours, finally, I found the elegant solution for the trailing spaces problem.
In my Table definition, In Country field definition I was using nchar(100) instead of nvarchar(100). The former always has 100 characters (thus the trailing spaces), the later can have up to 100 characters, but doesn't fill up the space (thus matches the item in DropDownlist).
In nutshell, <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Country")%>'> is working fine now.
I hope it helps future readers.
use this:
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Convert.ToString(Eval("Country"))%>'>
<asp:ListItem Value="">Select Country</asp:ListItem>
<asp:ListItem Value="India">India</asp:ListItem>
<asp:ListItem Value="USA">USA</asp:ListItem>
<asp:ListItem Value="UK">UK</asp:ListItem>
<asp:ListItem Value="China">China</asp:ListItem>
<asp:ListItem Value="North Korea">North Korea</asp:ListItem>
<asp:ListItemValue="Kazakhstan">Kazakhstan</asp:ListItem>
</asp:DropDownList>
Because values are not same male/Male and female/Female. Its case problems.
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>male</asp:ListItem>
<asp:ListItem>female</asp:ListItem>

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--");

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>

Dropdown list bound to object data source - how to update on a button click

This is probably something really stupidly simple..
I have a drop down list bound to an object data source. I have set AppendDataBoundItems to true so that I can have an initial select.
<asp:DropDownList ID="Accommodations1" runat="server" AutoPostBack="true" DataTextField="AccommodationTypeDescription" DataValueField="Id" OnDataBound="Accommodations1_DataBound" onSelectedIndexChanged="Accommodations1_SelectedIndexChanged" Width="200px" DataSourceID="AccommodationDs" AppendDataBoundItems="true">
<asp:ListItem Text="Select" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="AccommodationDs" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="ListByPropertyId" TypeName="PropertyAccommodationController">
<SelectParameters>
<asp:Parameter Name="PropertyId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
I have a button which adds an accommodaton - so after that happens I need the dropdown list to update to include the new accommodation. So I've tried calling databind on the dropdownlist, and databind on the datasource - and nothing is making this dropdown list update.
eg. PropertyAccommodations1.DataBind();
Could someone please let me know what I'm doing wrong. Originally I thought it was due to an update panel issue.. but I've removed the update panel and it still doesn't work (and checked the master page doesn't include an update panel).
Thanks!!
You can subrcibe to DDL OnDataBound and add the "Select" item
Accommodations1.Items.Add(new ListItem("Select",""));
In the button click, after you call .DataBind() on the DDL, you can then do
Accommodations1.Items.Add(new ListItem("Select"));
Thanks for the quick responses - have implemented
Accommodations1.Items.Insert(0,new ListItem("Select",""));
as I specifically wanted it at the top :)
I still think it would be neater to have this default item in the source of the page - but that AppendDataItems is tripping me up.

Categories