一大早起來編程序,在經(jīng)過慎重選擇之后用HyperLink解決了RowEdit的問題??墒墙又霈F(xiàn)了一個刪除操作的問題,無論我如何修改、設(shè)置,總是提示我:激發(fā)了未處理的事件“RowDeleting”。即使我把自帶的CommandField刪除按鈕換作我自己添加的ButtonField刪除按鈕,同樣都定義了RowDeleting事件,可是問題依然如此,反復(fù)出現(xiàn)這樣的提示。
微軟的MSDN上面提供了相應(yīng)的事件介紹,還有事例程序代碼,可是即使我按照上面的代碼寫,依然還是這個錯誤,好像根本找不到我定義的函數(shù)。
-------------------------------
void CustomersGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
// Cancel the delete operation if the user attempts to remove
// the last record from the GridView control.
if (CustomersGridView.Rows.Count <= 1)
{
e.Cancel = true;
Message.Text = "You must keep at least one record.";
}
}
--------------------------------
google了一下,發(fā)現(xiàn)遇到同樣問題的還是挺多的,基本上情況和我相同,都不是用直接綁定ObjectDataSource數(shù)據(jù)源的方式,而是采用動態(tài)綁定數(shù)據(jù)的??磥聿煌耆梦④浀臇|西就會出問題啊,微軟太霸道了。網(wǎng)上的回帖大多都是不了了之,也不知道孰是孰非,反正覺得有用的都記下來吧,也許我真的是犯了很小的錯誤,就像下面的朋友一樣,忘了注冊一下而已。
①代碼應(yīng)該沒問題,難道事件沒有注冊
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting" >
②處理方法有2種:
(1)如果你不需要這個事件,那么刪除
GridView2.RowDeleting += .....
這一行.
(2)如果需要這個事件,則添加處理該事件的方法,
方法的具體格式去查MSDN中關(guān)于RowDeleting的幫助.
③private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(dgShow.Items.Count==1)
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string strSql = "delete from tbStudentinfo where studentid= "+e.Item.Cells[0].Text+ " ";
ExecuteSql(strSql);
BindData();
}這是刪除的全碼
④如果是用Button刪除的話,這里就是有Button刪除的代碼
private void btnDelete_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl( "cbSelect ");
if(cb.Checked)
{
//以下執(zhí)行刪除操作
int nID = int.Parse(dgi.Cells[0].Text);
string strSql = "delete from tbStudentinfo where studentid= "+nID;
ExecuteSql(strSql);
}
}
dgShow.CurrentPageIndex = 0;
BindData();
}
⑤
把下面的代碼
<asp:Button ID="Button3" runat="server" Text="刪除用戶" CommandArgument='<%#DataBinder.eval_r(Container.DataItem,"UserID") %>' CommandName="delete" />
修改為:
<asp:Button ID="Button3" runat="server" Text="刪除用戶" CommandArgument='<%#DataBinder.eval_r(Container.DataItem,"UserID") %>' CommandName="del" />
commandName屬性的值不要等于“delete”就可以了。因為該值(“delete”)是微軟的默認(rèn)值,它默認(rèn)會觸發(fā)RowDeleting事件。