简体   繁体   English

GridView'GridView1'触发了未处理的事件RowDeleting,但是有一个事件

[英]The GridView 'GridView1' fired event RowDeleting which wasn't handled , but there is an event

ı have been traying to create dynamic control panel using webusercontrol , delegate,and ADO. ı一直在使用webusercontrol,委托和ADO创建动态控制面板。 Even ı have write the delegate for deleting and editing ı faced with "The GridView 'GridView1' fired event RowDeleting which wasn't handled."problem . 甚至我已经编写了删除和编辑委托的程序,遇到了“未处理的GridView'GridView1'触发事件RowDeleting。”问题。 Can anybody help me pls here is my codes 有人可以帮我吗,这是我的密码

    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;
        }
    }
}

//My webform //我的网络表单

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));   
    }

The code that you have posted is incomplete (missing the aspx file code), from your description of the problem it sounds as though you have not assigned the RowDeleting event to GridView1. 您发布的代码不完整(缺少aspx文件代码),从对问题的描述来看,好像您没有将RowDeleting事件分配给GridView1一样。

Inside the opening gridview tag in the aspx file add the assignment as follows: 在aspx文件的打开的gridview标记内,按如下所示添加分配:

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

If the event handler ControlPanel_RowDeleting is designed to handle a delete from gridview action then insert this as the event handler name. 如果事件处理程序ControlPanel_RowDeleting设计为处理从gridview动作中删除的事件,则将其插入为事件处理程序名称。 Ensure that you re-bind the gridview after delete so that the changes are shown on postback. 确保删除后重新绑定gridview,以便更改显示在回发中。

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();
}

You are trying with a command name of Delete for your delete button. 您正在尝试使用命令名称Delete来删除按钮。 So the gridview creates a row deleting event automatically.... 因此,gridview自动创建一个行删除事件。

You need to change the command argument from Delete to something else like Delete_Product or whatever you want... 您需要将命令参数从Delete更改为Delete_Product之类的东西或您想要的任何东西...

One of the good things about GridView is that it provides a built-in CommandField Buttons which allows us to perform certain actions like editing, updating,deleting and selecting of GridView data. GridView的优点之一是它提供了一个内置的CommandField Button,它使我们能够执行某些操作,例如GridView数据的编辑,更新,删除和选择。

To add those command fields mentioned in the GridView you can follow these few steps below: 1. Switch to Design View 2. Right Click on the GridView and Select --> Show Smart Tag --> Add New Columns 3. On the List Select CommandField 4. Check Delete and Edit/Update options then OK 要添加GridView中提到的那些命令字段,您可以按照以下几步操作:1.切换到设计视图2.右键单击GridView并选择->显示智能标签->添加新列3.在列表中选择CommandField 4.选中“删除”和“编辑/更新”选项,然后单击“确定”

As you can see the Edit and Delete CommandField are automatically added in the last column of GridView. 如您所见,Edit和Delete CommandField会自动添加到GridView的最后一列中。 Now we can start to write our codes for editing and updating the information in the GridView. 现在,我们可以开始编写代码以编辑和更新GridView中的信息。

In-order to perform Edit and Update in GridView we need to use three events ( GridView_RowEditing, GridView_RowCancelingEdit , GridView_RowUpdating). 为了在GridView中执行编辑和更新,我们需要使用三个事件(GridView_RowEditing,GridView_RowCancelingEdit和GridView_RowUpdating)。 For those who do not know on how to generate Events in GridView you can follow these steps below: 对于那些不知道如何在GridView中生成事件的人,可以按照以下步骤操作:

  1. Switch to Design View in Visual Studio Designer 切换到Visual Studio Designer中的设计视图
  2. Click on the GridView 单击GridView
  3. Navigate to the GridView Property Pane and then SWITCH to Event Properties 导航到GridView属性窗格,然后切换到事件属性
  4. From there you would be able to find the list of events including those three events mentioned above 从那里您将能够找到事件列表,包括上述三个事件
  5. Double Click on that to generate the Event handler for you 双击它为您生成事件处理程序

尝试在方法签名中添加protected。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM