sql数据集按钮字段的命令参数

时间:2015-09-14 08:58:17

标签: c# asp.net sql-server data-binding

我有一个sql数据集表,它有两个buttonfields(upvote和downvote)。我正在尝试将commandArgument添加到这两个按钮区域,这两个按钮区域链接到sql数据值compName(请参阅下面的代码),但我不断收到以下错误:

"错误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 个答案:

答案 0 :(得分: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>