首页  ·  知识 ·  云计算
vs.net2005生成HTML
佚名  本站原创  综合  编辑:dezai  图片来源:网络
列表及分面产效果: 先贴前台添加数据页面 ----------------------------

列表及分面产效果:

先贴前台添加数据页面
------------------------------------数据填加页面(AddData.aspx)-----------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddData.aspx.cs" Inherits="AddData" %>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://www.w3.org/1999/xhtml" >

    无标题页


   
   


       
           
               
               
           
           
               
               
           
           
               
               
           
           
               
               
           
           
               
               
           
       

                    姓名:

                   

                    电话:

                   

                    腾讯:

                   

                    备注:

                   

               

                   

       

   
   

       

       

         
   



---------------------------------------后台代码(AddData.aspx.cs)-----------------------------------------------

using System;
using System.Data;
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 AddData : System.Web.UI.Page
{
    protected Hover hover = new Hover();

    int pagesize = 4; 
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAddDate_Click(object sender, EventArgs e)
    {
        string s = hover.WriteFile(this.txtUserName.Text,this.txtNetQQ.Text,this.txtTelphone.Text,this.txtMemo.Text);
        if (s != "err")
        {
            string sql = "insert into t(name,qq,tel,memo) values('" + this.txtUserName.Text+ "','" + this.txtNetQQ.Text + "','" + this.txtTelphone.Text+ "','" + this.txtMemo.Text + "')";
            if (hover.StrQuery(sql))
            {
                this.lblMessage.Text = "操作成功!";
                this.lblMessage.Text = "

Html生成成功

请点击查看连接页面!";
            }
            else
            {
                this.lblMessage.Text = "HTML生成错误!";
            }
        }

    }
    protected void btnAutoPage_Click(object sender, EventArgs e)
    {
        string pageContent;
        int i = 0;
        int n = 0;
        int t = 0;
        int x;
        int pageNum = 5;
        int pageNumSize;
        string page;

    }
}

---------------------------------------------类文件(Hover.cs)-----------------------------------------

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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.Text;
using System.IO;


///


/// Hover 的摘要说明
///

public class Hover
{
    SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString());
    SqlCommand comm = null;
 public Hover()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    public string WriteFile(string strName, string strTel, string strQQ, string StrContent)
    {
        string path = HttpContext.Current.Server.MapPath("~/html/");
        Encoding code = Encoding.GetEncoding("utf-8");
        //读取模板文件
        string temp = HttpContext.Current.Server.MapPath("~/Templates/Text.html");
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";
        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd();
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write("");
            HttpContext.Current.Response.End();
            return "err";
        }
        finally
        {
            sr.Close();
        }

        string htmlFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";

        //替换内容
        //这是模板文件已经读到STR变量当中
        str=str.Replace("XingMing",strName);
        str=str.Replace("DianHua",strTel);
        str = str.Replace("QQHao", strQQ);
        str = str.Replace("BeiZhu",StrContent);
        //写文件
        try
        {
            sw = new StreamWriter(path + htmlFileName, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("");
            HttpContext.Current.Response.End();
            return "err";
        }
        finally
        {
            sw.Close();   
        }

        return htmlFileName;
    }

    public bool WriteList(string list, string page, int i)
    {
        string path = HttpContext.Current.Server.MapPath("~/Html/Pagelist/");//存储位置
        Encoding code = Encoding.GetEncoding("utf-8");

        //读取模板
        string temp = HttpContext.Current.Server.MapPath("~/Templates/list.html");
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";
        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("");
            HttpContext.Current.Response.End();
            return false;
        }
        finally
        {
            sr.Close();
        }

        string htmlFileName="List_"+i+".html";
        //替换内容
        str = str.Replace("$List",list);
        str = str.Replace("$Page",page);
        //写文件
        try
        {
            sw = new StreamWriter(path + htmlFileName, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write("");
            HttpContext.Current.Response.End();
            return false;
        }
        finally
        {
            sw.Close();
        }

        return true;
    }

    public DataView opentable(string strSql)
    {
        try
        {
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(strSql, Conn);
            sda.Fill(ds, "mydate");
            return ds.Tables[0].DefaultView;
        }
        finally
        {
        Conn.Close();
        }
    }

    public SqlDataReader GetReader(string strSql)
    {
        try
        {
            comm = new SqlCommand(strSql);
            comm.Connection = Conn;
            comm.Connection.Open();
            return comm.ExecuteReader(CommandBehavior.CloseConnection);
        }
        finally
        {
            comm.Dispose();
            Conn.Close();
        }
    }

    public bool StrQuery(string strSql)
    {
        try
        {
            comm = new SqlCommand(strSql, Conn);
            comm.Connection.Open();
            comm.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("");
            HttpContext.Current.Response.End();
            return false;
        }
        finally
        {
            comm.Dispose();
            Conn.Close();
            Conn.Dispose();
        }
        return true;
    }
}

本文作者:佚名 来源:本站原创
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读