Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GridView“GridView1”触发了未处理的事件,但存在一个事件_C#_Asp.net_Gridview_Webforms - Fatal编程技术网

C# GridView“GridView1”触发了未处理的事件,但存在一个事件

C# GridView“GridView1”触发了未处理的事件,但存在一个事件,c#,asp.net,gridview,webforms,C#,Asp.net,Gridview,Webforms,ı一直在尝试使用webusercontrol、delegate和ADO创建动态控制面板。 即使ı已经编写了用于删除和编辑ı的委托,以面对未处理的GridView“GridView1”触发事件RowDeleteing。问题。有人能帮我吗 这是我的密码 protected void Page_Load(object sender, EventArgs e) { GridView1.DataSource = this.DataSource; GridVi

ı一直在尝试使用webusercontrol、delegate和ADO创建动态控制面板。 即使ı已经编写了用于删除和编辑ı的委托,以面对未处理的GridView“GridView1”触发事件RowDeleteing。问题。有人能帮我吗 这是我的密码

    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = this.DataSource;
        GridView1.DataBind();
        GridView1.DataKeyNames = new string[] { this.DataKeyNames };

    }

    public object DataSource { get; set; }
    public string DataKeyNames { get; set; }

    public event GridHander RowDeleting;
    public event GridHander RowSawing;


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow gvr = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
        int rowIndex = gvr.RowIndex;
        object id = GridView1.DataKeys[rowIndex].Value;

        switch (e.CommandName)
        {
            case "Edit":
                GridView1.EditIndex = rowIndex;
                break;

            case "Delete":
                if (RowDeleting != null)
                {
                    GridEventArgs args = new GridEventArgs() 
                    {
                         row=gvr,
                         id=id,
                         rowIndex=rowIndex
                    };
                    RowDeleting.Invoke(e.CommandSource, args);
                }
                break;

            case"Save":
                if (RowSawing != null)
                {
                    GridEventArgs args = new GridEventArgs() 
                    {
                        row = gvr,
                        id = id,
                        rowIndex = rowIndex
                    };
                    RowSawing.Invoke(e.CommandSource, args);

                }
                GridView1.EditIndex = -1;
                break;

            case "Cancel":
                GridView1.EditIndex = -1;
                break;

            default:
                break;
        }
    }
}
//我的网络表单

ublic partial class CategoryControlPanel : System.Web.UI.Page
{
    CategoryResposite _categoryResposite=new CategoryResposite();

    protected void Page_Load(object sender, EventArgs e)
    { 
        ControlPanel.DataSource = _categoryResposite.ListCategories();
        ControlPanel.RowDeleting += ControlPanel_RowDeleting;
        ControlPanel.RowSawing += ControlPanel_RowSawing;
    }

    void ControlPanel_RowSawing(object sender, GridEventArgs e)
    {
        throw new NotImplementedException();
    }

    void ControlPanel_RowDeleting(object sender, GridEventArgs e)
    {
        _categoryResposite.DeleteCategories(Convert.ToInt32(e.id));   
    }

尝试向方法签名添加受保护的签名。

您发布的代码不完整,缺少aspx文件代码,从您对问题的描述来看,听起来好像您没有将行删除事件分配给GridView1

在aspx文件的打开gridview标记内,按如下方式添加分配:

<asp:gridview ID="..." runat="server" ... OnRowDeleting="<name of event handler>" ...>

GridView的一个优点是它提供了一个内置的CommandField按钮,允许我们执行某些操作,如编辑、更新、删除和选择GridView数据

要添加GridView中提到的那些命令字段,可以执行以下几个步骤: 1.切换到设计视图 2.右键单击GridView并选择->显示智能标记->添加新列 3.在列表中选择CommandField 4.选中“删除”和“编辑/更新选项”,然后单击“确定”

如您所见,编辑和删除命令字段会自动添加到GridView的最后一列中。现在我们可以开始编写代码来编辑和更新GridView中的信息

为了在GridView中执行编辑和更新,我们需要使用三个事件GridView\u行编辑、GridView\u行取消编辑、GridView\u行更新。对于那些不知道如何在GridView中生成事件的人,您可以按照以下步骤操作:

在Visual Studio Designer中切换到“设计”视图 单击GridView 导航到GridView属性窗格,然后切换到事件属性 从那里你可以找到包括上述三个事件在内的事件列表 双击它为您生成事件处理程序
您正在尝试使用删除按钮的命令名Delete。因此gridview会自动创建一个行删除事件


您需要将命令参数从Delete更改为Delete\u Product或您想要的任何内容…

您在代码中的何处设置GridView1.RowDeleting的事件处理程序?我看不出来。我看到您正在为ControlPanel.RowDeleting设置处理程序,ControlPanel是对GridView1的引用吗?如果是,您确定参考是正确的吗?
protected void ControlPanel_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    // cancel the automatic delete action
    e.Cancel = true;

    // do the delete
    _categoryResposite.DeleteCategories(Convert.ToInt32(e.id));

    // complete delete action
    GridView1.DataBind();
}