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.
Related
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>
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.
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.
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>
On my page I have FormView control, and I am binding Integer database field to the TextBox that's residing inside a FormView's EditItemTemplate.
<FormView ...>
<EditItemTemplate>
<dx:ASPxTextBox ID="txtDiameter" runat="server" Text='<%# Bind("Diameter") %>' />
...
<EditItemTemplate>
...
</FormView>
My problem is that when Diameter field is null, the txtDiameter gets value of empty string. When I click Update command (if I didn't provide any numerical value in txtDiameter), a client error is raised
Sys.WebForms.PageRequestManagerServerErrorException: is not a valid value for Int32.
I found some post from 2005 that claims that this is happening due to some bug. Well, now is 2012. The only way I figured how to deal with this is by using FormView_ItemUpdating event, to circle through all problematic values and convert them from String.Empty to null.
I am just little suspicious that it's maybe not necessary. Is there another way to deal with this problem?
Bind the editor's Value property instead:
<dx:ASPxTextBox ID="txtDiameter" runat="server" Value='<%# Bind("Diameter") %>' />
Does this work?