Aspx页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Manage_News_Default" EnableEventValidation = "false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form ></html>
Aspx.cs 代码:
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; using System.IO;
public partial class Manage_News_Default : System.Web.UI.Page { OleDbConnection sqlcon; OleDbCommand sqlcom; string strCon = ConfigurationManager.AppSettings["DB"].ToString(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); }
//删除 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlstr = "delete from blog_Article where log_ID=" + Convert.ToInt16( GridView1.DataKeys[e.RowIndex].Value.ToString()) + ""; sqlcon = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(strCon)); sqlcom = new OleDbCommand(sqlstr, sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); bind(); } //更新 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { sqlcon = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(strCon));
string sqlstr = "update blog_Article set log_Title='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() //+ "',字段2='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "' where log_ID=" + Convert.ToInt16( GridView1.DataKeys[e.RowIndex].Value.ToString() )+ ""; sqlcom = new OleDbCommand(sqlstr, sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); GridView1.EditIndex = -1; bind(); }
//取消 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); }
//绑定 public void bind() { string sqlstr = "select * from [blog_Article]"; sqlcon = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(strCon)); OleDbDataAdapter myda = new OleDbDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, "表"); GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "log_ID" };//主键 GridView1.DataBind(); sqlcon.Close(); sqlcon.Dispose();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; bind(); } public override void VerifyRenderingInServerForm(Control control) { } protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls"); //转化为其他 后缀名改以下 Response.ContentEncoding = System.Text.Encoding.UTF7; //;字符集格式 Response.ContentType = "application/ms-excel"; //转化为其他 如 excel 改为 Word
StringWriter oStringWriter = new StringWriter(); HtmlTextWriter OHtmlTextWriter = new HtmlTextWriter(oStringWriter); GridView1.AllowPaging = false; GridView1.Columns[GridView1.Columns.Count - 1].Visible = false; //不显示删除 GridView1.Columns[GridView1.Columns.Count - 2].Visible = false; //不显示编辑 bind();
GridView1.RenderControl(OHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End();
} }
|
请发表评论