![](/img/trans.png)
[英]The GridView 'OrdersGridView' fired event RowDeleting which wasn't handled
[英]The GridView 'GridView1' fired event RowDeleting which wasn't handled , but there is an event
ı一直在使用webusercontrol,委托和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;
}
}
}
//我的網絡表單
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文件代碼),從對問題的描述來看,好像您沒有將RowDeleting事件分配給GridView1一樣。
在aspx文件的打開的gridview標記內,按如下所示添加分配:
<asp:gridview ID="..." runat="server" ... OnRowDeleting="<name of event handler>" ...>
如果事件處理程序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();
}
您正在嘗試使用命令名稱Delete來刪除按鈕。 因此,gridview自動創建一個行刪除事件。
您需要將命令參數從Delete更改為Delete_Product之類的東西或您想要的任何東西...
GridView的優點之一是它提供了一個內置的CommandField Button,它使我們能夠執行某些操作,例如GridView數據的編輯,更新,刪除和選擇。
要添加GridView中提到的那些命令字段,您可以按照以下幾步操作:1.切換到設計視圖2.右鍵單擊GridView並選擇->顯示智能標簽->添加新列3.在列表中選擇CommandField 4.選中“刪除”和“編輯/更新”選項,然后單擊“確定”
如您所見,Edit和Delete CommandField會自動添加到GridView的最后一列中。 現在,我們可以開始編寫代碼以編輯和更新GridView中的信息。
為了在GridView中執行編輯和更新,我們需要使用三個事件(GridView_RowEditing,GridView_RowCancelingEdit和GridView_RowUpdating)。 對於那些不知道如何在GridView中生成事件的人,可以按照以下步驟操作:
嘗試在方法簽名中添加protected。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.