0

这是我的网格视图

<asp:GridView ID="GridView1" runat="server" 
          AutoGenerateColumns="False" 
          OnRowDeleting="GridView1_RowDeleting"
          OnRowEditing="GridView1_RowEditing"
          OnRowUpdating="GridView1_RowUpdating"
          OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:BoundField DataField="name" HeaderText="name" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btn1" Text="edit" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="Edit" ></asp:LinkButton>
                <asp:LinkButton ID="Button1" Text="delete" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="Delete" OnClientClick=' return confirm("do you want to delete")' ></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是函数调用

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Response.Write(e.CommandArgument.ToString());

    if (e.CommandName == "Delete")
    {
        //Response.Write(e.CommandArgument);
        MySqlConnection conn = new MySqlConnection(connectionString);
        conn.Open();

        string query = "delete from brand where id='" + e.CommandArgument + "'";
        MySqlCommand cmd = new MySqlCommand(query,conn);

        cmd.ExecuteNonQuery();
        conn.Close();

        fillgrid();
    }
}

onrowcommand不会被解雇。为什么?

4

2 回答 2

0

只是猜测:因为您的Page_Load外观与此类似:

protected void Page_Load(Object sender, EventArgs e)
{
    DataBindGridView(); // here you load the datasource of the grid and call DataBind();
}

不要DataBind在回发时使用网格,否则不会触发事件,因为您使用值表单数据库覆盖更改,因此这应该可以工作:

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
        DataBindGridView(); // here you load the datasource of the grid and call DataBind();
}
于 2013-09-04T09:11:14.763 回答
0

启用视图状态:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="something.cs"
    ValidateRequest="false" Inherits="something" EnableViewState="true" %>

在这种情况Page load下,请执行以下操作:

if(!IsPostback)
{
    callmethodtobindgrid();
}

行命令事件处理程序:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Response.Write(e.CommandArgument.ToString());

    if (e.CommandName == "Delete")
    {
        //Response.Write(e.CommandArgument);
        MySqlConnection conn = new MySqlConnection(connectionString);
        conn.Open();

        string query = "delete from brand where id='" + e.CommandArgument + "'";
        MySqlCommand cmd = new MySqlCommand(query, conn);
        cmd.ExecuteNonQuery();
        conn.Close();

        fillgrid();
    }
}
于 2013-09-04T10:26:14.733 回答