using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DataGrid : System.Web.UI.Page
{
private static string Ids="";
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
BindData("select * from myTable");
}
private void BindData(string sql)
{
GridV.DataSource =GetDataSet(sql);
GridV.DataBind();
}
protected void GridV_RowEditing(object sender, GridViewEditEventArgs e)
{
GridV.EditIndex = e.NewEditIndex;
BindData("select * from myTable");
SetBindedClientScript(e);
}
protected void GridV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridV.EditIndex = -1;
BindData("select * from myTable");
}
private void SetBindedClientScript(GridViewEditEventArgs e)
{
string js = @"<script language=javascript>";
js += "<!-- function SetJs() {";
js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".focus();";
js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".size=10;";
js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".maxlength=10;";
js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[2].Controls[0].UniqueID + ".attachEvent('onclick',isOK);";
js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[3].Controls[0].UniqueID + ".size=10;";
js += "};";
js += "setTimeout(SetJs,1);";
js += "//--></script>";
if (!this.IsStartupScriptRegistered("clientjs"))
this.RegisterStartupScript("clientjs", js);
}
protected void GridV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton del=(LinkButton)e.Row.Cells[4].Controls[2];
if(del.Text.ToString()=="删除")
del.Attributes["onclick"] = "return confirm('确定要删除?')";
}
}
protected void GridV_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string aId=GridV.DataKeys[e.RowIndex].Value.ToString();
if (Ids == "")
Ids += aId;
else
Ids += "," + aId;
string sql = "select * from myTable where id not in(" + Ids.ToString()+")";
BindData(sql);
}
/// <summary>
///得到DATASET
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns></returns>
protected DataSet GetDataSet(string sql)
{
string StrCon = "";
StrCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/Web/FileSite/aSite/DB/aDb.mdb;Persist Security Info=False";
OleDbConnection Conn = new OleDbConnection(StrCon);
OleDbCommand Cmd = Conn.CreateCommand();
Cmd.CommandText =sql;
Conn.Open();
//Cmd.ExecuteNonQuery();
OleDbDataAdapter Adpt = new OleDbDataAdapter();
Adpt.SelectCommand = Cmd;
DataSet dst = new DataSet();
Adpt.Fill(dst);
Conn.Close();
return dst;
}
}