2015-09-14 59 views
1

我有一個sql數據集表,它有兩個buttonfields(upvote和downvote)。我試圖添加一個commandArgument到這兩個按鈕字段鏈接到sql數據值compName(見下面的代碼),但我不斷收到以下錯誤:sql數據集buttonfield的commandargument

「錯誤6數據綁定表達式只支持具有DataBinding事件,System.Web.UI.WebControls.ButtonField沒有DataBinding事件。「

這裏是我的C#代碼來構建表:

protected void searchTheDB() 
{ 
    string s = "SELECT compName As 'Company/Organization Name', btcAddr As 'Bitcoin Address', Premium_User as 'Premium User'," + 
    "upvote as 'Upvotes',downvote As 'Downvotes' FROM clientDataTable WHERE compName LIKE '%" + searchBox.Text + "%'"; 

    try 
    { 
     SqlConnection forSearch = new SqlConnection(connectionString); 
     SqlDataAdapter search = new SqlDataAdapter(s, forSearch); 
     DataSet dB = new DataSet(); 
     search.Fill(dB); 
     searchGridView.DataSource = dB; 
     searchGridView.DataBind(); 
     searchBox.Text = String.Empty; 
    } 
    catch (SqlException exp) 
    { 
     throw new InvalidOperationException("Sorry, the website is experiencing difficulties, please try again, error: ", exp); 
    } 
} 

這裏是Asp.net代碼:

<asp:GridView ID="searchGridView" runat="server" CellPadding="10" ForeColor="#333333" GridLines="None" Height="161px" Width="935px" CellSpacing="5" HorizontalAlign="Justify" BorderStyle="Solid" OnRowCommand="searchGridView_RowCommand"> 
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
        <Columns> 
         <asp:ButtonField ButtonType="Button" CommandName="Upvote" Text="Upvote" CommandArgument='<%#Eval("compName")%>'/> 
         <asp:ButtonField ButtonType="Button" CommandName="Downvote" Text="Downvote" CommandArgument='<%#Eval("compName")%>'/> 
        </Columns> 

        <EditRowStyle BackColor="#999999" /> 
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
        <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
        <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
        <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
       </asp:GridView> 

的問題是在ButtonField字段commandArguments

<asp:ButtonField ButtonType="Button" CommandName="Upvote" Text="Upvote" CommandArgument='<%#Eval("compName")%>'/> 
         <asp:ButtonField ButtonType="Button" CommandName="Downvote" Text="Downvote" CommandArgument='<%#Eval("compName")%>'/> 
        </Columns> 

非常感謝您的幫助!

回答

1

一個asp:ButtonField沒有CommandArgument財產

你應該去自定義方式與TemplateField

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:Button ID="DownButton" runat="server" CommandName="Down" CommandArgument='<%#Eval("compName")%>' Text="Down"> </asp:Button> 
     <asp:Button ID="UpButton" runat="server" CommandName="Up" CommandArgument='<%#Eval("compName")%>' Text="Up"> </asp:Button> 
    </ItemTemplate> 
</asp:TemplateField>