电脑网络: 全部 | 电脑诊所 | 图形图象 | 数据库 | IT论坛 | 硬件DIY | Web开发 | 编程语言 | 网络技术  
主题:DataGrid点击删除按钮弹出确认对话框一个好办法
摘要
关键词
  发送私信

datagrid-》属性生成器-》列-》添加按钮列-》删除-》文本(T)->在文本框里加上:
删除

不错的办法哦~
 
下面是一个示例:
<%@ Page language="c#" Codebehind="test1.aspx.cs" AutoEventWireup="false" Inherits="tieba.test3" %>


 
  
  
  
  
  http://schemas.microsoft.com/intellisense/ie5"; name="vs_targetSchema">
 
 
  
   
         runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
     CellPadding="3" AutoGenerateColumns="False" Width="800px" Height="248px">
     
     
     
     
     
      
       
      

      
       
        http://x.yeeyoo.com/ViewPage/"; + DataBinder.Eval(Container.DataItem,"Path")+".html" %>' Target="_blank" ID="Hyperlinkid1">
         <%# DataBinder.Eval(Container.DataItem,"标题") %>
        

       

      

      
       
      

      
       
      

      
       
      

      删除

"
       HeaderText="删除" CommandName="Delete">
       
      
     
     
    
         ms_positioning="FlowLayout">48小时内发布的前100个热门帖子:

         Width="248px" Height="32px">操作状态:
 

-------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO ;

//using tieba.Controls;
using tieba.Controls.Article;

namespace tieba
{
 /// 


 /// test1 的摘要说明。
 /// 

 public class test3 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(! IsPostBack)
   {BindGrid();   }
   
  }

  private void BindGrid()
  {
   string sql="SELECT top 100 id as ID,Title as 标题,CatalogID,PostDate as 发帖日期,PostUserName as 发帖人,HitCount as 点击 " +
    " ,rtrim(REPLICATE('0',5-len(convert(char(5),[CatalogID])))+convert(char(5),[CatalogID]))+'/'+rtrim(REPLICATE('0',7-len(convert(char(7),[id])))+convert(char(7),[id])) as Path " +
       "FROM dbo.tb_tie WHERE  (DATEDIFF(hh,PostDate,GETDATE())<64) ORDER by HitCount DESC";
   SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["sqlconnectionString"]);
   DataSet ds=new DataSet();
   SqlDataAdapter da=new SqlDataAdapter (sql,conn);
   da.Fill (ds);
   //ds.Tables [0].PrimaryKey =new DataColumn[]{ds.Tables [0].Columns("ID")};
   DataView dw=ds.Tables [0].DefaultView ;
   int count=ds.Tables [0].Rows.Count;
   long[] ArrID=new long[count];
   for(int i=0;i    ArrID[i]=(int)ds.Tables [0].Rows [i][0];
   Session["ArrID"]=ArrID;
   
   //Label1.Text  =ds.Tables [0].Rows [3][0].ToString ();
   DataGrid1.DataSource =dw;
   DataGrid1.DataBind ();
  }


  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// 


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// 

  private void InitializeComponent()
  {    
   this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_Delete);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  

  

  public void DataGrid1_Delete(Object sender, DataGridCommandEventArgs e)
  {
   long[] ArrID=(long[])Session["ArrID"];
   long id=ArrID[e.Item.ItemIndex];
   Label1.Text="Record Deleted ID=" + id.ToString ();
   Page.RegisterStartupScript("alert", "window.alert('警告:你已经删除记录ID:"+id.ToString ()+"');");
   //Tie.DeleteArticle (id);
     
   
   string deleteCmd = "DELETE from tb_tie where id = @Id";
   SqlConnection myConnection=new SqlConnection(ConfigurationSettings.AppSettings["sqlconnectionString"]);
   SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
   myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int ));
   myCommand.Parameters["@Id"].value = ArrID[e.Item.ItemIndex] ;//DataGrid1.DataKeys[(int)e.Item.ItemIndex];

   myCommand.Connection.Open();

   try
   {
    myCommand.ExecuteNonQuery();
    //Message.InnerHtml = "Record Deleted
" + deleteCmd;
    
    Label1.Text="Record Deleted ID=" + ArrID[e.Item.ItemIndex].ToString ();
   }
   catch (SqlException)
   {
    //Message.InnerHtml = "ERROR: Could not delete record";
    //Message.Style["color"] = "red";
    Label1.Text="ERROR: Could not delete record";
   }

   myCommand.Connection.Close();

   BindGrid();
  }

  

 }
}


来源:  作者:
点击数: | 回复数: | 时间:2007-11-2 11:47:03
共有评论 
快速回复
游客身份发表,或者输入大名:
支持HTML语法,限250字内 
     
请根据下图中的字符输入验证码:
Powered by pwmis.com  技术支持