尝试通过控制参数使用 Gridview 字段值过滤下拉列表时出错

发布于 2024-09-06 09:13:12 字数 3570 浏览 6 评论 0原文

我收到可怕的 'DropDownList1' 有一个无效的 SelectedValue,因为它不存在于项目列表中。 尝试使用其他绑定字段值之一过滤模板字段中的下拉列表时出现参数名称:值错误(我试图根据员工的部门获取员工列表 - 即用户可以更改员工但仅限于同一部门的另一名成员)。

这是代码:

<asp:GridView ID="Rotas" runat="server" AllowSorting="True"
DataSourceID="SqlDataSource3" AutoGenerateEditButton="True"  DataKeyNames="DateFrom,DateTo,DepartmentId"
    AutoGenerateColumns="False" OnRowUpdating="Rotas_RowUpdating">
    <Columns>
        <asp:BoundField DataField="DateFrom" HeaderText="DateFrom" ReadOnly="True" 
            SortExpression="DateFrom" />
        <asp:BoundField DataField="DateTo" HeaderText="DateTo" ReadOnly="True" 
            SortExpression="DateTo" />                
        <asp:TemplateField HeaderText="Employee Name" SortExpression="EmployeeName">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" 
                    DataSourceID="SqlDataSource4" DataTextField="EmployeeName" 
                    DataValueField="EmployeeName" SelectedValue='<%# Bind("EmployeeName") %>'>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>

                </asp:DropDownList>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="DepartmentId" HeaderText="DepartmentId" 
            ReadOnly="True" SortExpression="DepartmentId" />
        <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" ReadOnly="False" 
            SortExpression="EmployeeId" />
    </Columns>
</asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>" 
        SelectCommand="SELECT r.DateFrom, r.DateTo, e.EmployeeName, e.EmployeeId, r.departmentid  
FROM  
           dbo.[Rota] r INNER JOIN
           dbo.[Employee] AS e ON r.EmployeeId = e.EmployeeId
WHERE (r.DateTo >= GETDATE()) " 
        UpdateCommand="UPDATE [Rota] SET [EmployeeId] = (select employeeid from employee where employeename = @EmployeeName),
        [departmentid] = (select departmentid from employee where employeename = @EmployeeName)
         WHERE [DateFrom] = @DateFrom AND [DateTo] = @DateTo AND [DepartmentId] = @DepartmentId">

        <UpdateParameters>
            <asp:Parameter Name="DateTo" Type="DateTime" />
            <asp:Parameter Name="DateFrom" Type="DateTime" />
            <asp:Parameter Name="DepartmentId" Type="Int16" />
            <asp:Parameter Name="EmployeeId" Type="Int16" />
            <asp:Parameter Name="EmployeeName" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
</p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" 
    ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>" 
    onselecting="SqlDataSource4_Selecting" 
    SelectCommand="SELECT [EmployeeName] FROM [Employee] where DepartmentId=@DepartmentId"
    >
    <SelectParameters>
      <asp:ControlParameter ControlID="Rotas" Name="DepartmentId" 
            PropertyName="SelectedValue" Type="Int16" />               
    </SelectParameters>
</asp:SqlDataSource>

真的看不出我做错了什么。如果我不使用“选择参数”而仅使用“从员工中选择员工姓名”,则整个员工列表将显示良好。一旦我尝试使用控制参数,它就会崩溃。帮助! :)

预先感谢您提供的任何帮助。

