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

时间:2010-06-23 09:21:55

标签: asp.net gridview sqldatasource controlparameter

我遇到了可怕的'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>

真的看不出我做错了什么。如果我不使用选择参数,只选择“从员工中选择雇员名称”,那么整个员工列表都会正常显示。一旦我尝试使用控制参数,它就会崩溃。救命! :)

提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

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

  

如果要创建ControlParameter   对象并希望访问关键字段   除了第一个字段,使用   索引的SelectedDataKey属性   的PropertyName属性   ControlParameter对象

所以我认为你需要的是:

<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>

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