Search code examples
c#asp.net

Like and Dislike button in Gridview. How to implement?


I have a gridview that shows User's feedback. Each row has some buttons such as "Like" and "Dislike" and each of which fires gridview RowCommand Event with specific commandname and commandargument.

If the user clicks on the Like button it should disable the like button and leave the dislike button enabled. If the user changes his mind and clicks the Dislike button it should disable the dislike button and enable the Like button and so on. Any ideas?

I tried this and no luck

protected void BtnLike_Click(object sender, EventArgs e)
{
    Button thumbs_up = (Button)sender;
    thumbs_up.Enabled = false;

    Button thumbs_down = (Button)sender;
    thumbs_down.Enabled = true;
}

protected void btnDislike_Click(object sender, EventArgs e)
{
    Button thumbs_down = (Button)sender;
    thumbs_down.Enabled = false;

    Button thumbs_up = (Button)sender;
    thumbs_up.Enabled = true;
}

aspx code

<asp:GridView ID="GridViewFeedback" runat="server" CellPadding="4" ForeColor="#333333"
                        GridLines="None" AutoGenerateColumns="False" DataKeyNames="ItemID" AllowPaging="True"
                        AllowSorting="True" DataSourceID="SqlDataSource1" OnRowCommand="GridViewFeedback_RowCommand"
                        Width="100%" OnRowDataBound="GridViewFeedback_RowDataBound">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>                              
                            <asp:TemplateField HeaderText="Item Description" SortExpression="ItemDesc">                                   
                                <ItemTemplate>
                                    <asp:Label ID="lblDesc" runat="server" Text='<%# Bind("ItemDesc") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Like" InsertVisible="False" SortExpression="Vote">
                                <ItemTemplate>
                                    <asp:Button ID="BtnLike_Click" runat="server" Text="Like" CommandName="VoteUp" OnClick="BtnLike_Click_Click"
                                        CommandArgument='<%# Bind("ItemID") %>' />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Dislike" InsertVisible="False" SortExpression="Vote">
                                <ItemTemplate>
                                    <asp:Button ID="btnDislike_Click" runat="server" Text="Dislike" CommandName="VoteDown" OnClick="btnDislike_Click_Click"
                                        CommandArgument='<%# Bind("ItemID") %>' />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>

                    </asp:GridView>

Solution

  • You can Try Like This

    protected void BtnLike_Click_Click(object sender, EventArgs e)
        {
    
    
            GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
            Button BtnLike_Click = (Button)row.FindControl("BtnLike_Click");
            Button btnDislike_Click = (Button)row.FindControl("btnDislike_Click");
            BtnLike_Click.Enabled = false;
            btnDislike_Click.Enabled = true;
        }
    
        protected void btnDislike_Click_Click(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
            Button BtnLike_Click = (Button)row.FindControl("BtnLike_Click");
            Button btnDislike_Click = (Button)row.FindControl("btnDislike_Click");
            BtnLike_Click.Enabled = true ;
            btnDislike_Click.Enabled = false ;
        }