通过中继器循环项目 [英] Looping through repeater items

查看:165
本文介绍了通过中继器循环项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是建立像下面的一个中继器:

 < ASP:直放站=服务器ID = rptItemsOnItemDataBound =rptItems_ItemDataBound> 
<&ItemTemplate中GT;
< DIV CLASS =span12灰箱>
< DIV CLASS =英雄BLOCK3>
< DIV CLASS =行秀网>
< DIV CLASS =span9>
< DIV CLASS =英雄内容-3>
< H2>< ASP:文字ID =ltrName=服务器>< / ASP:文字>< / H>
< P>< ASP:文字ID =ltrDescription=服务器>< / ASP:文字>< / P>
< / DIV>
< / DIV>
< DIV CLASS =span3>
< ASP:面板ID =pnlAmount=服务器>
< DIV CLASS =巡回BTNID =divAmount=服务器>
<小>有多少< BR />< /小>
< ASP:文本框=服务器ID =TBOXWIDTH =40>< / ASP:文本框>
< / DIV>
< / ASP:面板>
< / DIV>
< / DIV>
< / DIV>
< / DIV>
< DIV CLASS =明确的两个>< / DIV>
< BR />

< / ItemTemplate中>
< / ASP:直放站>



它使用约束> ListProducts = db.GetDataTable(SELECT * FROM产品哪里身份证件(+ selectedValues +));

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

和则额外的东西与完成的:

 保护无效rptItems_ItemDataBound(对象发件人,
System.Web.UI.WebControls.RepeaterItemEventArgs E)
{
DataRowView的nRow = NULL;

开关(e.Item.ItemType)
{
情况下ListItemType.Item:
情况下ListItemType.AlternatingItem:
nRow =(DataRowView的)如Item.DataItem;
((文字)e.Item.FindControl(ltrDescription))文本=+ nRow [说明]。
((文字)e.Item.FindControl(ltrName))文本=+ nRow [名称]。

如果(+ nRow [HasAmount] ==假)
{
((面板)e.Item.FindControl(pnlAmount))。可见=虚假的;
}

中断;
}
}



不过,现在的页onclick事件,我试图保存存储的信息 - 这是我到目前为止已经完成,但它始终都似乎是空的,我不能.text的等来的结束添加(文本框)item.FindControl(tbSelected);



我的继承人循环我试图在点击:

 保护无效doStageThree(对象发件人,EventArgs五)
{
的foreach(在rptItems.Items的RepeaterItem项)
{
如果(item.ItemType == || ListItemType.Item == item.ItemType ListItemType.AlternatingItem)
{
VAR tbSelected =(文本框)item.FindControl(tbSelected);
VAR lblDescription =(文字)item.FindControl(ltrDescription);
VAR lblName =(文字)item.FindControl(ltrName);

}
}
}


解决方案

它总是空,因为没有文本框 id为 tbSelected

 < ASP:文本框=服务器ID =TBOXWIDTH =40>< / ASP:文本框> 



将其更改为:

  VAR tbSelected =(文本框)item.FindControl(TBOX); 

要防止空使用关键字的代码

  VAR tbSelected = item.FindControl(TBOX)的文本框;如果(!tbSelected = NULL)
{
// id为TBOX文本存在
tbSelected.Text =文本

;
}


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

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

                }
            }
        }

解决方案

It is always null because there is no TextBox with id tbSelected

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

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

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

这篇关于通过中继器循环项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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