Sunday, March 27, 2011

Programatic Access to DragHandleTemplate in ASP.NET AJAX ReorderList

Is there a way to programatically access the DragHandleTemplate of a ReorderList (ASP.NET AJAX Control Toolkit) ... Specifically during ItemDataBound for the ReorderList, in order to change its appearance at the per item level?

From stackoverflow
  • You can not access DragHandleTemplate programatically on the server, but if you create the surrounding elements with unique ID's (per row) you should be able the use CSS-selectors or Javascript to only change some of the items.

  • Unfortunately there is no way of getting drag holder from ReorderListItem. Instead, you can create a server control inside DragHandleTemplate (e.g. PlaceHolder) and then find it in your ItemDataBound event handler:

    In aspx file:

    <DragHandleTemplate>
      <div class="dragHandle">
        <asp:Label ID="lblDragHandle" runat="server" />
      </div>
    </DragHandleTemplate>
    

    In aspx.cs file:

    protected void ReorderList1_ItemDataBound(object sender, AjaxControlToolkit.ReorderListItemEventArgs e)
    {
       Label lblDragHandle = (Label)FindControlRecursive(e.Item, "lblDragHandle");
       lblDragHandle.Text = e.Item.ItemIndex.ToString();
    }
    
    private Control FindControlRecursive(Control root, string id)
    {
       if (root.ID == id)
       {
          return root;
       }
    
       foreach (Control c in root.Controls)
       {
          Control t = FindControlRecursive(c, id);
          if (t != null)
          {
             return t;
          }
       }
    
       return null;
    }
    

    I copied FindControlRecursive method from Jeff's blog.

0 comments:

Post a Comment