Search code examples
asp.netlistviewlinqdatasource

DropDownList inside ListView


I have a DropDownList inside a ListView. The ListView shows employees of a company, and the DDL lists groups to which they can be assigned.

I used 2 separate datasources. One to list the employees, and one to list the groups. It looks fine, but whenever I try to update the groups, it doesn't register.

How do I make sure that the DDL is a part of the update query for employees, even though it's connected to another datasource?

I haven't been able to find anything about this on here. Appreciate your time, thanks. :)

DataSources:

                <asp:LinqDataSource 
                    ID="LinqDataSource_Employees" 
                    runat="server" 
                    ContextTypeName="Blabla.bla.myContext" 
                    EntityTypeName="" 
                    OrderBy="Group_ID, DisplayName" 
                    TableName="Employees" 
                    EnableDelete="True" 
                    EnableInsert="True" 
                    EnableUpdate="True">
                </asp:LinqDataSource>

                <asp:LinqDataSource 
                    ID="LinqDataSource_Groups" 
                    runat="server" 
                    ContextTypeName="Blabla.bla.myContext" 
                    EntityTypeName="" 
                    Select="new (ID, Name)" 
                    TableName="Groups">
                </asp:LinqDataSource>

EditItemTemplate:

<EditItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                            </td>
                            <td>
                                <asp:Label ID="ID_UserNameLabel" runat="server" Text='<%# Eval("ID_UserName") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="DisplayNameTextBox" runat="server" Text='<%# Bind("DisplayName") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="EmailAddressTextBox" runat="server" Text='<%# Bind("EmailAddress") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="PhoneNumberTextBox" runat="server" Text='<%# Bind("PhoneNumber") %>' />
                            </td>
                            <td>
                                <asp:DropDownList ID="GroupDropDownList" runat="server" DataSourceID="LinqDataSource_Groups" DataTextField="Name" DataValueField="ID" />
                            </td>
                        </tr>
                    </EditItemTemplate>

Solution

  • After some more digging, I found a clue on how to continue.

    Turns out, that if you use Bind on the Property SelectedValue, it'll bind to the DataSource of the ListView, and not the one that was declaratively set in the DataSource property. :)

    <asp:DropDownList 
     ID="GroupDropDownList" 
     runat="server" 
     DataSourceID="LinqDataSource_Groups" 
     DataTextField="Name" 
     DataValueField="ID" 
     SelectedValue='<%# Bind("Group_ID") %>' />
    

    Hope this helps someone. :)