如何让这个DropDownList在ListView中运行?

时间:2011-12-21 08:48:19

标签: c# asp.net

我的数据库中有两个表如下:

员工表:用户名,姓名,职位,DivisonCode

分部表:DivisionCode,DivisionName

我正在使用ListView来显示员工的信息。我没有显示DivisionCode,而是放置一个用于显示DivsionNames的DropDownList,当然我将此DropDownList放在ListView中的EditItemTemplate中。除编辑员工信息外,一切正常。当我试图改变员工的分工时,我收到以下错误:


  

无法将值NULL插入列'DivisionCode',表中   'psspdbTest.dbo.employee';列不允许空值。更新失败。   声明已经终止。


ASP.NET中的代码:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="Username" 
                DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" >

                <AlternatingItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                                Text="Delete" />
                            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                        </td>
                        <td>
                            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                        </td>
                        <td>
                            <asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
                        </td>
                        <td>
                            <asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
                        </td>
                        <td>
                            <asp:Label ID="BadgeNoLabel" runat="server" 
                                Text='<%# Eval("BadgeNo") %>' />
                        </td>
                        <td>
                            <asp:Label ID="EmpOrgTypeLabel" runat="server" 
                                Text='<%# Eval("EmpOrgType") %>' />
                        </td>
                        <td>
                            <asp:Label ID="DivisionCodeLabel" runat="server" 
                                Text='<%# Eval("DivisionCode") %>' />
                        </td>
                    </tr>
                </AlternatingItemTemplate>

                <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:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                        </td>
                        <td>
                            <asp:Label ID="UsernameLabel1" runat="server" Text='<%# Eval("Username") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="JobTitleTextBox" runat="server" 
                                Text='<%# Bind("JobTitle") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="BadgeNoTextBox" runat="server" Text='<%# Bind("BadgeNo") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="EmpOrgTypeTextBox" runat="server" 
                                Text='<%# Bind("EmpOrgType") %>' />
                        </td>
                        <td>
                            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                                        DataSourceID="SqlDataSource1" DataTextField="DivisionName" 
                                        DataValueField="SapCode">
                                    </asp:DropDownList>
                                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                                        DeleteCommand="DELETE FROM [Divisions] WHERE [SapCode] = @SapCode" 
                                        InsertCommand="INSERT INTO [Divisions] ([SapCode], [DivisionName]) VALUES (@SapCode, @DivisionName)" 
                                        SelectCommand="SELECT * FROM [Divisions]" 
                                        UpdateCommand="UPDATE [Divisions] SET [DivisionName] = @DivisionName WHERE [SapCode] = @SapCode">
                                        <DeleteParameters>
                                            <asp:Parameter Name="SapCode" Type="Double" />
                                        </DeleteParameters>
                                        <InsertParameters>
                                            <asp:Parameter Name="SapCode" Type="Double" />
                                            <asp:Parameter Name="DivisionName" Type="String" />
                                        </InsertParameters>
                                        <UpdateParameters>
                                            <asp:Parameter Name="DivisionName" Type="String" />
                                            <asp:Parameter Name="SapCode" Type="Double" />
                                        </UpdateParameters>
                                    </asp:SqlDataSource>
                        </td>
                    </tr>
                </EditItemTemplate>
                <EmptyDataTemplate>
                    <table runat="server" 
                        style="">
                        <tr>
                            <td>
                                No data was returned.</td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <InsertItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                                Text="Insert" />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                                Text="Clear" />
                        </td>

                        <td>
                            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="UsernameTextBox" runat="server" 
                                Text='<%# Bind("Username") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="JobTitleTextBox" runat="server" 
                                Text='<%# Bind("JobTitle") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="BadgeNoTextBox" runat="server" Text='<%# Bind("BadgeNo") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="EmpOrgTypeTextBox" runat="server" 
                                Text='<%# Bind("EmpOrgType") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="DivisionCodeTextBox" runat="server" 
                                Text='<%# Bind("DivisionCode") %>' />
                        </td>
                    </tr>
                </InsertItemTemplate>
                <ItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                                Text="Delete" />
                            <asp:Button ID="EditButton" runat="server" CommandName="Edit" 
                                Text="Edit" />
                        </td>
                        <td>
                            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                        </td>
                        <td>
                            <asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
                        </td>
                        <td>
                            <asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
                        </td>
                        <td>
                            <asp:Label ID="BadgeNoLabel" runat="server" Text='<%# Eval("BadgeNo") %>' />
                        </td>
                        <td>
                            <asp:Label ID="EmpOrgTypeLabel" runat="server" 
                                Text='<%# Eval("EmpOrgType") %>' />
                        </td>
                        <td>
                            <asp:Label ID="DivisionCodeLabel" runat="server" 
                                Text='<%# Eval("DivisionCode") %>' />
                        </td>
                    </tr>
                </ItemTemplate>
                <LayoutTemplate>
                                <table runat="server">
                                    <tr runat="server">
                                        <td runat="server">
                                            <table ID="itemPlaceholderContainer" runat="server" border="0" 
                                                style="">
                                                <tr runat="server" style="">
                                                    <th runat="server">
                                                        </th>
                                                    <th runat="server">
                                                        Name</th>
                                                    <th runat="server">
                                                        Username</th>
                                                    <th runat="server">
                                                        JobTitle</th>
                                                    <th runat="server">
                                                        BadgeNo</th>
                                                    <th runat="server">
                                                        EmpOrgType</th>
                                                    <th runat="server">
                                                        DivisionCode</th>
                                                </tr>
                                                <tr ID="itemPlaceholder" runat="server">
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <tr runat="server">
                                        <td runat="server" 
                                            style="">
                                            <asp:DataPager ID="DataPager1" runat="server">
                                                <Fields>
                                                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                                        ShowLastPageButton="True" />
                                                </Fields>
                                            </asp:DataPager>
                                        </td>
                                    </tr>
                                </table>
                </LayoutTemplate>
                <SelectedItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                                Text="Delete" />
                            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                        </td>
                        <td>
                            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                        </td>
                        <td>
                            <asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
                        </td>
                        <td>
                            <asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
                        </td>
                        <td>
                            <asp:Label ID="BadgeNoLabel" runat="server" 
                                Text='<%# Eval("BadgeNo") %>' />
                        </td>
                        <td>
                            <asp:Label ID="EmpOrgTypeLabel" runat="server" 
                                Text='<%# Eval("EmpOrgType") %>' />
                        </td>
                        <td>
                            <asp:Label ID="DivisionCodeLabel" runat="server" 
                                Text='<%# Eval("DivisionCode") %>' />
                        </td>
                    </tr>
                </SelectedItemTemplate>
            </asp:ListView>

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                SelectCommand="SELECT     dbo.employee.Name, dbo.employee.Username, dbo.employee.JobTitle, dbo.employee.BadgeNo, dbo.employee.EmpOrgType, dbo.employee.DivisionCode
FROM         dbo.Divisions INNER JOIN
                      dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode" 
                DeleteCommand="DELETE FROM [employee] WHERE [Username] = @Username" 
                InsertCommand="INSERT INTO [employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (@Name, @Username, @JobTitle, @BadgeNo, @EmpOrgType, @DivisionCode)" 
                UpdateCommand="UPDATE [employee] SET [Name] = @Name, [JobTitle] = @JobTitle, [BadgeNo] = @BadgeNo, [EmpOrgType] = @EmpOrgType, [DivisionCode] = @DivisionCode WHERE [Username] = @Username">
                <DeleteParameters>
                    <asp:Parameter Name="Username" Type="String" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="Name" Type="String" />
                    <asp:Parameter Name="Username" Type="String" />
                    <asp:Parameter Name="JobTitle" Type="String" />
                    <asp:Parameter Name="BadgeNo" Type="Double" />
                    <asp:Parameter Name="EmpOrgType" Type="Double" />
                    <asp:Parameter Name="DivisionCode" Type="Double" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Name" Type="String" />
                    <asp:Parameter Name="JobTitle" Type="String" />
                    <asp:Parameter Name="BadgeNo" Type="Double" />
                    <asp:Parameter Name="EmpOrgType" Type="Double" />
                    <asp:Parameter Name="DivisionCode" Type="Double" />
                    <asp:Parameter Name="Username" Type="String" />
                </UpdateParameters>
            </asp:SqlDataSource>

那么如何解决这个问题呢?

1 个答案:

答案 0 :(得分:2)

将此添加到您的DropDownList SelectedValue='<%# Bind("DivisionCode") %>'

并删除AutoPostBack="true"