C# GridView 'GridView1' 触发了未处理的事件 RowDeleting,但有一个事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15856463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The GridView 'GridView1' fired event RowDeleting which wasn't handled , but there is an event
提问by Burak Can
? have been traying to create dynamic control panel using webusercontrol , delegate,and ADO. Even ? have write the delegate for deleting and editing ? faced with "The GridView 'GridView1' fired event RowDeleting which wasn't handled."problem . Can anybody help me pls here is my codes
? 一直在尝试使用 webusercontrol、delegate 和 ADO 创建动态控制面板。甚至 ?是否编写了用于删除和编辑的委托?面临“未处理的 GridView 'GridView1' 触发事件 RowDeleting。”问题。谁能帮我请这是我的代码
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));
}
回答by Burak Can
Try adding protected to the method signatures.
尝试将 protected 添加到方法签名中。
回答by FastGeek
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. Ensure that you re-bind the gridview after delete so that the changes are shown on postback.
如果事件处理程序 ControlPanel_RowDeleting 旨在处理从 gridview 操作中删除,则将其作为事件处理程序名称插入。确保在删除后重新绑定 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();
}
回答by queen
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 按钮,它允许我们执行某些操作,例如编辑、更新、删除和选择 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. Now we can start to write our codes for editing and updating the information in the GridView.
如您所见,Edit 和 Delete CommandField 自动添加到 GridView 的最后一列中。现在我们可以开始编写代码来编辑和更新 GridView 中的信息。
In-order to perform Edit and Update in GridView we need to use three events ( 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 中执行编辑和更新,我们需要使用三个事件(GridView_RowEditing、GridView_RowCancelingEdit、GridView_RowUpdating)。对于那些不知道如何在 GridView 中生成事件的人,您可以按照以下步骤操作:
- Switch to Design View in Visual Studio Designer
- Click on the GridView
- Navigate to the GridView Property Pane and then SWITCH to Event Properties
- From there you would be able to find the list of events including those three events mentioned above
- Double Click on that to generate the Event handler for you
- 在 Visual Studio 设计器中切换到设计视图
- 单击网格视图
- 导航到 GridView 属性窗格,然后切换到事件属性
- 从那里您将能够找到包括上述三个事件在内的事件列表
- 双击它为您生成事件处理程序
回答by Abdullah Ibn Mannan
You are trying with a command name of Deletefor your delete button. So the gridview creates a row deleting event automatically....
您正在尝试使用的命令名称删除你的删除按钮。所以 gridview 会自动创建一个行删除事件....
You need to change the command argument from Deleteto something else like Delete_Productor whatever you want...
您需要将命令参数从Delete更改为其他内容,例如Delete_Product或您想要的任何内容...