环境搭建
源码解压
- DataBase 将这个文件中的数据库导入到数据库服务器中(SQLServer)
- 在vs中打开这个项目
- 环境搭建就不再赘述了
解析常用代码
$(function () {
//检测IE
if ('undefined' == typeof (document.body.style.maxHeight)) {
window.location.href = 'ie6update.html';
}
});
- 检测IE6 由于兼容问题在页面加载前要对浏览器的版本进行检查。
界面布局
- …list.aspx …edit.aspx
- list一般是展示页面用于查询数据库中的记录展示出来。
- edit一般是用来添加记录
常用功能—搜索
- 你会发现几乎每一个页面都有一个小放大镜,在数据成对的时候这就排上用场了。
- 注这里以 aticle_list.aspx为例子
前端代码
<div class="r-list">
<asp:TextBox ID="txtKeywords" runat="server" CssClass="keyword" />
<asp:LinkButton ID="lbtnSearch" runat="server" CssClass="btn-search" onclick="btnSearch_Click"><i class="iconfont icon-search"></i></asp:LinkButton>
</div>
//TextBox 就是前面的输入框
//LinkButton就是后面的放大镜 点击以后会去执行 后台的btnSearch_Click事件
后台的 搜索事件
protected void btnSearch_Click(object sender, EventArgs e)
{
Response.Redirect(Utils.CombUrlTxt("article_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}",
this.channel_id.ToString(), this.category_id.ToString(), txtKeywords.Text, this.property));
}
//可以看出就是执行了重定向括号里面就是为了 拼出url(根据里输入的条件)
1.可以看出{0}{1} 一猜就是要吧后面的东西加到里面组成符合http 协议的参数。
2.其他的参数与这个功能无关 我们只看 keywords
3.txtKeywords 就是前端的TextBox
CobUrlTxt 很容易理解 很像 Format
CombUrlTxt 方法
public static string CombUrlTxt(string _url, string _keys, params string[] _values)
{
StringBuilder urlParams = new StringBuilder();
//定义一个StringBuilder 准备好拼接工程
try
{
string[] keyArr = _keys.Split(new char[] { '&' });
//_keys 里面 channel_id={0}&category_id={1}&keywords={2}&property={3}
//将其通过‘&’进行分割 然后就要拼接了
for (int i = 0; i < keyArr.Length; i++)
{
if (!string.IsNullOrEmpty(_values[i]) && _values[i] != "0")
{
//从可变参数中拿出对应的值
_values[i] = UrlEncode(_values[i]);
//进行编码 可以不用看
urlParams.Append(string.Format(keyArr[i], _values) + "&");
//使用Format (keyword={2} ,"好文章")->keyword=好文章&
//因为有多个参数所以要加上‘&’
//拼接完成
}
}
if (!string.IsNullOrEmpty(urlParams.ToString()) && _url.IndexOf("?") == -1)
urlParams.Insert(0, "?");
//如果有参数的话就在参数的开头加上‘?’
//这样就符合http协议了如果没有参数就只用地址。
}
catch
{
return _url;
}
return _url + DelLastChar(urlParams.ToString(), "&");
//将最后一个&去掉 内部使用 Substring 不再细说
}
- 现在我们就可以带着条件重定向到网页了 例子:article_list.aspx?keyword=好文章
- 现在就会去请求article_list.aspx
this.channel_id = DTRequest.GetQueryInt("channel_id");
this.category_id = DTRequest.GetQueryInt("category_id");
***this.keywords = DTRequest.GetQueryString("keywords");***
//我们先只看这个从
//为什么要自己定义方法呢 内部多了一个如果是sql 语句的会进行安全检测防止注入。
this.property = DTRequest.GetQueryString("property");
//这里都是使用
//判断点击的是哪一个按钮
if (channel_id == 0)
{
JscriptMsg("频道参数不正确!", "back");
return;
}
this.channel_name = new BLL.site_channel().GetChannelName(this.channel_id); //取得频道名称
this.pageSize = GetPageSize(10); //每页数量
this.prolistview = Utils.GetCookie("article_list_view"); //显示方式
//看页面是否是第一次加载(asp 的请求就不会执行这里面的代码)
if (!Page.IsPostBack)
{
ChkAdminLevel("channel_" + this.channel_name + "_list", DTEnums.ActionEnum.View.ToString()); //检查权限
TreeBind(this.channel_id); //绑定类别
***RptBind(this.channel_id, this.category_id, "id>0" + CombSqlTxt(this.keywords, this.property), "sort_id asc,add_time desc,id desc");***
//我们先看与 搜索有关的代码
}
}
//看一下RptBind 数据绑定 前台数据都是通过 数据绑定来实现 显示的
//这里 只看string _strWhere 代码 即
//先看一下CombSqlTxt(this.keywords, this.property)
//根据搜索的条件生成 sql 这样在查询数据库的时候又多一个条件 从而实现搜索
#region 组合SQL查询语句==========================
protected string CombSqlTxt(string _keywords, string _property)
{
StringBuilder strTemp = new StringBuilder();
_keywords = _keywords.Replace("'", "");
if (!string.IsNullOrEmpty(_keywords))
{
//如果是第一次加载 没有点击搜索 这里就不会有多余的条件。
strTemp.Append(" and title like '%" + _keywords + "%'");
//条件最中会生成 sql 中的 title like %好文章%
}
return strTemp.ToString();
}
#endregion
//接下来就是去查询数据库和把数据绑定到前端界面了
#region 数据绑定=================================
private void RptBind(int _channel_id, int _category_id, string _strWhere, string _orderby)
{
BLL.article bll = new BLL.article();
switch (this.prolistview)
{
//两种 服务器模板绑定 并且设置一个不可见。
//这里有两种显示方式 不用管 我们先看一种
case "Txt":
this.rptList2.Visible = false; 设置这个模板 不显示
this.rptList1.DataSource = bll.GetList(_channel_id, _category_id, this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
模板一的 数据源 就是用生成好的 去查数据库获取数据
this.rptList1.DataBind();
//绑定 数据 显示数据
break;
default:
this.rptList1.Visible = false;
this.rptList2.DataSource = bll.GetList(_channel_id, _category_id, this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
this.rptList2.DataBind();
break;
}
//绑定页码
string pageUrl = Utils.CombUrlTxt("article_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}&page={4}",
_channel_id.ToString(), _category_id.ToString(), this.keywords, this.property, "__id__");
PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
}
#endregion