2010-01-22 19 views
0

所以,我有一个ListView,其中包含一个过程中的步骤。左边有一个标签,简单说明它是哪一步,右边是带说明的文本框。然后在该文本框的右侧是通常的编辑和删除按钮,但我也有一个向上箭头和一个向下箭头。如果点击,我希望将当前的一组项目移入该插槽。试图重新排列我的列表视图,但不知道如何获得我需要编辑的属性

这个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(); 

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

回答

2

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

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

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

编辑 - 添加进一步明晰

假设你有一个List<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(); 

} 
+0

嗯,感谢您的输入,但有没有办法像现在这样做?我真的没有使用过中继器,而且我已经把更多时间放在学习/设置上。 – Justen 2010-01-23 03:05:23

+0

每个项目是否有独特的pk值?如果是这样,您可以将上下按钮的CommandArgument绑定到该值。然后在onClick事件中,获取值并执行ListView.Items。[button.CommandArgument] .StepNumber ++;这应该与ListView一起工作。 – 2010-01-23 13:58:40

+0

不知道什么是pk值。 – Justen 2010-01-23 20:31:11

相关问题