通过中继器迭代 [英] Iterate through repeater

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

问题描述

有此中继...

 <asp:Repeater ID="myRepeater" OnItemCommand="rpt1_ItemCommand" runat="server" OnItemDataBound="rpt1_OnItemDataBound">
     <HeaderTemplate>
         <table width="99%" border="0" cellpadding="0" cellspacing="0">
             <tr class="lgrey">
                <td>Default</td>
             </tr>
     </HeaderTemplate>
     <ItemTemplate>
         <table>
             <tr>
                <td>
                    <asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible="True" CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
                    <asp:Label ID="label1" Text="Yes" runat="server" Visible="False"></asp:Label>
                </td>
             </tr>
     </ItemTemplate>
     <FooterTemplate>
         </table>
     </FooterTemplate>
</asp:Repeater>

我要的是,当用户点击任何一个lnk1链接按钮,在lsit该中继器呈现,
链接应与标签LABEL1..被替换即当用户点击设为默认值链接,就应及时更换
是的标签

What I want is that when user clicks on any of the "lnk1" link button in the lsit that repeater renders, the link should be replaced with the label "label1"..ie when the user clicks on "Make Default" link, it should be replaced with "Yes" label

现在,当我点击2链接按钮,都得到他们的标签是,显示在哪里,我想只有一个链接按钮显示是
即其已在项目clciked和其余部分的应显示设为默认链接按钮而已。

Now when I click 2 link buttons, both get their label "Yes" displayed where as I want only one link button to display Yes ie the one which has been clciked and rest of the items should display "Make Default" link button only.

即只有一个项目应显示是的标签......现在我怎么通过中继项目重复设置只有一个项目
为默认值,而不是多??

ie Only ONE item should be displaying "Yes" label...now how do I iterate through the repeater items to set only ONE item as default and not multiple ??

推荐答案

您可以遍历中继物品收集,

You can iterate the repeater items collection,

protected void myRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{

    int selectedIndex = e.Item.ItemIndex;
    foreach(RepeaterItem item in myRepeater.Items)
    {
        ((LinkButton)item.FindControl("lnk1")).Visible = (item.ItemIndex != selectedIndex);
        ((Label)item.FindControl("label1")).Visible = (item.ItemIndex == selectedIndex);
    }
}

此选项的优点是:1,没有第二个撞数据库

The pros of this option are: 1. no second hit on the database.

或者,我会把我的逻辑ItemDataBound事件相反,点击链接按钮的索引存储在一个成员变量,并调用DataBind在命令事件处理程序。

Or I would put my logic in the ItemDataBound event instead, store the clicked link button index in a member variable and call DataBind in the command event handler.

private int selectedIndex = -1;
//...
protected void myRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        selectedIndex = e.Item.ItemIndex;

        myRepeater.DataSource = MyGetDataMethod();
        myRepeater.DataBind();        

    }

在处理的ItemDataBound比较当前指数与存储的索引,如果他们匹配显示的标签。

In the ItemDataBound handler compare the current index with the stored index and if they match show the label.

protected void myRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if(e.Item.ItemIndex == selectedIndex)
            {
               ((LinkButton)e.Item.FindControl("lnk1")).Visible = false;
               ((Label)e.Item.FindControl("label1")).Visible = true;
            }

        }
    }

第二个选项的缺点是:1。第二次打击的数据库。 2.如果用户单击说两排,和其他一些用户插入一个新的地址记录,第2行现在可能是不同的东西,当你重新绑定。此外,如果你使用的不是为了通过数据库调用和存储的的selectedIndex之间会发生变化可能是无效的thatway了。

The cons of this second option are: 1. A second hit on the database. 2. If the user clicks say row two, and some other user inserts a new address record, row 2 may now be something different when you re-bind. Also if you're not using an order by that could change between database calls and your stored selectedIndex could be invalidated thatway too.

所以在最后我会与方案一走,现在我想过它所有的方式通过。

So in conclusion I'd go with option one now I've thought it all the way through.

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

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