Need help with repeater

This is my repeater:

<asp:Repeater ID="myRepeater" OnItemCommand="myRepeater_ItemCommand" runat="server" OnItemDataBound="myRepeater_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>

What I want is that when user clicks on any of
the “lnk1” link button in the list that repeater renders,
the link should be replaced with the label “label1”..
i.e. when the user clicks on “Make Default” link, it should be replaced with “Yes” label

Calling this method obj.SetDefaultAddress(); is setting the default address in the DB alright..
problem is with the display of the label1 and lnk1 when the repeater renders…

what is happening is that BOTH “Make Default” LinkButton and the “YES” label are getting displayed
under the “Default” column of the table inside my repeater.

I want some code that will check the “IsDefault” value in my DB and display “Make Default ” link button

and “YES” label accordingly… i.e. if IsDefault’s value in the DB is TRUE then “YES” should be displayed in the repeater
otherwise “Make Default”

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Are you sure your piece of code in code behind under ItemCommand is executing?
I only changed the CommandName from SetDefault to SetDefaultAddress in aspx file to match with the one in code behind, it worked.

Method 2

Where to start…

I think what’s causing your problem is that the SelectedItem and the DefaultAddress are not mapped to each other, so when you click the button you’re getting the selected index set and the OnItemDatabound event is showing/hiding what you want, but when the grid is initialized from the database, the SelectedItem is not being set.

I don’t know what your datasource is, and there’s obviously more code to this than what you’ve posted, but if you can look at the e.Item.DataItem in the myRepeater_ItemDataBound handler, you can set the current item as selected when the address is the default (e.Item.ItemType... or use your "selectedIndex" counter)

Method 3

I will probably do it from markup itself – this is assuming that you have “IsDefault” column/property of bit/boolean type in your data-source indicating the address is default. So use following markup:

...
<tr>
   <td>
       <asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible='<%# !Eval("IsDefault") %>' CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
       <asp:Label ID="label1" Text="Yes" runat="server" Visible='<%# !Eval("IsDefault") %>'></asp:Label>
    </td>
</tr>
...

You need to control visibility based on property in your data source (either using markup or ItemDataBound event). Also when SetDefault link is clicked, you must either re-bind the repeater new state or toggle visibility explicitly (as your current code is doing).

EDIT:
If data binding expression are not working then you have to do it in ItemDataBound event. I see that you have already tried that but there is one mistake – bllUsers obj=new bllUsers(); will always have IsDefault as false – you need to use data item. For example,

protected void myRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
           bllUsers obj = e.Item.DataItem as bllUsers;
           ((Label)e.Item.FindControl("ldefault")).Visible = obj.isDefault; 
           ((Button)e.Item.FindControl("btnMakeDefault")).Visible = ! obj.isDefault; 
        }
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x