如何使这个DropDownList工作并更新ListView中的数据? [英] How to make this DropDownList works and update data inside the ListView?

查看:71
本文介绍了如何使这个DropDownList工作并更新ListView中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

**员工表:用户名,名称,工作,部门代码**

**部门表:部门代码,部门名称,部门快捷方式**

我正在使用ListView来显示员工的信息.我没有显示DivisionCode,而是放置了一个DropDownList来显示DivsionShortcut,当然,我将此DropDownList放在了ListView的EditItemTemplate中.除编辑员工信息外,其他所有操作均正常.当我尝试更改员工的部门时,出现以下错误:

***

DropDownList1''的SelectedValue无效,因为它确实存在
在项目列表中不存在.参数名称:值


***

我在ASP.NET中的代码:

I have two tables in my database as following:

**Employee Table: Username, Name, Job, DivisonCode**

**Division Table: DivisionCode, DivisionName, DivisionShortcut**

I am using ListView to show the information of the employee. Instead of showing the DivisionCode, I put a DropDownList that for showing the DivsionShortcut and of course I put this DropDownList inside the EditItemTemplate in the ListView. Everything works fine except editing the employee information. When I tried to change the division of the employee, I got the following error:

***

DropDownList1'' has a SelectedValue which is invalid because it does
not exist in the list of items. Parameter name: value



***

My Code in ASP.NET:

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

                 <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:DropDownList ID="DropDownList1" runat="server"
                                        DataSourceID="SqlDataSource1" DataTextField="DivisionShortcut" selectedValue=''<%# Bind("DivisionShortcut") %>''
                                        DataValueField="SapCode">
                                    </asp:DropDownList>
                                    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                        InsertCommand="INSERT INTO [Divisions] ([SapCode], [DivisionShortcut]) VALUES (@SapCode, @DivisionShortcut)"
                                        SelectCommand="SELECT * FROM [Divisions]"
                                        UpdateCommand="UPDATE [Divisions] SET [DivisionShortcut] = @DivisionShortcut WHERE [SapCode] = @SapCode">
                                        <DeleteParameters>
                                            <asp:Parameter Name="SapCode" Type="Double" />
                                        </DeleteParameters>
                                        <InsertParameters>
                                            <asp:Parameter Name="SapCode" Type="Double" />
                                            <asp:Parameter Name="DivisionShortcut" Type="String" />
                                        </InsertParameters>
                                        <UpdateParameters>
                                            <asp:Parameter Name="DivisionShortcut" 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="DivisionShortcutTextBox" runat="server"
                                 Text=''<%# Bind("DivisionShortcut") %>'' />
                         </td>
                     </tr>
                 </InsertItemTemplate>
                 <ItemTemplate>
                     <tr style="">
                        <td>
                            <asp:ImageButton ID="DeleteButton" ImageUrl="images/delete.png" Width="20px" runat="server" CommandName="Delete" />
                            <asp:ImageButton ID="EditButton" ImageUrl="images/edit.png" Width="20px" runat="server" CommandName="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="DivisionShortcutLabel" runat="server"
                                 Text=''<%# Eval("DivisionShortcut") %>'' />
                         </td>
                     </tr>
                 </ItemTemplate>
                 <LayoutTemplate>
                                 <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                                     <tr runat="server" style="">
                                         <th runat="server">
                                             Name</th>
                                         <th runat="server">
                                             Username</th>
                                         <th runat="server">
                                             JobTitle</th>
                                         <th runat="server">
                                             BadgeNo</th>
                                         <th runat="server">
                                             Division</th>
                                     </tr>
                                     <tr ID="itemPlaceholder" runat="server">
                                     </tr>
                                 </table>
                         <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:ImageButton ID="DeleteButton" ImageUrl="images/delete.png" Width="20px" runat="server" CommandName="Delete" />
                            <%--<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
                                Text="Delete" />--%>
                            <asp:ImageButton ID="EditButton" ImageUrl="images/edit.png" Width="20px" runat="server" CommandName="Edit" />
                            <%--<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="DivisionShortcutLabel" runat="server"
                                 Text=''<%# Eval("DivisionShortcut") %>'' />
                         </td>
                     </tr>
                 </SelectedItemTemplate>
             </asp:ListView>
             <br />
             <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                 ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                 SelectCommand="SELECT     dbo.employee.Name, dbo.employee.Username, dbo.employee.JobTitle, dbo.employee.BadgeNo, dbo.Divisions.DivisionShortcut
