您的位置:首页 > 编程语言 > ASP

牛腩购物5 aspnetpager控件的巩固(屏蔽恶意字符过滤转换 单引号问题,制作新闻页面)

2012-03-05 14:39 309 查看
过滤非法字符串(但是查询的时候,假如是英文名字,是很容易有单引号的例如Joey’sname,这个时候我们就需要把单引号,换成2个单引号

///<summary>过滤sql非法字符串
///
///</summary>
///<paramname="value"></param>
///<returns></returns>
publicstaticstringGetSafeSQL(stringvalue)
{
if(string.IsNullOrEmpty(value))
returnstring.Empty;
value=Regex.Replace(value,@";",string.Empty);
//value=Regex.Replace(value,@"'",string.Empty);
value=Regex.Replace(value,@"'","''");
value=Regex.Replace(value,@"&",string.Empty);
value=Regex.Replace(value,@"%20",string.Empty);
value=Regex.Replace(value,@"--",string.Empty);
value=Regex.Replace(value,@"==",string.Empty);
value=Regex.Replace(value,@"<",string.Empty);
value=Regex.Replace(value,@">",string.Empty);
value=Regex.Replace(value,@"%",string.Empty);
returnvalue;
}
接下来我们制作新闻表和前台的新闻制作。
shop_news:id,title,body,visitnum,createdate,type
新闻id,标题,内容,浏览量,创建时间,新闻类型(商品专题或者是新闻中心)
要学会代码的复用,ctrl+c,Ctrl+v
/*********************************************************
*开发人员:JoeyQQ:1727050508博客:http://1727050508.cnblogs.com*创建时间:2012-3-510:39:42
*描述说明:news_list.aspx新闻列表页
*
*更改历史:
*
********************************************************/
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;

namespaceNiunan.Shop.Web.admin
{
publicpartialclassnews_list:System.Web.UI.Page
{
Niunan.Shop.DAL.NewsDAOnewsdao=newDAL.NewsDAO();
protectedvoidPage_Load(objectsender,EventArgse)
{
BindRep();
}

protectedvoidanp_PageChanged(objectsender,EventArgse)
{
BindRep();
}
protectedvoidlbtnDel_Click(objectsender,EventArgse)
{
stringid=(senderasLinkButton).CommandArgument;
newsdao.Delete(int.Parse(id));
BindRep();
}

privatevoidBindRep()
{
intpagesize=anp.PageSize;
intpageindex=anp.CurrentPageIndex;
anp.RecordCount=newsdao.ClacCount(GetCond());
repList.DataSource=newsdao.GetList("*","id","desc",pagesize,pageindex,GetCond());

repList.DataBind();
}

privatestringGetCond()
{
stringcond="1=1";

stringtype=Request.QueryString["type"];
if(!string.IsNullOrEmpty(type)&&type=="spzt")
{
cond+="andtype='商品专题'";
litH1.Text="商品专题";
}
else
{
cond+="andtype='新闻中心'";
litH1.Text="新闻中心";
}

stringkey=txtKey.Text.Trim();
key=Niunan.Shop.Utility.Tool.GetSafeSQL(key);
if(key.Length!=0)
{
cond+="andtitlelike'%"+key+"%'";
}
returncond;
}

protectedvoidbtnSearch_Click(objectsender,EventArgse)
{
BindRep();
}
}
}
下面是新闻添加和修改页面的代码
/*********************************************************
*开发人员:JoeyQQ:1727050508博客:http://1727050508.cnblogs.com*创建时间:2012-3-515:30:56
*描述说明:news_add.aspx新闻添加和修改页面
*
*更改历史:
*
********************************************************/
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;

namespaceNiunan.Shop.Web.admin
{
publicpartialclassnews_add:System.Web.UI.Page
{
Niunan.Shop.DAL.NewsDAOnewsdao=newDAL.NewsDAO();

//Page_Load是页面进入的时候执行的函数,不论是第一次进入,还是我们点了按钮回发进入,都会执行的
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!Page.IsPostBack)
{
stringid=Request.QueryString["id"];
intx;
if(!string.IsNullOrEmpty(id)&&int.TryParse(id,outx))
{
Niunan.Shop.Model.Newsnewsmodel=newsdao.GetModel(x);
if(newsmodel!=null)
{

txtTitle.Text=newsmodel.title;
txtBody.Text=newsmodel.body;
litH1.Text="修改";
btnAdd.Text="修改";
}
}
}

}

protectedvoidbtnAdd_Click(objectsender,EventArgse)
{
stringtitle=txtTitle.Text.Trim();
stringbody=txtBody.Text.Trim();
stringtype=Request.QueryString["type"];
if(!string.IsNullOrEmpty(type)&&type=="spzt")
{
type="商品专题";
}
else
{
type="新闻中心";
}

if(title.Length==0||body.Length==0)
{
litRes.Text="<spanstyle='color:blue'>请填写完整的信息</span>";
return;
}

//如果有传入ID,那么就是修改
stringid=Request.QueryString["id"];
intx;
if(!string.IsNullOrEmpty(id)&&int.TryParse(id,outx))
{
//这里是重复判断,到底根据这个ID,能不能获得这个实体
Niunan.Shop.Model.Newsnewsmodel=newsdao.GetModel(x);
if(newsmodel!=null)
{
newsmodel.title=title;
newsmodel.body=body;
newsdao.Update(newsmodel);
litRes.Text="<spanstyle='color:red'>修改成功</span>";
return;
}
}

//否则是添加
intres=newsdao.Add(newNiunan.Shop.Model.News()
{
title=title,
body=body,
createdate=DateTime.Now,
type=type,
visitnum=0
});

if(res>0)
{
txtTitle.Text="";
txtBody.Text="";
litRes.Text="<spanstyle='color:blue'>添加成功</span>";
}
else
{
litRes.Text="<spanstyle='color:red'>添加失败,请联系管理员</span>";
}

}
}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