给DataGrid添加确定删除的功能

来源:互联网 发布:颓然乎其间者的颓然 编辑:程序博客网 时间:2024/05/16 13:49
 

  首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才 移 动∣专 题∣SUN∣IBM∣微 软∣微 创∣精 华∣Donews∣人 邮 我的技术中心  我的分类 我的文档 全部文章 发表文章 专栏管理 使用说明
<script type="text/javascript"><!--google_ad_client = "pub-1076724771190722";google_ad_width = 125;google_ad_height = 125;google_ad_format = "125x125_as";google_ad_channel ="";//--></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1076724771190722&amp;dt=1164211180968&amp;lmt=1145584240&amp;format=125x125_as&amp;output=html&amp;url=http%3A%2F%2Fdev.csdn.net%2Fdevelop%2Farticle%2F20%2F20892.shtm&amp;cc=492&amp;u_h=800&amp;u_w=1280&amp;u_ah=772&amp;u_aw=1280&amp;u_cd=32&amp;u_tz=480&amp;u_his=1" frameborder="0" width="125" scrolling="no" height="125" allowtransparency="allowtransparency"></iframe>

 RSS 订阅  最新文档列表 Windows/.NET .NET  (rss)     Visual C++  (rss)     Delphi  (rss)     Visual Basic  (rss)     ASP  (rss)     JavaScript  (rss)     Java/Linux Java  (rss)     Perl  (rss)     综合 其他开发语言  (rss)     文件格式  (rss)     企业开发 游戏开发  (rss)     网站制作技术  (rss)     数据库 数据库开发  (rss)     软件工程 其他  (rss)    
积极原创作者  tellmenow (22) cutemouse (22) softj (78) iiprogram (69) qdzx2008 (50) goodboy1881 (14) wangchinaking (58) fancyhf (1) harrymeng (41) yjz0065 (113)   CSDN - 文档中心 - .NET 阅读:11493   评论: 9    参与评论     标题   给DataGrid添加确定删除的功能     选择自 cuike519 的 Blog 关键字   DataGrid 出处    

给DataGrid添加确定删除的功能
DataGrid的功能我想大家是知道的,我在实际的应用中遇到如下的问题,客户要求在删除之前做一次提示。类

似于windows。首先我们都知道DataGrid支持删除的功能,我们可以向DataGrid里面添加删除列就可以实现,

下面我想用模板列来实现带提示的删除按钮。我们用northwind的示例数据库作为例子数据库操纵Categories表。

DataGrid的Html页的内容如下:
<asp:DataGrid id="grdTest" style="Z-INDEX: 101; LEFT: 205px; POSITION: absolute; TOP: 134px"

runat="server">
    <Columns>
     <asp:TemplateColumn>
      <ItemTemplate>
       <asp:Button id="btnDelete"

runat="server" Text="Button" CommandName="Delete"></asp:Button>
      </ItemTemplate>
     </asp:TemplateColumn>
    </Columns>
   </asp:DataGrid>
我们只添加了一个模板列,其他的列都是在运行的时候自动生成的。
可以看出这个模板列很像删除列但是又不是删除列,我们给一个普通的Button添加了一个CommandName

="Delete"的属性。这是用来响应DataGrid的ItemCommand事件的!在删除列里面就是这样的!

接下来就是后台代码了,代码如下所示:
private DataSet ds = new DataSet();
private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!this.IsPostBack){
    string strConnection = ConfigurationSettings.AppSettings

["sa"].ToString();
    SqlConnection myConnection = new SqlConnection(strConnection);
    SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT

CategoryID,CategoryName, Description FROM Categories",myConnection);
    myAdapter.Fill(ds);
    this.grdTest.DataSource = ds.Tables[0].DefaultView;
    this.grdTest.DataKeyField = "CategoryID";
    this.grdTest.DataBind();
   }
  }

接下来我们给模板列里面的每一个按钮都添加一个客户端的onclick事件。我想大家都应改知道Attributes属

性吧!可以通过他向客户端输出客户端控件的属性比如:长度、颜色等等。但是通常情况我们使用它添加客户

端事件。知道javascript的朋友肯定知道confirm了!它会弹出一个确认对话框如果确定才提交form否则就不

提交,所以使用这个也是很自然的了。
private void grdTest_ItemDataBound(object sender,

System.Web.UI.WebControls.DataGridItemEventArgs e) {
   switch(e.Item.ItemType){
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
    case ListItemType.EditItem:{
     Button btn = (Button)e.Item.FindControl("btnDelete");
     btn.Attributes.Add("onclick", "return confirm('你是否

确定删除这条记录');");
     break;
    }
   }
  }
添加好这个事件里以后我们还需要添加如下的代码才能完成我们的工作:
private void grdTest_ItemCommand(object source,

System.Web.UI.WebControls.DataGridCommandEventArgs e) {
   if(e.CommandName == "Delete"){
    this.DeleteRow(this.grdTest.DataKeys[e.Item.ItemIndex].ToString

());
   }
  }
上面的事件就是我们点击DataGrid里面的控件的时候激发的事件,我们可以通过CommandName筛选出来我们想

要激发的方法DeleteRow(),一下就是这个方法的代码:
private void DeleteRow(string i){
   string strConnection = ConfigurationSettings.AppSettings["sa"].ToString

();
   SqlConnection myConnection = new SqlConnection(strConnection);
   SqlCommand cmd = new SqlCommand("DELETE FROM Categories WHERE

(CategoryID = "+i+")",myConnection);
   myConnection.Open();
   cmd.ExecuteNonQuery();
   myConnection.Close();
  }
上面的函数接收一个参数,此参数是当前选中行的关键字。
有错误的地方请多多指教。e_mail:wu_jian830@hotmail.com


作者Blog:http://blog.csdn.net/cuike519/
相关文章
如何使CheckBoxList的Attributes属性生效(修改微软的一个bug) TreeView父子联动效果保持节点状态一致 如何有效的使用C#读取文件 如何复制一个目录里面的所有目录和文件 如何使用.NET生成C#源代码 对该文的评论 riman ( 2004-12-26) 这段代码有问题:如果该页的PageIndex为0就会出错 CSDN 网友 ( 2004-10-28) are you ok
CSDN 网友 ( 2004-06-16) 不错 CSDN 网友 ( 2004-06-12) kao michaelowenii ( 2004-05-18) thanks .
【评论】 【关闭】 【报告bug】

#CFContent a:link {color: #000;text-decoration: none;}#CFContent a:visited {color: #000;text-decoration: none;}#CFContent a:hover {color: #F00;text-decoration: underline;}#CFContent a:active {color: #000;text-decoration: none;}#CFBig {width: 760px;clear: both;margin: auto;font-family: Tahoma, sans-serif;text-align: center;background-color: #FFF;}#CFBig #CFContent {margin: auto;width: 500px;text-align: center;}#CFBig #CFContent p {margin: 2px;font-size:9pt; }#CFBig hr {height: 1px;color: #4682B4;}#CFBig #CFBiaoShi {width: 40px;float: right;}

网站简介-广告服务-网站地图-帮助信息-联系方式-English-问题报告

CSDN北京百联美达美数码科技有限公司  版权所有  京 ICP 证 020026 号 CSDN

© 2000-04, CSDN.NET, All Rights Reserved


  
原创粉丝点击