locked
Repeater controls dynamic display RRS feed

  • Question

  • User-569896929 posted

    Hi

    I have database of users,items and bookmarks tables.

    When user bookmarks an item it stores user id and item id into bookmarks table

    I have a repeater that shows many items list using data binding and it have an bookmark button on it.

    If the item is already bookmarked by logged in user it should be of green color else red color.

    How can i achieve this using repeater control.

    Thursday, April 23, 2015 8:22 AM

Answers

  • User61956409 posted

    Hi Nithin,

    Thanks for your post.

    If the item is already bookmarked by logged in user it should be of green color else red color.

    You could dynamically set item’s background color in Repeater ItemDataBound Event, You code may look like this.

    <ItemTemplate>
        <tr runat="server" id="itemrow">
            <td>
                <asp:Label runat="server" ID="lblId" Text='<%# Eval("Id") %>' />
            </td>
            <td>
                <asp:Label runat="server" ID="lblItemId" Text='<%# Eval("ItemId") %>' />
            </td>
            <td>
                <asp:Label runat="server" ID="lblUserId" Text='<%# Eval("UserId") %>' />
            </td>
        </tr>
    </ItemTemplate> 
    
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType== ListItemType.Item)
        {
            string uid = ((Label)e.Item.FindControl("lblUserId")).Text.ToString();
    
            if (uid == loginuid)
            {
                ((HtmlTableRow)e.Item.FindControl("itemrow")).BgColor = "green";
            }
            else
            {
                ((HtmlTableRow)e.Item.FindControl("itemrow")).BgColor = "red";
            }
        }
                
    } 
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 24, 2015 1:43 AM

All replies

  • User-492460945 posted

    Hi,

    If you maintain user in the bookmarks table, then in databound event of repeater, check whether the logged in user is already bookmarked the corresponding item are not.

    Thursday, April 23, 2015 8:39 AM
  • User61956409 posted

    Hi Nithin,

    Thanks for your post.

    If the item is already bookmarked by logged in user it should be of green color else red color.

    You could dynamically set item’s background color in Repeater ItemDataBound Event, You code may look like this.

    <ItemTemplate>
        <tr runat="server" id="itemrow">
            <td>
                <asp:Label runat="server" ID="lblId" Text='<%# Eval("Id") %>' />
            </td>
            <td>
                <asp:Label runat="server" ID="lblItemId" Text='<%# Eval("ItemId") %>' />
            </td>
            <td>
                <asp:Label runat="server" ID="lblUserId" Text='<%# Eval("UserId") %>' />
            </td>
        </tr>
    </ItemTemplate> 
    
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType== ListItemType.Item)
        {
            string uid = ((Label)e.Item.FindControl("lblUserId")).Text.ToString();
    
            if (uid == loginuid)
            {
                ((HtmlTableRow)e.Item.FindControl("itemrow")).BgColor = "green";
            }
            else
            {
                ((HtmlTableRow)e.Item.FindControl("itemrow")).BgColor = "red";
            }
        }
                
    } 
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 24, 2015 1:43 AM