views:

871

answers:

2

Ok, I am going to try and give a very bried yet detailed description of what I need to accomplish.

First, I have a LinqToSQL datacontext defined that has an employee object. I then created a BLL (partial of employee) that handles my validations, inserts, updates etc. Everthing is good at this point. Next, I created a detailsview with an object data source binding to my select/update/insert methods on my Employee object. This is good too, data pulls like it should, updates, inserts like it should except for one issue. I am having trouble with drop down list inside of the details view. These are required fields, cannot be null. When that constraint is dropped, the code functions just fine.

Code Snippet of details view - fields abbreviated to show relevant information.

<asp:DetailsView ID="grd_empDetails"  runat="server" DataSourceID="empDataSource" DataKeyNames="EmployeeID" DefaultMode="Insert" AutoGenerateRows="false">
            <Fields>
                <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                <asp:TemplateField HeaderText="Type of Employee">
                    <ItemTemplate>
                        <asp:DropDownList ID="empType" runat="server" DataValueField="empType" AppendDataBoundItems="true">
                            <asp:ListItem Text="Full Time" Value="1" />
                            <asp:ListItem Text="Part Time" Value="2" />
                            <asp:ListItem Text="Subcontractor or 1099" Value="3" />
                            <asp:ListItem Text="Intern" Value="4" />
                        </asp:DropDownList>
                    </ItemTemplate>                   
                </asp:TemplateField>
                <asp:CommandField ShowInsertButton="true" ShowCancelButton="true" />    
            </Fields>
        </asp:DetailsView>

When the insert method is called, the empType is always returned as Nothing (empType is the database field).

What is the proper way to attach the value of the dropdownlist to my object so that it can be properly created in the database? Most of my searches keep telling me how to bind a dropdownlist to an object data source, but that is not what I need to do. This is a basic drop down list, 4 items that will never change. Would it make sense to use just a standard HTML select tag here or would that even help.

Thanks for looking, let me know if you need any more information.

A: 

Everything looks fine, but when you load it up, is the drop down list initially populated? If not, try setting one of the items to selected when it loads up, otherwise it might return no value if the user doesn't select one.

NickLarsen
That what was driving me nuts! The list displayed correctly, but couldn't get the value back out from it. I did figure it out after some crafty google searches - its posted above. Thanks for looking!
Tommy
A: 

Looks like it was something simple, yet the VS2008 IDE wasn't showing me that this was an option. In order to bind to the ObjectDataSource, you have to do something like this:

<asp:TemplateField HeaderText="Type of Employee">
                    <ItemTemplate>
                        <asp:DropDownList ID="empType" runat="server" DataValueField="empType" SelectedValue='<%#Bind("empType") %>'>
                            <asp:ListItem Text="Full Time" Value="1" />
                            <asp:ListItem Text="Part Time" Value="2" />
                            <asp:ListItem Text="Subcontractor or 1099" Value="3" />
                            <asp:ListItem Text="Intern" Value="4" />
                        </asp:DropDownList>
                    </ItemTemplate>                   
                </asp:TemplateField>

The SelectedValue part of the drop down list was not showing up for me as a valid tag for the dropdownlist, but VS doesn't choke on it at compile or runtime and most importantly, it works!

Tommy