循环遍历重复项

2024-01-27

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

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

                }
            }
        }

它始终为空,因为没有TextBox带身份证tbSelected

<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";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

循环遍历重复项 的相关文章

随机推荐