2015-09-14 26 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>