FROM         dbo.Divisions INNER JOIN
                      dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode"

                DeleteCommand="DELETE FROM [Employee] WHERE [Username] = @Username"

                UpdateCommand="UPDATE [Divisions] SET [DivisionShortcut] = @DivisionShortcut WHERE [DivisionCode] = @DivisionCode"

                FilterExpression="[DivisionShortcut] like ''{0}%''">

                <DeleteParameters>
                    <asp:Parameter Name="Username" Type="String" />
                </DeleteParameters>

                <UpdateParameters>
                    <asp:Parameter Name="DivisionShortcut" Type="String" />
                </UpdateParameters>

                <FilterParameters>
                        <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName"
                                                 PropertyName="SelectedValue" Type="String" />
                </FilterParameters>
             </asp:SqlDataSource>


那么如何解决此问题?

推荐答案

ConnectionStrings:testConnectionString%> InsertCommand ="INSERT INTO [Divisions]([SapCode],[DivisionShortcut])VALUES(@SapCode,@DivisionShortcut)" SelectCommand ="SELECT * FROM [部门]" UpdateCommand ="UPDATE [Divisions] SET [DivisionShortcut] = @DivisionShortcut WHERE [SapCode] = @SapCode"> < DeleteParameters> < asp:参数名称="SapCode"类型="Double"/> </DeleteParameters> < InsertParameters> < asp:参数名称="SapCode"类型="Double"/> < asp:参数名称="DivisionShortcut"类型="String"/> </InsertParameters> < UpdateParameters> < asp:参数名称="DivisionShortcut"类型="String"/> < asp:参数名称="SapCode"类型="Double"/> </UpdateParameters> </asp:SqlDataSource> </td> </tr> </EditItemTemplate> < EmptyDataTemplate> < table runat ="server" style ="> < tr> < td> 没有数据返回. </tr> </table> </EmptyDataTemplate> < InsertItemTemplate> < tr style ="> < td> < asp:Button ID ="InsertButton" runat ="server" CommandName ="Insert" Text =插入"/> < asp:Button ID ="CancelButton" runat ="server" CommandName ="Cancel" 文字=清除"/> </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 =服务器" Text =''<%#Bind("JobTitle")%>''/> </td> < td> < asp:TextBox ID ="BadgeNoTextBox" runat =服务器" Text =''<%#Bind(" BadgeNo)%>''/> </td> < td> < asp:TextBox ID ="DivisionShortcutTextBox" runat =服务器" Text =''<%#Bind("DivisionShortcut")%>''/> </td> </tr> </InsertItemTemplate> < ItemTemplate> < tr style ="> < td> < asp:ImageButton ID ="DeleteButton" ImageUrl ="images/delete.png" Width ="20px" runat ="server" CommandName ="Delete"/> < asp:ImageButton ID ="EditButton" ImageUrl ="images/edit.png" Width ="20px" runat ="server" CommandName ="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:标签ID ="DivisionShortcutLabel" runat =服务器" Text =''<%#Eval("DivisionShortcut")%>''/> </td> </tr> </ItemTemplate> < LayoutTemplate> <表ID ="itemPlaceholderContainer" runat =服务器" border ="0" style ="> < tr runat ="server" style ="> < th runat ="server"> 名称</th> < th runat ="server"> 用户名</th> < th runat ="server"> JobTitle</th> < th runat ="server"> BadgeNo&th; < th runat ="server"> 部门 </tr> < tr ID ="itemPlaceholder" runat ="server"> </tr> </table> < tr runat ="server"> < td runat ="server" style ="> < asp:DataPager ID ="DataPager1" runat ="server"> <字段> < asp:NextPreviousPagerField ButtonType ="Button" ShowFirstPageButton ="True" ShowLastPageButton ="True"/> </字段> </asp:DataPager> </td> </tr> </table> </LayoutTemplate> < SelectedItemTemplate> < tr style ="> < td> < asp:ImageButton ID ="DeleteButton" ImageUrl ="images/delete.png" Width ="20px" runat ="server" CommandName ="Delete"/> <%-< asp:按钮ID ="DeleteButton" runat =服务器" CommandName =删除" Text =删除"/>-%> < asp:ImageButton ID ="EditButton" ImageUrl ="images/edit.png" Width ="20px" runat ="server" CommandName ="Edit"/> <%-< 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:标签ID ="DivisionShortcutLabel" runat =服务器" Text =''<%#Eval("DivisionShortcut")%>''/> </td> </tr> </SelectedItemTemplate> </asp:ListView> < br/> < asp:SqlDataSource ID ="SqlDataSource1" runat =服务器" ConnectionString =<%
ConnectionStrings:testConnectionString %>" InsertCommand="INSERT INTO [Divisions] ([SapCode], [DivisionShortcut]) VALUES (@SapCode, @DivisionShortcut)" SelectCommand="SELECT * FROM [Divisions]" UpdateCommand="UPDATE [Divisions] SET [DivisionShortcut] = @DivisionShortcut WHERE [SapCode] = @SapCode"> <DeleteParameters> <asp:Parameter Name="SapCode" Type="Double" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="SapCode" Type="Double" /> <asp:Parameter Name="DivisionShortcut" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="DivisionShortcut" 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="DivisionShortcutTextBox" runat="server" Text=''<%# Bind("DivisionShortcut") %>'' /> </td> </tr> </InsertItemTemplate> <ItemTemplate> <tr style=""> <td> <asp:ImageButton ID="DeleteButton" ImageUrl="images/delete.png" Width="20px" runat="server" CommandName="Delete" /> <asp:ImageButton ID="EditButton" ImageUrl="images/edit.png" Width="20px" runat="server" CommandName="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="DivisionShortcutLabel" runat="server" Text=''<%# Eval("DivisionShortcut") %>'' /> </td> </tr> </ItemTemplate> <LayoutTemplate> <table ID="itemPlaceholderContainer" runat="server" border="0" style=""> <tr runat="server" style=""> <th runat="server"> Name</th> <th runat="server"> Username</th> <th runat="server"> JobTitle</th> <th runat="server"> BadgeNo</th> <th runat="server"> Division</th> </tr> <tr ID="itemPlaceholder" runat="server"> </tr> </table> <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:ImageButton ID="DeleteButton" ImageUrl="images/delete.png" Width="20px" runat="server" CommandName="Delete" /> <%--<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />--%> <asp:ImageButton ID="EditButton" ImageUrl="images/edit.png" Width="20px" runat="server" CommandName="Edit" /> <%--<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="DivisionShortcutLabel" runat="server" Text=''<%# Eval("DivisionShortcut") %>'' /> </td> </tr> </SelectedItemTemplate> </asp:ListView> <br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%


