We help IT Professionals succeed at work.

DropDownList in FormView with no list in EditItemTemplate

Medium Priority
790 Views
Last Modified: 2012-05-06
DropDownList in EditItemTemplate of a FormView contains only the value in the record.

How can I get the four different options to show in the DropDownList so that I can change the value when in Edit mode of the FormView?

The DropDownList shows the correct value from the selected record, but only that one value is in the dropdown.  I want to be able to select another value and change the record. I can change other fields which are mostly TextBoxes and CheckBoxes just fine.

What am I missing?  Thanks.
<asp:DropDownList ID="ddlGoal" runat="server" Width="250px" 
							DataValueField="SuggestionID"
							DataSourceID="SqlDataSource2"
							DataTextField="Goal">
				        <asp:ListItem Value="1" Text="Goal 1 - Increase Sustainability of Workforce System"></asp:ListItem>
						<asp:ListItem Value="2" Text="Goal 2 - Maintain Role as Local Workforce Leaders"></asp:ListItem>
				        <asp:ListItem Value="3" Text="Goal 3 - Customers Choose One-Stops as 1st Choice"></asp:ListItem>
				        <asp:ListItem Value="4" Text="Goal 4 - Maximize Employment & Training to Targeted Groups"></asp:ListItem>
					</asp:DropDownList>

Open in new window

Comment
Watch Question

Praveen VenuTechnical Project Manager

Commented:
put the codes that bind the ddlGoal outside the IspostBack check

Author

Commented:
IspostBack check?

On the .aspx page I have a SqlDataSource2.
The code in the Code Snippet above is in the EditItemTemplate of the FormView.  That's where I'm binding the ddlGoal.

Unless I missunderstood your comment, I don't have an IspostBack check.
Commented:
I added a ListItem with a Value="0" and added AppendDataBoundItems="True" and SelectedValue='<%# Bind("Goal") %>'

The DropDownList works but I get an extra instance of whatever the current item is at the bottom of the list.  For example if the record currently contains "Goal 2 -", when I go to edit it I get a dropdown populated thus:
-Select-
Goal 1 -
Goal 2 -
Goal 3 -
Goal 4 -
Goal 2 -

It appears that "AppendDataBoundItems="True" is doing exactly that, but if I set it to False, then I get this error:
'ddlGoal' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Do I just have to live with that extra item or is there a way to fix it?
<asp:DropDownList ID="ddlGoal" runat="server" Width="250px" 
		DataSourceID="SqlDataSource2"
		DataTextField="Goal"
		DataValueField="SuggestionID"
		Enabled="True"
		SelectedValue='<%# Bind("Goal") %>' 
		AppendDataBoundItems="True">
	<asp:ListItem Text="-Select-" Value="0"></asp:ListItem>
    <asp:ListItem Value="Goal 1 - Increase Sustainability of Workforce System" Text="Goal 1 - Increase Sustainability of Workforce System"></asp:ListItem>
	<asp:ListItem Value="Goal 2 - Maintain Role as Local Workforce Leaders" Text="Goal 2 - Maintain Role as Local Workforce Leaders"></asp:ListItem>
    <asp:ListItem Value="Goal 3 - Customers Choose One-Stops as 1st Choice" Text="Goal 3 - Customers Choose One-Stops as 1st Choice"></asp:ListItem>
    <asp:ListItem Value="Goal 4 - Maximize Employment & Training to Targeted Groups" Text="Goal 4 - Maximize Employment & Training to Targeted Groups"></asp:ListItem>
</asp:DropDownList>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Dear Admin,
I haven't abandoned this question, I just haven't gotten a response from the "Expert".
Praveen VenuTechnical Project Manager

Commented:
Can you please explain the requirement little more clearly?

Author

Commented:
Hi, yes the problem is this:
With a DropDownList containing 4 items, configured as in the Code Snippet above...
The DropDownList works but I get an extra instance of whatever the current item is at the bottom of the list.  For example if the record currently contains "Goal 2 -", when I go to edit it I get a dropdown populated thus:
-Select-
Goal 1 -
Goal 2 -
Goal 3 -
Goal 4 -
Goal 2 -

What I need is for the DropDownList to only show the 4 items:
Goal 1 -
Goal 2 -
Goal 3 -
Goal 4 -

It is showing an extra item at the botton of the list which is whatever the value of that field currently in the database.   This is in an EditItemTemplate, so I'm changing an existing record with an existing value.  That existing value is being Appended to the list of items in the DropDownList.

See my comment, "ID:23606539Author:megninDate:02.10.2009 at 06:00PM EST".

Thanks.

Author

Commented:
Okay, here's a new RadioButtonList I'm working with that has the same problem.

This one is also inside the EditItemTemplate of a FormView:

                        <asp:RadioButtonList ID="radStatus" runat="server" RepeatDirection="Vertical"
                                                 RepeatLayout="Flow"
                                                 DataSourceID="SqlDataSource1"
                                                 DataTextField="Status"
                                                 DataValueField="keyApplicantID"
                                                 SelectedValue='<%# Bind("Status") %>'
                                                 AppendDataBoundItems="True"
                                                 >
                          <asp:ListItem Value="0">None</asp:ListItem>
                          <asp:ListItem Value="1">Complete</asp:ListItem>
                          <asp:ListItem Value="2">Pending</asp:ListItem>
                          <asp:ListItem Value="3">Ineligable</asp:ListItem>
                    </asp:RadioButtonList>

It doesn't work at all and I get an error if I don't have "AppendDataBoundItems='True' "
With it, instead of the four items above that I want radio buttons for, there are 5.  One extra one at the bottom always has whatever value is currently in the database (for the field this radiobutton is for).  I'd really like to get rid of that extra item.

Author

Commented:
Here is what it looks like.  Since the Current value is "1" there's that extra radio button on the bottom labeled "1"
RadioButtonList.jpg
Praveen VenuTechnical Project Manager

Commented:
try this

 <asp:RadioButtonList ID="radStatus" runat="server" RepeatDirection="Vertical"
                                                 RepeatLayout="Flow"
                                                 DataSourceID="SqlDataSource1"
 
                                                 SelectedValue='<%# Bind("Status") %>'
                                                 AppendDataBoundItems="True"
                                                 >
                          <asp:ListItem Value="0">None</asp:ListItem>
                          <asp:ListItem Value="1">Complete</asp:ListItem>
                          <asp:ListItem Value="2">Pending</asp:ListItem>
                          <asp:ListItem Value="3">Ineligable</asp:ListItem>
                    </asp:RadioButtonList>

Open in new window

Author

Commented:
I think I need that line to tell it where to get the data.
RadioButtonList2.jpg
RadioButtonList3.jpg
Praveen VenuTechnical Project Manager

Commented:
Are you sure that the value of

<%# Bind("Status") %> is 0,1,2 or 3

Author

Commented:
Well right now it's all 0 except for one test record that is currently 3 and so the EditItemTemplate in the web browser looks like this:

RadioButtonList4.jpg
Praveen VenuTechnical Project Manager

Commented:
Please post the answer and mark the solution

Author

Commented:
In this comment:
ID:23606539Author:megninDate:02.10.2009 at 06:00PM EST

What I added was not suggested by anyone else, I found it "Googling".  
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.