牛腩购物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>";
}
}
}
}
相关文章推荐
- 牛腩购物4 aspnetpager控件的使用 以及 linkbutton 如何传值,判断传入的id值是否为数字
- 给认为SQL注入不仅仅只需要转换整数和过滤单引号,还需要考虑编码问题的人解惑
- asp.net 一个页面放两个aspnetpager控件,同时分页时产生的问题
- 牛腩新闻发布系统--分页学习(AspnetPager控件)
- 牛腩购物18 : 添加商品页面2 。泛型List<T>的使用,linq 的使用, 如何在 DropDownList 里面绑定二级分类(运用linq),fileupload和图片控件的配合使用用来上传图片,try catch
- 牛腩购物21:新品上架页面制作,下拉dropdownlist 进行产品的排序,asc,desc
- 牛腩购物29:用户中心订单页面制作,com+事务的运用(Transactions/TransactionScope)
- AngularJS页面【uib-dropdown】控件在模态窗口(弹出窗)中无法使用问题
- ReadString在UNICODE下中文乱码的解决办法(Char型字符和Unicode字符的转换问题)
- 解决WebBrowser控件打开本地页面报安全提示的问题
- java获取到的引号"会转换成&quot;问题
- 牛腩--验证控件问题
- 分页控件AspnetPager的用法,巩固用
- 在用户控件页面(ascx)中使用ViewState的一个问题
- 牛腩新闻发布系统(五):VS网站发布及常见问题
- PullToRefreshScrollView包含复杂组合控件时的页面定位问题
- MFC CString转换为string的宽字符问题解决
- Python unicode问题-如何根据引号的内容还原字符
- 字符串转换成整数&带通配符的字符串匹配问题
- 位操作和字符转换的一些小问题