⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 default.aspx.cs

📁 用C#+Access写的留言本
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.OleDb;
using System.Drawing;
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;

//该源码下载自www.51aspx.com(51aspx.com)

public partial class _Default : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {
        Label1.Visible = false;
        Label2.Visible = true;
        if (Session["Admin"] == "admin")
        {
            Label1.Visible = true;
            Label2.Visible = false;
        }
        else
        {
            Label1.Visible = false;
            Label2.Visible = true;
        }
        if (!Page.IsPostBack)
        {
            strpage();
            ItIsFace();
        }

    }

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Panel Panel4 = (Panel)e.Item.FindControl("Panel4");
        Panel4.Visible = true;
    }

    public void ItIsFace()
    {
        string currentDir, fileName, imgDir;
        FileInfo fi;
        currentDir = Server.MapPath(".") + "\\Images\\GG";
        imgDir = "Images\\GG";

        DirectoryInfo dir = new DirectoryInfo(currentDir);
        foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
        {
            if (fsi is FileInfo)
            {
                fi = (FileInfo)fsi;
                fileName = fi.Name;
                if ((fi.Extension == ".gif") || (fi.Extension == ".jpg"))
                    lstPic.Items.Add(new ListItem(fileName, imgDir + "\\" + fileName));
            }
        }
        lstPic.SelectedIndex = 0;
        face.Src = lstPic.Value;
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            condata();
        }
    }
    protected void LogOut_Click(object sender, EventArgs e)
    {
        Session["Admin"] = null;
        Response.Redirect("Default.aspx");
    }
    //添加留言
    public void condata()
    {
        //ACCESS数据库的连接字符串
        string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=";
        strConn += Server.MapPath(".\\Data\\BookData.mdb");
        //生成一个新的连接
        string strCom = "Insert into GuestBook (UserName,lstSex,Oicq,Email,Homepage,lstPic,Content,IP) Values ('"
            + UserName.Text.ToString() + "','"
            + lstSex.SelectedItem.Text + "','"
            + Oicq.Text.ToString() + "','"
            + Email.Text.ToString() + "','"
            + Homepage.Text.ToString() + "','"
            + lstPic.Value + "','"
            + UbbReplace(content.Text.ToString()) + "','"
            + Request.ServerVariables["REMOTE_HOST"] + "')";
            OleDbConnection myConn = new OleDbConnection(strConn);
            myConn.Open();
            OleDbCommand oldConn = new OleDbCommand(strCom, myConn);
            oldConn.ExecuteNonQuery();
            myConn.Close();
            Response.Redirect("Default.aspx");        
    }
    //分页
    public void strpage()
    {
        //int curPage = Convert.ToInt32(this.nowpage.Text);
        int CurPage;
        if (Request.QueryString["Page"] != null)
            CurPage = Convert.ToInt32(Request.QueryString["Page"]);
        else
            CurPage = 1;
        string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=";
        strConn += Server.MapPath(".\\Data\\BookData.mdb");
        //OleDbConnection strConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +
        //    System.Web.HttpContext.Current.Server.MapPath(".\\Data\\BookData.mdb"));
        OleDbConnection myConn = new OleDbConnection(strConn);
        //定义数据适配器
        OleDbDataAdapter oda = new OleDbDataAdapter();
        oda.SelectCommand = new OleDbCommand("select * from GuestBook order by ID DESC", myConn);
        //创建数据集
        DataSet ds = new DataSet();
        //创建所有数集
        oda.Fill(ds,"GuestBook");
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = ds.Tables["GuestBook"].DefaultView;
        ps.AllowPaging = true;
        //每个页面显示的留言数
        ps.PageSize = 5;
        this.onepage.Text = ps.PageSize.ToString();
        //求留言总数
        this.allmsg.Text = ps.DataSourceCount.ToString();
        //ps.CurrentPageIndex = curPage - 1;
        ps.CurrentPageIndex = CurPage - 1;
        //求总页
        this.allpage.Text = ps.PageCount.ToString();
        this.allpage1.Text = ps.PageCount.ToString();
        this.nowpage.Text = CurPage.ToString();
        this.DataList1.DataSource = ps;
        this.DataList1.DataBind();
        //上一页
        if (!ps.IsFirstPage)
            prepage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
        //下一页
        if (!ps.IsLastPage)
            nextpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
    }

    public string UbbReplace(string content)
    {
        content = content.Replace("\n", "<BR>");
        content = content.Replace("\t", "   ");
        content = content.Replace(" ", "&nbsp;");
        for (int i = 1; i < 43; i++)
            content = content.Replace("[em" + i + "]", "<IMG SRC=\"ubb/face/em" + i + ".gif\">");
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[url=(?<x>[^\]]*)\](?<y>[^\]]*)\[/url\]", @"<a href=$1 target=_blank>$2</a>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[email=(?<x>[^\]]*)\](?<y>[^\]]*)\[/email\]", @"<a href=mailto:$1>$2</a>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[flash](?<x>[^\]]*)\[/flash]", @"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[img](?<x>[^\]]*)\[/img]", @"<IMG SRC=""$1"" border=0>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[color=(?<x>[^\]]*)\](?<y>[^\]]*)\[/color\]", @"<font color=$1>$2</font>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[face=(?<x>[^\]]*)\](?<y>[^\]]*)\[/face\]", @"<font face=$1>$2</font>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[size=(?<x>[^\]]*)\](?<y>[^\]]*)\[/size\]", @"<font size=$1>$2</font>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[align=(?<x>[^\]]*)\](?<y>[^\]]*)\[/align\]", @"<div align=$1>$2</div>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[fly](?<x>[^\]]*)\[/fly]", @"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[move](?<x>[^\]]*)\[/move]", @"<marquee scrollamount=3>$1</marquee>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[glow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/glow\]", @"<table width=$1 style=""filter:glow(color=$2, strength=$3)"">$4</table>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[shadow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/shadow\]", @"<table width=$1 style=""filter:shadow(color=$2, strength=$3)"">$4</table>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[b\](?<x>[^\]]*)\[/b\]", @"<b>$1</b>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[i\](?<x>[^\]]*)\[/i\]", @"<i>$1</i>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[u\](?<x>[^\]]*)\[/u\]", @"<u>$1</u>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[h1\](?<x>[^\]]*)\[/h1\]", @"<h1>$1</h1>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[h2\](?<x>[^\]]*)\[/h2\]", @"<h2>$1</h2>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[h3\](?<x>[^\]]*)\[/h3\]", @"<h3>$1</h3>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[h4\](?<x>[^\]]*)\[/h4\]", @"<h4>$1</h4>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[h5\](?<x>[^\]]*)\[/h5\]", @"<h5>$1</h5>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[h6\](?<x>[^\]]*)\[/h6\]", @"<h6>$1</h6>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[small\](?<x>[^\]]*)\[/small\]", @"<small>$1</small>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[big\](?<x>[^\]]*)\[/big\]", @"<big>$1</big>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[del\](?<x>[^\]]*)\[/del\]", @"<del>$1</del>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[blink\](?<x>[^\]]*)\[/blink\]", @"<blink>$1</blink>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[sub\](?<x>[^\]]*)\[/sub\]", @"<sub>$1</sub>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[sup\](?<x>[^\]]*)\[/sup\]", @"<sup>$1</sup>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[list\](?<x>[^\]]*)\[/list\]", @"<li>$1</li>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[indent\](?<x>[^\]]*)\[/indent\]", @"<blockquote><p>$1</p></blockquote>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        content = System.Text.RegularExpressions.Regex.Replace(content, @"\[quote\](?<x>[^\]]*)\[/quote\]", @"以下内容为引用:<table border=0 width=95% cellpadding=10 cellspacing=1 bgcolor=#000000><tr><td bgcolor=#FFFFFF>$1</td></tr></table>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        return content;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -