C# 循环遍历中继器项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16606208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:26:24  来源:igfitidea点击:

Looping through repeater items

c#repeater

提问by TMB87

I have a repeater which is built like the following:

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

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

It's bound using:

它绑定使用:

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

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

And then extra stuff is done with:

然后额外的东西完成了:

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

However, now on an onclick event for the page, i'm trying to save the information stored - This is what i've done so far, but it always all seems to be null, and I can't add a .text etc to the end of the (TextBox)item.FindControl("tbSelected");

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

Heres my loop i'm trying on click:

这是我尝试点击的循环:

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");

                }
            }
        }

采纳答案by gzaxx

It is always null because there is no TextBoxwith id tbSelected

它始终为空,因为没有TextBoxwith idtbSelected

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

change it to:

将其更改为:

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

To protect your code from null use keyword as:

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

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

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

回答by Mikey Mouse

Try replacing

尝试更换

    foreach (RepeaterItem item in rptItems.Items)

with

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