为什么页脚项目不包含在Repeater.Items中? [英] Why is the footer-item not included in Repeater.Items?

查看:95
本文介绍了为什么页脚项目不包含在Repeater.Items中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从按钮的OnClick事件中的FooterTemplate内的文本框中获取值.我的第一个想法是遍历中继器上的项属性,但是正如您在此示例中所看到的,它仅包含实际的数据绑定项,而不包括页脚项.

I need to get a value from a textbox inside a FooterTemplate in the OnClick event of a button. My first thought was to loop through the items-property on my repeater, but as you can see in this sample, it only includes the actual databound items, not the footer-item.

ASPX:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        Item<br />
    </ItemTemplate>
    <FooterTemplate>
        Footer<br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Code-behind.cs:

Code-behind.cs:

protected void Page_Load(object sender, EventArgs e)
{
    ListItemCollection items = new ListItemCollection();
    items.Add("value1");
    items.Add("value2");
    Repeater1.DataSource = items;
    Repeater1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine(Repeater1.Items.Count);
}

此代码将仅输出"2"作为计数,那么如何在页脚模板中引用我的文本框?

This code will only output "2" as the count, so how do I get to reference my textbox inside the footertemplate?

推荐答案

来自

From the MSDN documentation, the Items is simply a set of RepeaterItems based off the DataSource that you are binding to and does not include items in the Header or FooterTemplates.

如果要引用文本框,则可以从中继器获得ItemDataBound事件的引用,您可以在其中测试页脚.

If you want to reference the textbox, you can get a reference on ItemDataBound event from the repeater where you can test for the footer.

例如

private void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
{

  if (e.Item.ItemType == ListItemType.Footer) 
  {
    TextBox textBox = e.Item.FindControl("TextBox1") as TextBox;
  }
}    

这篇关于为什么页脚项目不包含在Repeater.Items中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