每行Gridview上的启用/禁用按钮

时间:2014-03-12 18:13:21

标签: c# asp.net gridview buttonclick

我有一个GridView,每个Like都有Dislike ButtonRow

我想要做的是让用户能够click每个RowEnableDisable只有Buttonrow Button当特定clickedSql Table时。{/ p>

我的tblVote Field Name Vote voted。如果用户对该项目有clicks,则会为用户保留一个统计信息。如果第一条记录的用户Dislike Button0,则Vote Column下的itemId 1会写clicks 。如果Like上的用户1,则会为每条记录写一个table等等。我已经把这部分工作了。我如何阅读Button并根据value Vote Field上的tblVote发出Table: ItemId | UserID | Vote 1 | 123 | 0 2 | 123 | 1 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="SwotItemID" AllowPaging="True" AllowSorting="True" DataSourceID="SqlDataSource1" OnRowCommand="GridViewStrength_RowCommand" Width="100%" onrowdatabound="GridView1_RowDataBound"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="ItemDesc" HeaderText="Item Description" SortExpression="ItemDesc"> <ItemStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:TemplateField HeaderText="Like" InsertVisible="False" SortExpression="Vote"> <ItemTemplate> <asp:Button ID="Btn_thumbs_up" runat="server" Text = "Like" CommandName="VoteUp" CommandArgument='<%# Bind("SwotItemID") %>' /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="Dislike" InsertVisible="False" SortExpression="Vote"> <ItemTemplate> <asp:Button ID="Btn_thumbs_down" runat="server" Text = "Dislike" CommandName="VoteDown" CommandArgument='<%# Bind("SwotItemID") %>' /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> </Columns> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <SortedAscendingCellStyle BackColor="#FDF5AC" /> <SortedAscendingHeaderStyle BackColor="#4D0000" /> <SortedDescendingCellStyle BackColor="#FCF6C0" /> <SortedDescendingHeaderStyle BackColor="#820000" /> </asp:GridView> 该州的信息。

{{1}}

1 个答案:

答案 0 :(得分:0)

我认为您应该能够使用foreach循环执行此操作。你可以做点什么......

string vote;

//Your connection string. You don't want to open and close within the foreach because it will open and close for each row. Not ideal
conn.Open();

foreach(GridViewRow gvr in GridView1.Rows)
{
    //you'll want to find the buttons
    Button voteUp = ((Button)gvr.FindControl("Btn_thumbs_up"));
    Button voteDown = ((Button)gvr.FindControl("Btn_thumbs_down"));
    Label SwotItemID = ((Label)gvr.FindControl("SwotItemID));//I would add SwotItemID as a templatefield with an asp label in it in your gridview

    SqlCommand cmdSelectVote= new SqlCommand("SELECT ThumpUpColumn FROM tLoveOrHateTable WHERE SwotItemID = '" + SwotItemID + "'", conn);
    vote = Convert.ToString(cmdSelectVote.ExecuteScalar());


    if(vote == "0")
    {
       voteDown.Enabled = false;
    }
    else if (vote == "1")
    {
       voteUp.Enabled = true;
    }
}
conn.Close();

然后我会在页面加载或按钮或其他东西。无论填充数据如何。这是我先尝试然后从那里开始的。那么它正在做什么就是为每一行运行sql语句。如果它为0,则禁用向下拇指按钮。如果它是1那么它将禁用竖起大拇指按钮。它将逐行进行,所以如果你有1000行(我不会想象你这样做),那么它可能会减慢速度。不要告诉我它会马上工作,因为我没有测试它,我在盒子里写了它。希望这有帮助!