【问题标题】:Getting value in footer template from code behind-Repeater从代码后面的代码中获取页脚模板中的价值-Repeater
【发布时间】:2010-03-03 22:07:09
【问题描述】:

我有一个这样的中继器:

<asp:Repeater runat="server" ID="pde">
<HeaderTemplate></HeaderTemplate>

<ItemTemplate>
<asp:Literal runat="server" ID="literal1"></asp:Literal>
</ItemTemplate>

<FooterTemplate><asp:Literal runat="server" ID="literal2"></asp:Literal></FooterTemplate>
</asp:Repeater>

现在在literal2中,我想从后面的代码中获取值。但是我无法在后面的代码中获取literal2。知道如何得到这个吗?

【问题讨论】:

    标签: asp.net repeater


    【解决方案1】:

    您应该能够通过访问转发器中的最后一个RepeaterItem 来访问它,这将是您的页脚。然后使用FindControlRepeaterItem 上搜索您要查找的任何控件。

    使用上面的示例:

    Literal controlYouWant = pde.Controls[pde.Controls.Count - 1].FindControl("literal2") as Literal;
    

    您可以将语句分解为:

    // This will get you the footer
    RepeaterItem item = pde.Controls[pde.Controls.Count - 1];
    // From here you finds any control you want within the RepeaterItem.
    Literal controlYouWant = item.FindControl("literal2") as Literal;
    

    【讨论】:

    • 上述语句 pde.Controls[pede.Controls.Count-1].FindControl...正在引发异常...我想知道,因为在我的页脚模板中我有这样的
      但我尝试了 pde.Controls.Count-2 ......它也没有工作异常
  • 我认为模板中的其他内容并不重要。我使用您刚刚使用&lt;/li&gt;&lt;/div&gt; 发布的相同HTML 对其进行了测试,它仍然可以正常工作。确保您绑定到至少一个项目的Datasource。将每个项目分解为单独的代码行并从那里调试以准确查看失败的原因并回发。
  • 是的,我明白了,非常感谢....错误是我在将数据项绑定到转发器之前这样做了,所以这就是为什么当时没有控件并且它显示错误。我需要在我在中继器中执行 DataBind 后使用它 ....Wokrs 完美...谢谢...
  • 【解决方案2】:

    首先搜索repeater中的项目,只取页脚项目类型并丢弃其他项目。看看下面的代码。

    foreach(RepeaterItem item in repeterName.Controls) 
    { 
        if (item.ItemType != ListItemType.Footer) continue; 
        var lblMyLabel = ((Label)item.FindControl("lblMyLabel")); 
        lblMyLabel.Text = "I found it!!!"; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      相关资源
      最近更新 更多