我个人会在这种情况下使用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();
}
嗯,感谢您的输入,但有没有办法像现在这样做?我真的没有使用过中继器,而且我已经把更多时间放在学习/设置上。 – Justen 2010-01-23 03:05:23
每个项目是否有独特的pk值?如果是这样,您可以将上下按钮的CommandArgument绑定到该值。然后在onClick事件中,获取值并执行ListView.Items。[button.CommandArgument] .StepNumber ++;这应该与ListView一起工作。 – 2010-01-23 13:58:40
不知道什么是pk值。 – Justen 2010-01-23 20:31:11