I have a gridview bound to a sql server datasource. I am currently moving selected items to a ListBox to show chosen records. I'd like to switch to the ReOrderList from the AJAX Toolkit to be able to reorder the selected items. Unfortunately, the ReorderList needs a true datasource to bind to. What is the best practice for creating some sort of temporary table that the user would use?
I don't follow. The ReOrderList has a DataSourceID property to which you can just point at your existing SqlDataSource. Or do you not actually have an SqlDataSource control?
If not, how are you binding the data to your GridView? If you are binding to some kind of object colllection then you could use an ObjectDataSource instead, so long as it implements IList interface. Perhaps have a read of ReorderList - bind to DataTable if you are trying to bind to a DataTable instead.
OK, what you could do is persist your temporary list datasource in viewstate. Here's a rough example:
<asp:ScriptManager ID="SM1" runat="server"></asp:ScriptManager>
<ajaxToolkit:ReorderList ID="RList" runat="server"
DragHandleAlignment="Left" ItemInsertLocation="End"
AllowReorder="true" ShowInsertItem="true" PostBackOnReorder="false">
<ItemTemplate>
<p><%# Eval("ID") %> = <%# Eval("Name") %></p>
</ItemTemplate>
</ajaxToolkit:ReorderList>
<asp:Button ID="ButtonAdd" runat="server" OnClick="ButtonClick_AddItem" Text="Add New" />
Then in codebehind:
public partial class SortList : System.Web.UI.Page
{
[Serializable]
public class MyItem
{
public Guid Id { get; set; }
public string Name { get; set; }
public MyItem(Guid id, string name)
{
Id = id;
Name = name;
}
}
protected List<MyItem> MyList
{
get
{
if (ViewState["myClass"] == null)
ViewState["myClass"] = new List<MyItem>();
return (List<MyItem>)ViewState["myClass"];
}
}
protected void AddItem(Guid id, string name)
{
MyList.Add(new MyItem(id, name));
RList.DataSource = MyList;
RList.DataBind();
}
protected void ButtonClick_AddItem(object sender, EventArgs e)
{
AddItem(Guid.NewGuid(), DateTime.Now.Ticks.ToString());
}
}
Obviously you would substitute MyItem class with whatever you want to store and replace the button with the GridView select item event. But hopefully the principle is there?