I'm getting the dreaded 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
error when trying to filter a drop down list in a templatefield using one of the other boundfield values (I'm trying to get a list of employees based on their department - i.e. the user can change the employee but only to another member of the same department).

Here's the code:

<asp:GridView ID="Rotas" runat="server" AllowSorting="True"
DataSourceID="SqlDataSource3" AutoGenerateEditButton="True"  DataKeyNames="DateFrom,DateTo,DepartmentId"
    AutoGenerateColumns="False" OnRowUpdating="Rotas_RowUpdating">
    <Columns>
        <asp:BoundField DataField="DateFrom" HeaderText="DateFrom" ReadOnly="True" 
            SortExpression="DateFrom" />
        <asp:BoundField DataField="DateTo" HeaderText="DateTo" ReadOnly="True" 
            SortExpression="DateTo" />                
        <asp:TemplateField HeaderText="Employee Name" SortExpression="EmployeeName">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" 
                    DataSourceID="SqlDataSource4" DataTextField="EmployeeName" 
                    DataValueField="EmployeeName" SelectedValue='<%# Bind("EmployeeName") %>'>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>

                </asp:DropDownList>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="DepartmentId" HeaderText="DepartmentId" 
            ReadOnly="True" SortExpression="DepartmentId" />
        <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" ReadOnly="False" 
            SortExpression="EmployeeId" />
    </Columns>
</asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>" 
        SelectCommand="SELECT r.DateFrom, r.DateTo, e.EmployeeName, e.EmployeeId, r.departmentid  
FROM  
           dbo.[Rota] r INNER JOIN
           dbo.[Employee] AS e ON r.EmployeeId = e.EmployeeId
WHERE (r.DateTo >= GETDATE()) " 
        UpdateCommand="UPDATE [Rota] SET [EmployeeId] = (select employeeid from employee where employeename = @EmployeeName),
        [departmentid] = (select departmentid from employee where employeename = @EmployeeName)
         WHERE [DateFrom] = @DateFrom AND [DateTo] = @DateTo AND [DepartmentId] = @DepartmentId">

        <UpdateParameters>
            <asp:Parameter Name="DateTo" Type="DateTime" />
            <asp:Parameter Name="DateFrom" Type="DateTime" />
            <asp:Parameter Name="DepartmentId" Type="Int16" />
            <asp:Parameter Name="EmployeeId" Type="Int16" />
            <asp:Parameter Name="EmployeeName" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
</p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" 
    ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>" 
    onselecting="SqlDataSource4_Selecting" 
    SelectCommand="SELECT [EmployeeName] FROM [Employee] where DepartmentId=@DepartmentId"
    >
    <SelectParameters>
      <asp:ControlParameter ControlID="Rotas" Name="DepartmentId" 
            PropertyName="SelectedValue" Type="Int16" />               
    </SelectParameters>
</asp:SqlDataSource>

Really can't see what I'm doing wrong. If I don't use the Select Parameter and just a 'select employeename from employee' then the whole list of employees is displayed fine. As soon as I try and use a controlparameter it falls over. Help! :)

Thanks in advance for any assistance offered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め七分饶幸 2024-09-13 09:13:12

我认为问题在于返回部门员工列表的 SqlDataSource 没有返回任何行,我认为这是因为 ControlParameter 不正确。尽管您通过将其指向 Rotas GridView 和 SelectedValue 属性来完成正确的操作,但 DataKeyNames 属性中使用了三个字段(DateFrom、DateTo、DepartmentId),并且我相信您当前正在将 DateFrom 值传递到您的查询 - 因此,没有结果。
我认为您需要在 ControlParameter 的 PropertyName 而不是 Rotas 的 SelectedValue 属性中使用的是 SelectedDataKey - MSDN 上有详细信息 此处,尽管那里的演示代码不是特别有用。然而,重要的一行是:

如果您正在创建 ControlParameter
对象并想要访问关键字段
除了第一个字段之外,使用
索引的 SelectedDataKey 属性
的 PropertyName 属性
控制参数对象

因此,我认为您需要的是:

<asp:SqlDataSource ID="SqlDataSource4" runat="server" 
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>" 
onselecting="SqlDataSource4_Selecting" 
SelectCommand="SELECT [EmployeeName] FROM [Employee] where DepartmentId=@DepartmentId"
>
<SelectParameters>
  <asp:ControlParameter ControlID="Rotas" Name="DepartmentId" 
        PropertyName="SelectedDataKey[2]" Type="Int16" />               
</SelectParameters>
</asp:SqlDataSource>

如果没有数据的副本,我无法测试它,但尝试一下,看看您会得到什么......

I think the problem is that your SqlDataSource that returns the list of employees for a department isn't returning any rows, and I think this is because the ControlParameter isn't right. Although you've done the right thing by pointing it at the Rotas GridView and the SelectedValue property, there are three fields used in the DataKeyNames property (DateFrom, DateTo, DepartmentId), and I believe you're currently passing the DateFrom value into your query - hence, no results.
What I think you need to use in the PropertyName of the ControlParameter instead of the SelectedValue property of Rotas is the SelectedDataKey - there's details on MSDN here although the demo code there isn't particularly useful. However the important line is:

If you are creating a ControlParameter
object and want to access a key field
other than the first field, use the
indexed SelectedDataKey property in
the PropertyName property of the
ControlParameter object

So from that I think what you need is:

<asp:SqlDataSource ID="SqlDataSource4" runat="server" 
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>" 
onselecting="SqlDataSource4_Selecting" 
SelectCommand="SELECT [EmployeeName] FROM [Employee] where DepartmentId=@DepartmentId"
>
<SelectParameters>
  <asp:ControlParameter ControlID="Rotas" Name="DepartmentId" 
        PropertyName="SelectedDataKey[2]" Type="Int16" />               
</SelectParameters>
</asp:SqlDataSource>

Without a copy of your data I can't test it, but give it a go and see what you get...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文