简体   繁体   中英

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>

First, you need to know on what row you are, then do what you need on this row :

void GridViewFeedback_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    GridViewRow gvr = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
    Button btnUp = gvr.FindControl("BtnLike_Click");
    Button btnDown = gvr.FindControl("btnDislike_Click");

    if(e.CommandName == "VoteUp")
    {
        btnUp.Enabled = false;
        btnDown.Enabled = true;
    }
    else if(e.CommandName == "VoteDown")
    {
        btnUp.Enabled = true;
        btnDown.Enabled = false;
    }
}    

Make sure you handle the RowCommand event of the GridView !

Disclaimer : I haven't tested the code, so their might be syntax error..

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

This works.

protected void ButtonLike_Click_Click(object sender, EventArgs e)
{
    GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
    Button ButtonLike_Click = (Button)row.FindControl("ButtonLike_Click");
    Button ButtonDislike_Click = (Button)row.FindControl("ButtonDislike_Click");
    ButtonLike_Click.Enabled = false;
    ButtonDislike_Click.Enabled = true;
}

protected void ButtonDislike_Click_Click(object sender, EventArgs e)
{
    GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
    Button ButtonLike_Click = (Button)row.FindControl("ButtonLike_Click");
    Button ButtonDislike_Click = (Button)row.FindControl("ButtonDislike_Click");
    ButtonLike_Click.Enabled = true ;
    ButtonDislike_Click.Enabled = false ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM