この場合、私は個人的にリピーターを使用し、それを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();
}