ConnectionStrings:testConnectionString%>" SelectCommand ="SELECT dbo.employee.Name,dbo.employee.Username,dbo.employee.JobTitle,dbo.employee.BadgeNo,dbo.Divisions.DivisionShortcut 来自dbo.Divisions内部联接 dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode DeleteCommand ="DELETE FROM [Employee] WHERE [Username] = @Username" UpdateCommand ="UPDATE [Divisions] SET [DivisionShortcut] = @DivisionShortcut WHERE [DivisionCode] = @DivisionCode" FilterExpression ="[DivisionShortcut]之类的"{0}%""> < DeleteParameters> < asp:参数名称=用户名"类型=字符串"/> </DeleteParameters> < UpdateParameters> < asp:参数名称="DivisionShortcut"类型="String"/> </UpdateParameters> < FilterParameters> < asp:ControlParameter ControlID ="ddlDivision" Name ="DivisionName" PropertyName ="SelectedValue" Type ="String"/> </FilterParameters> </asp:SqlDataSource>
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT dbo.employee.Name, dbo.employee.Username, dbo.employee.JobTitle, dbo.employee.BadgeNo, dbo.Divisions.DivisionShortcut FROM dbo.Divisions INNER JOIN dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode" DeleteCommand="DELETE FROM [Employee] WHERE [Username] = @Username" UpdateCommand="UPDATE [Divisions] SET [DivisionShortcut] = @DivisionShortcut WHERE [DivisionCode] = @DivisionCode" FilterExpression="[DivisionShortcut] like ''{0}%''"> <DeleteParameters> <asp:Parameter Name="Username" Type="String" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="DivisionShortcut" Type="String" /> </UpdateParameters> <FilterParameters> <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName" PropertyName="SelectedValue" Type="String" /> </FilterParameters> </asp:SqlDataSource>


那么如何解决此问题?


这篇关于如何使这个DropDownList工作并更新ListView中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