开发者

Iterate through repeater

开发者 https://www.devze.com 2023-01-25 12:28 出处:网络
There\'s this repeater... <asp:Repeater ID=\"myRepeater\" OnItemCommand=\"rpt1_ItemCommand\" runat=\"server\" OnItemDataBound=\"rpt1_OnItemDataBound\">

There's this repeater...

 <asp:Repeater ID="myRepeater" OnItemCommand="rpt1_ItemCommand" runat="server" OnItemDataBound="rpt1_OnItemDataBound">
     <HeaderTemplate>
         <table width=开发者_JAVA技巧"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>

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

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

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

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

    }

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

        }
    }

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消