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 Language="C#" AutoEventWireup="true" CodeBehind="something.cs"
    ValidateRequest="false" Inherits="something" EnableViewState="true" %>

Page loadイベントでは、次のことを行います。

if(!IsPostback)
{
    callmethodtobindgrid();
}

Rowcommand イベント ハンドラ:

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 に答える