Kentico 的 webpart control ID in value in a repeater and transformation

Kentico's webpart control ID in value in a repeater and transformation

我正在构建一个基于 bootstrap 的手风琴。它几乎就在那里,除了我需要用一个带有唯一 ID 的标签来包装每个手风琴。我的想法是使用中继器控制 ID。那么我如何从转换中访问它,以及 HTML 信封?

这是转发器HTML的信封

<div class="accordion" id="askUsAccordion">
  
  
</div>

这是我的转换代码

<div class="panel panel-default">
  <div class="panel-heading">
    <h4 class="panel-title">
      <a href="#accordionPanel<%# DataItemIndex + 1 %>" data-toggle="collapse" data-parent="#"><%# DataItemIndex + 1 %> <%# Eval("Heading") %></a>
    </h4>    
  </div>  
  <div id="accordionPanel<%# DataItemIndex + 1 %>" class="panel-collaspe collapse" role="tabpanel" aria-labeledby="panel<%# DataItemIndex + 1 %>">
    <div class="panel-body">
      <%# Eval("Panel") %>
    </div>
  </div> 
</div>  

马克,不确定这是最好的解决方案,但它应该适合你。像这样将服务器端功能添加到您的转换中:

<script runat="server">
  protected string GetID()
    {      
      Control parent = this;      
      while ( (!(parent is CMSWebParts_Viewers_Documents_cmsrepeater)) && 
             (parent != null))
      {
        parent = parent.Parent;
      }      
      return (parent as CMSWebParts_Viewers_Documents_cmsrepeater).WebPartID;
    }
</script>

并在您的转换中调用此方法,如下所示:

<%# GetID() %>

虽然这不是我的首选方法,但我写了一个快速的 js 片段。我尽量避免使用过多的 JS。

  /* Accordions */
  // we first detect if there is an accordion in the DOM, and if see we ensure that each is with it's own names space
  if ($accordion.length){
    // we need the ID of each accordion on the page which then becomes the data-parent value, which is needed to ensure we can isolate accordions
    $accordion.each(function(i,v){
      var $this = $(this),
          $id = $this.attr('id');
      // loop through each accordion panel
      $this.children('.panel').each(function(){
        var $that = $(this);
        $('.panel-title-link', $that).attr('data-parent', $id);
      });
    });
  }

为什么不直接使用 Repeater 的 ClientID?

尝试 <%# Container.ClientID %> 在这种情况下,容器应该引用转换 运行 开启的转发器。