天天看点

C#简单文章管理系统后台源码

C#简单文章管理系统后台源码

看到网上各种各样功能强大的cms,相当羡慕啊,工作后一直用的.net开发程序,很想找个时间写个简单的玩玩,正好前些天看到一系统的界面还可以,暂时拿来用下,仅供学习交流使用。

网上的cms功能太强大,我写的代码只是简单的实现了cms中的文章管理功能部分

开发环境 vs2008 SQLEXPRESS2005

1.后台登陆页面

C#简单文章管理系统后台源码

图一

if (Session["ValidateCode"].ToString() == this.tbValidateCode.Text.ToUpper())

{

if (AdminManager.Exist("UserName='" + CommonFunctions.AntiSqlIn(this.tbUserName.Text) + "' and Password='" + CommonFunctions.AntiSqlIn(this.tbPassword.Text) + "'"))

{

Admin admin= AdminManager.GetList(0, "UserName='" + this.tbUserName.Text + "' and Password='" + this.tbPassword.Text + "'", "")[0];

LoginLog loginLog = new LoginLog();

loginLog.AddTime = System.DateTime.Now;

loginLog.Status = 0;

loginLog.IP = Request.UserHostAddress;

loginLog.UserID = admin.ID;

loginLog.Type = 5;

LoginLogManager.AddLoginLog(loginLog);

Session["AdminUser"] = admin;

Response.Redirect("Index.htm");

}

else

{

CommonFunctions.ShowMessage(Page, "账号或密码错误", "");

}

}

else

{

CommonFunctions.ShowMessage(Page, "验证码错误", "");

}

2.登陆后的系统界面

C#简单文章管理系统后台源码

图二

文章列表主要代码

public void BindData()

{

PagedDataSource pds = new PagedDataSource();

string sql = "1=1 ";

if (txtKeyword.Text.Trim() != "")

{

sql += " and title like '%" + CommonFunctions.AntiSqlIn(txtKeyword.Text.Trim()) + "%'";

}

IList<Article> list = ArticleManager.GetList(0, sql + "and CategoryID=6 and Status=0", "id desc");

pds.DataSource = list;

pds.AllowPaging = true;

pds.PageSize = this.AspNetPager1.PageSize;

pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;

repList.DataSource = pds;

repList.DataBind();

this.AspNetPager1.RecordCount = list.Count;

}

分页用的AspNetPager1 控件

主要代码

protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)

{

this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;

BindData();

}

3.添加文章页面

C#简单文章管理系统后台源码

图三

model = new Article();

model.Content = Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(this.tbContent.Text);

model.Title = Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(this.tbTitle.Text);

model.CategoryID = Convert.ToInt32(this.ddlCategory.SelectedValue);

model.AddTime = System.DateTime.Now;

model.Status = 0;

try

{

ArticleManager.AddArticle(model);

CommonFunctions.ShowMessage(Page, "添加成功", "ArticleList.aspx");

}

catch (Exception ex)

{

CommonFunctions.ShowMessage(Page, ex.ToString(), "");

}

4.文章管理页面

C#简单文章管理系统后台源码

图4

删除文章代码

protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

Article article = ArticleManager.GetArticleById(Convert.ToInt32(gvList.DataKeys[e.RowIndex].Value));

article.Status = 1;

ArticleManager.ModifyArticle(article);

BindData();

}

光棒效果,文章编号代码

protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowIndex != -1)

{

int id = (e.Row.RowIndex + 1) + gvList.PageSize * gvList.PageIndex;

e.Row.Cells[0].Text = id.ToString();

}

if (e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#e9f4fd'");

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");

}

}

分页代码

protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

gvList.PageIndex = e.NewPageIndex;

BindData();

}

5.异常页面

C#简单文章管理系统后台源码

图5

6.过滤跨站代码直接用的微软的AntiXss类库

model.Content = Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(this.tbContent.Text);

7.异常记录代码

通过Global.asax文件实现的

void Application_Error(object sender, EventArgs e)

{

// 在出现未处理的错误时运行的代码

string referrer = (Request.UrlReferrer != null) ? Request.UrlReferrer.AbsoluteUri : String.Empty;

CommonFunctions.LogsWrite(Server.MapPath("~/Logs/Logs.xml"),Request.Url.AbsoluteUri,System.DateTime.Now.ToString(),Request.UserHostAddress,Request.UserAgent,referrer,Server.GetLastError().GetBaseException().Message);

}

8.数据库连接代码

<add name="SQLCon" connectionString="Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|/SZCMS.MDF;Integrated Security=True;User Instance=True"/>

9.session超时时间设定 可以通过 Inproc 方式或者是通过StateServer方式

推荐StateServer,用InProc时session容易丢失

<sessionState timeout="60" mode="InProc"></sessionState>

<!--<sessionState timeout="10" mode="StateServer"></sessionState>-->

10.Session登陆验证用的httpModules处理

void context_AcquireRequestState(object sender, EventArgs e)

{

HttpApplication ha = (HttpApplication)sender;

if (ha.Context.Session == null)

return;

if (ha.Context.Session["AdminUser"] == null)

{

string str=ha.Context.Request.Url.AbsolutePath.ToLower();

if (str.IndexOf("/system") == 0 && str != "/system/login.aspx")

{

ha.Context.Response.Write("<script>alert('登录超时,请重新登录');top.location.href='/System/Login.aspx';</script>");

ha.Context.Response.End();

}

}

}

11.登录账号 admin SZCMS

如果你发现有什么不合理的,需要改进的地方,邮件联系[email protected](qq常年不在线,邮件联系)朱晓 (泰山学院)。相互交流 谢谢

下载地址 http://download.csdn.net/source/3148143