【问题标题】:Programatic Access to DragHandleTemplate in ASP.NET AJAX ReorderList以编程方式访问 ASP.NET AJAX 重新排序列表中的 DragHandleTemplate
【发布时间】:2010-09-22 21:38:36
【问题描述】:

有没有办法以编程方式访问 ReorderList(ASP.NET AJAX 控件工具包)的 DragHandleTemplate ... 特别是在 ReorderList 的 ItemDataBound 期间,以便在每个项目级别更改其外观?

【问题讨论】:

    标签: asp.net ajaxcontroltoolkit reorderlist


    【解决方案1】:

    您不能以编程方式访问 DragHandleTemplate 在服务器上,但是如果您创建具有唯一 ID(每行)的周围元素,您应该能够使用 CSS 选择器或 Javascript 仅更改一些物品。

    【讨论】:

      【解决方案2】:

      不幸的是,没有办法从 ReorderListItem 获取拖拽架。相反,您可以在 DragHandleTemplate(例如 PlaceHolder)中创建一个服务器控件,然后在您的 ItemDataBound 事件处理程序中找到它:

      在 aspx 文件中:

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

      在 aspx.cs 文件中:

      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;
      }
      

      我从 Jeff 的博客中复制了 FindControlRecursive 方法。

      【讨论】:

        【解决方案3】:

        您也可以在 LINQ 中表达 FindControlRecursive:

                private Control FindControlRecursive(Control root, string id)
                {
                    return root.ID == id
                               ? root
                               : (root.Controls.Cast<Control>().Select(c => FindControlRecursive(c, id)))
                                     .FirstOrDefault(t => t != null);
                }
        

        【讨论】:

          猜你喜欢
          • 2012-08-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-16
          • 1970-01-01
          • 2017-03-25
          • 2011-01-31
          相关资源
          最近更新 更多