开发者

Need help with repeater

开发者 https://www.devze.com 2023-01-26 09:45 出处:网络
This is my repeater: <asp:Repeater ID=\"myRepeater\" OnItemCommand=\"myRepeater_ItemCommand\" runat=\"server\" OnItemDataBound=\"myRepeater_OnItemDataBound\">

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" colum开发者_如何学Gon 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"


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.


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)


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

精彩评论

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

关注公众号