Gridview中的Like和Dislike按钮。如何实施?

时间:2014-03-17 17:38:27

标签: c# asp.net

我有一个显示用户反馈的gridview。每行都有一些按钮,如“Like”和“Dislike”,每个按钮都会触发gridview RowCommand Event,并带有特定的commandname和commandargument。

如果用户点击“赞”按钮,则应禁用“喜欢”按钮并启用“不喜欢”按钮。如果用户改变主意并单击“不喜欢”按钮,则应禁用“不喜欢”按钮并启用“喜欢”按钮,依此类推。有什么想法吗?

我试过这个但没有运气

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代码

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

3 个答案:

答案 0 :(得分:2)

首先,你需要知道你在哪一行,然后在这一行做你需要的东西:

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

确保您处理RowCommand的{​​{1}}事件!

免责声明:我没有测试过代码,因此它们可能是语法错误..

答案 1 :(得分:1)

你可以尝试这样

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

答案 2 :(得分:0)

这很有效。

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