SQL数据集按钮字段的命令

本文关键字:命令 字段 按钮 数据集 SQL | 更新日期: 2023-09-27 18:12:41

我有一个sql数据集表,其中有两个按钮字段(upvote和downvote)。我试图添加一个命令参数到这两个按钮字段链接到sql数据值compName(见下面的代码),但我一直得到以下错误:

"Error 6 Databinding表达式只在有Databinding事件的对象上被支持。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命令参数

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

谢谢你的帮助!

SQL数据集按钮字段的命令

a 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>