• 推荐
  • 评论
  • 收藏

Asp.Net[C#] 使用GridView 分页,编辑,删除,导出XML完整页面

2022-11-10    1453次浏览

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 id="form1" runat="server">
    <div>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                         OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" AllowPaging="True"
                         OnPageIndexChanging="GridView1_PageIndexChanging" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
                         >
                         
            
            <FooterStyle BackColor="White" ForeColor="#000066" />
            
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
             <Columns>

                <asp:BoundField DataField="log_ID" HeaderText="编号" SortExpression="log_ID" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="log_Title" HeaderText="标题" SortExpression="log_Title" />
                             <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                             <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
             </Columns>
        </asp:GridView>
        
        <div> <asp:Button ID="Button1" runat="server" Text="导出 Excel" OnClick="Button1_Click" /></div>
        
    
    </div>
    </form>
</body>
</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();

    }
}

原文地址:https://www.cnblogs.com/Leo_wl/p/1995123.html