datagrid-》属性生成器-》列-》添加按钮列-》删除-》文本(T)->在文本框里加上:
删除
不错的办法哦~
下面是一个示例:
<%@ Page language="c#" Codebehind="test1.aspx.cs" AutoEventWireup="false" Inherits="tieba.test3" %>
http://schemas.microsoft.com/intellisense/ie5"; name="vs_targetSchema">
-------------------------------------------------------------
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", "");
//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();
}
}
}
来源:
作者: