Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fill dropdownlist in detailsview

Status
Not open for further replies.

pkailas

Programmer
Jun 10, 2002
555
US
When entering the Edit mode of a DetailsView control, I would like to populate a DropDownList with all the possible values from a database.

Then I would like to have the Value that was stored in that record to be selected.

This will allow the user to see what the current selection is and allow them to select a new value from the DropDownList.

Is this even possible with ASP.NET 2.0?

If so can someone point me in the right direction please?

_______
I love small animals, especially with a good brown gravy....
 
I thought I'd describe more of my troubles.

Here is the code I have in place.
Code:
            <asp:TemplateField HeaderText="LDirection" SortExpression="LDirection">
                <EditItemTemplate>
                    <asp:DropDownList ID="Direction" runat="server" DataSourceID="LWAdminLoadEdit" DataTextField="LDirection" DataValueField="LDirection" SelectedValue='<%# Eval("LDirection") %>' AutoPostBack="True">
                    <asp:ListItem>N</asp:ListItem>
                    <asp:ListItem>S</asp:ListItem>
                    <asp:ListItem>E</asp:ListItem>
                    <asp:ListItem>W</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>

The issue is that the only value that is in the dropdown is the one from the record itself, none of the ones I've added are in there.

_______
I love small animals, especially with a good brown gravy....
 
Try adding this property to your dropdownlist
AppendDataBoundItems="true"

Also, remove the autopostback=true. You won't want a post back to happen each time the user makes a selection in the ddl
 
Ok, I found what the issue was. I removed the DataSoureID="LWAdminLoadEdit" and it worked. Your code didn't hurt anything and I will assume it is better with your additions/subtractions.

Now is there a way for me to actually populate the dropdown list from values using data from a database? I have a table that stores the directions and would prefer to populate the dropdown list from my database. I realize that for directions there are only 8 values, but I have other fields that have scores of rows and they are dynamic.

_______
I love small animals, especially with a good brown gravy....
 
I haven't used the detailsview in a long time, but I think you have to use ItemCommand event and check the command name. If it's edit, then use FindControl() to find your ddl and query the db and bind to the ddl.
 
I resolved this issue. The problem was that there were spaces after the data in the database. So, the values didn't equal each other, therefore the item wasn't in the collection. By trimming the data within the SQL Select statement, I resolved this issue.

_______
I love small animals, especially with a good brown gravy....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top