views:

208

answers:

2

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?

A: 

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.

Dan Diplo
I'm sorry, I guess I wasn't clear. This is my first post on here and I'm just getting back into the .NET world. The ReorderList will be replacing the ListBoX that shows the currently "selected" items from the databound gridview. The GridView is not going away, only the ListBox hopefully.
adamweeks
I understand and I'd like to help, but am still not quite sure what you are trying to do. Are you saying that you want a user to be able to select a row in a GridView and that item then appears in a ReorderList? After they have finished and re-ordered what do you want to happen?
Dan Diplo
Yes, that's exactly what I want to happen. Once they are done with the ReorderList, I will pass the data to a webservices application to do some calculations on it, then return that data back to the user. (That part is all working now with the ListBox)
adamweeks
A: 

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?

Dan Diplo
thanks! I've been reading up on viewstate, which I haven't had to deal with yet, but this looks like it should do what I'm wanting.
adamweeks