分享

GridView.RowDeleting?事件

 suweixin 2011-01-15

GridView.RowDeleting 事件

(2008-04-22 14:35:15)

    一大早起来编程序,在经过慎重选择之后用HyperLink解决了RowEdit的问题。可是接着出现了一个删除操作的问题,无论我如何修改、设置,总是提示我:激发了未处理的事件RowDeleting”。即使我把自带的CommandField删除按钮换作我自己添加的ButtonField删除按钮,同样都定义了RowDeleting事件,可是问题依然如此,反复出现这样的提示。

    微软的MSDN上面提供了相应的事件介绍,还有事例程序代码,可是即使我按照上面的代码写,依然还是这个错误,好像根本找不到我定义的函数。

-------------------------------

  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了一下,发现遇到同样问题的还是挺多的,基本上情况和我相同,都不是用直接绑定ObjectDataSource数据源的方式,而是采用动态绑定数据的。看来不完全用微软的东西就会出问题啊,微软太霸道了。网上的回帖大多都是不了了之,也不知道孰是孰非,反正觉得有用的都记下来吧,也许我真的是犯了很小的错误,就像下面的朋友一样,忘了注册一下而已。

①代码应该没问题,难道事件没有注册
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting" >

②处理方法有2种:
(1)如果你不需要这个事件,那么删除
GridView2.RowDeleting += .....
这一行.
(2)如果需要这个事件,则添加处理该事件的方法,
方法的具体格式去查MSDN中关于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)
{
//以下执行删除操作
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”)是微软的默认值,它默认会触发RowDeleting事件。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多