【问题标题】:Looping through repeater items循环遍历中继器项目
【发布时间】:2013-05-17 09:54:19
【问题描述】:

我有一个中继器,其结构如下:

 <asp:Repeater runat="server" ID="rptItems" OnItemDataBound="rptItems_ItemDataBound">
            <ItemTemplate>
            <div class="span12 grey-box">
                        <div class="hero-block3">
                            <div class="row show-grid">
                                <div class="span9">
                                    <div class="hero-content-3">
                                        <h2><asp:Literal ID="ltrName" runat="server"></asp:Literal></h2>
                                        <p><asp:Literal ID="ltrDescription" runat="server"></asp:Literal></p>
                                    </div>
                                </div>
                                <div class="span3">
                                <asp:Panel ID="pnlAmount" runat="server">
                                    <div class="tour-btn" id="divAmount" runat="server">
                                        <small>How Many?<br /></small>
                                        <asp:TextBox runat="server" ID="tbox" Width="40"></asp:TextBox>
                                    </div>
                                    </asp:Panel>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="clear-both"></div>
                    <br />

            </ItemTemplate>
        </asp:Repeater>

绑定使用:

  ListProducts = db.GetDataTable("select * from Products where Id in (" + selectedValues + ")");

        rptItems.DataSource = ListProducts;
        rptItems.DataBind();

然后额外的东西完成了:

protected void rptItems_ItemDataBound(object sender,
                                  System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        DataRowView nRow = null;

        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                nRow = (DataRowView)e.Item.DataItem;
                ((Literal)e.Item.FindControl("ltrDescription")).Text = "" + nRow["Description"];
                ((Literal)e.Item.FindControl("ltrName")).Text = "" + nRow["Name"];

                if ("" + nRow["HasAmount"] == "False")
                {
                    ((Panel)e.Item.FindControl("pnlAmount")).Visible = false;
                }

                break;
        }
    }

但是,现在在页面的 onclick 事件中,我正在尝试保存存储的信息 - 这是我到目前为止所做的,但它似乎总是为空,我无法添加.text 等到(TextBox)item.FindControl("tbSelected"); 的末尾

这是我正在尝试点击的循环:

protected void doStageThree(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rptItems.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    var tbSelected = (TextBox)item.FindControl("tbSelected");
                    var lblDescription = (Literal)item.FindControl("ltrDescription");
                    var lblName = (Literal)item.FindControl("ltrName");

                }
            }
        }

【问题讨论】:

  • 尝试“foreach (Control c in rptItemss.Items)”而不是RepeaterItems,然后是((TextBox)c.FindControl("tbSelected")).Text
  • 看起来它可以工作,如果 (TextBox)c.FindControl("tbSelected") 为空,我将如何保护(现在总是显示)
  • var text = (c.FindControl("tbSelected")== null ? "Empty" : ((TextBox)c.FindControl("tbSelected")).Text; 使用调试器和看看什么有效

标签: c# repeater


【解决方案1】:

它始终为空,因为没有 ID 为 tbSelectedTextBox

<asp:TextBox runat="server" ID="tbox" Width="40"></asp:TextBox>

改成:

var tbSelected = (TextBox)item.FindControl("tbox");

为了保护您的代码免受 null 使用关键字 as:

var tbSelected = item.FindControl("tbox") as TextBox;

if (tbSelected != null)
{
   //textbox with id tbox exists
   tbSelected.Text = "your text";
}

【讨论】:

  • 不过,话说 - tbselected always = ""
  • PostBack事件之后检查你是否没有在Page_Load内重新绑定你的转发器,因为它会清除所有值,经常发生:)
  • 如果我有多个文本框(可能有 10 个)并且我想将某些文本框添加到列表中怎么办?
【解决方案2】:

尝试替换

    foreach (RepeaterItem item in rptItems.Items)

    foreach (Control c in rptItems.Items)
    {
        if(c.FindControl("tbSelected") != null)
        {
           var selectedText = ((TextBox)c.FindControl("tbSelected")).Text;
        }
    }

【讨论】:

  • 是的,正如 gzaxx 指出的那样,没有名为 tbSelected 的下一个框,它在您的示例代码中称为 tBox,它应该有一个更具描述性的名称,例如 tbHowManyItems
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
相关资源
最近更新 更多