0

因此,我有一个 ListView,其中包含流程中的步骤。左边有一个标签,简单地说明它是哪一步,右边是一个带有说明的文本框。然后在该 TextBox 的右侧是通常的编辑和删除按钮,但我也有一个向上箭头和一个向下箭头。如果单击,我希望将当前的一组项目移动到该插槽中。

此 ListView 由 LinqDataSource 绑定,如果我可以从单击按钮的集合中访问项目的属性,我可以调用 ListView.DataBind() 并且它会自行排序。

我正在谈论的属性是标签中的内容,说明它是哪一步。我的设置是这样的:

<asp:Label ID="lblStepNumber" runat="server" Text='<%# Eval( "StepNumber", "Step #{0}" ) %>' />

所以如果我能做类似的事情

ListView.Items.[Where_btn_clicked].StepNumber++;
ListView.Items.[Where_btn_clicked+1].StepNumber--;
ListView.DataBind();

那将是最简单的,但我不知道如何访问此属性。

4

1 回答 1

2

在这种情况下,我个人会使用中继器并将其绑定到您的 LinqDataSource。

然后,您可以处理 OnItemDataBound 事件并获取e.Item.DataItem每一行的对象。获取对向上和向下按钮的引用,并将按钮e.Item.FindControl("btnUP") as Button的命令参数设置为 DataItem 的序列号。

然后在按钮的 OnClick 事件中,使用 CommandArgument 重新排序和更新您的 LinqDataSource - 然后重新绑定中继器以显示更改。

编辑 - 增加清晰度

假设您有 aList<Employee>作为数据源,并且 Employee 对象定义为

public class Employee
{
    int EmployeeID;
    int PlaceInLine; // value indicating the sequence position
    string Name;
}

您的向上和向下按钮可以在您的 ListView 中定义,如下所示:

    <asp:Button ID="btnUpButton" runat="server" 
CommandArgument='<%#Eval("EmployeeID") %>' OnClick="btnUp_Click" />

单击按钮时,您可以处理该事件 - 这假设您将员工列表作为私有变量访问:

private List<Employee> _Employees;

protected void btnUp_Click(object sender, EventArgs e)
{
    Button btnUp = sender as Button;
    int employeeID = int.Parse(btnUp.CommandArgument); // get the bound PK
    Employee toMoveUp = _Employees.Where(e=>e.EmployeeID == employeeID).FirstOrDefault();
    // assuming PlaceInLine is unique among all employees...
    Employee toMoveDown = _Employees.Where(e=>e.PlaceInLine == toMoveUp.PlaceInLine + 1).FirstOrDefault();

    // at this point you need to ++ the employees sequence and
    // -- the employee ahead of him  (e.g. move 5 to 6 and 6 to 5)

    toMoveUp.PlaceInLine ++;
    toMoveDown.PlaceInLine --;

    // save the list 
    DataAccessLayer.Save(_Employees);
    //rebind the listivew
    myListView.DataBind();

}
于 2010-01-22T22:08:31.020 回答