2014-03-12 57 views
0

我有一個GridView,每個中有一個Like和一個Dislike Button每行Gridview上的啓用/禁用按鈕

我想要做的是用戶能夠click只有那些每和EnableDisablerowButton時特別Buttonclicked之一。

我有一個Sql TabletblVote有一個FieldName Vote。這爲用戶保留一個符號,如果他們有voted該項目。如果用戶clicksDislike Button爲第一條記錄,它將寫入0VoteColumn下的itemId1。如果用戶clicksLike上,它會爲每條記錄編寫一個1等等。我已經有這部分工作。我怎樣才能從table讀取,併發出Button的狀態取決於valuetblVote上的Vote Field

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

可能重複的[啓用和禁用鏈接按鈕上gridview](http://stackoverflow.com/questions/17207271/enable-and-disable-link-button-on-gridview) – Malachi

回答

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行(我不認爲你會這樣做),那麼它可能會減慢速度。不要抱着我,它會立即工作,因爲我沒有測試它,我寫在盒子裏。希望這可以幫助!

+0

我應該添加這在rowdatabound – Apollo

+0

你可以試試看看是否有效。你也可以把它放在它自己的方法中,並在頁面加載中調用它。 – Humpy