做网站中遇到的一些问题及解决方法

关于搜索:
点击搜索后跳转到本页。
response.redirect("userlist.aspx?xxx="+搜索关键字);
绑定数据的时候

判断是由否有搜索关键字。有则加上搜索关键字;
private void BindData()
    {
        try
        {
            string strWhere = "1=1";
            if (Request["typeid"] != null)
            {
                if (Request["typeid"]!="0")
                {
                   strWhere += " and CateId=" + Request["typeid"];
                }
               
            }
            if (Request["title"] != null)
            {
                strWhere += " and ArtTitle like '%" + Request["title"]+"%'";
            }

            int total = 0;
            var dt = news.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, strWhere, "ArtAddTime desc, ArtId asc", out total).Tables[0];
            AspNetPager1.RecordCount = total;
            if (dt.Rows.Count > 0)
            {
                repArticles.DataSource = dt;
                repArticles.DataBind();
                lbText.Text = "";
                lbText.Visible = false;
            }
            else
            {
                lbText.Text = "没有可显示的数据!";
                lbText.Visible = true;
            }

        }
        catch (Exception)
        {
        }
    }
protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("list.aspx?typeid=" + ddlCategory.SelectedValue + "&title=" + SearchText.Text.Trim());
    }

关于页面的导航:
如:首页--新闻中心--行业新闻
1.根据传过来的分类id获取他的CatePath路径(0,1,5),

 /// <summary>
        /// 获取该分类的所有顶级。
        /// </summary>
        /// <param name="id"></param>
        public List<Lvegu.Model.Category> GetEachTop(int id)
        {
            Lvegu.Model.Category model = GetModel(id);
            string idlist = "";
            List<Lvegu.Model.Category> list = new List<Lvegu.Model.Category>();
     //若已是最顶级的分类,则把该model加到list返回
            if (model.CatePId == 0)
            {
                list.Add(model);
            }
            else
            {
  //这样拼出来就是1,5
                idlist = model.CatePath.Substring(2, model.CatePath.Length - 2) + model.CateId;
  
  //这里当然就是CateId in(1,5),他的所有顶级分类据得到了
                DataSet ds = dal.GetList("CateId in(" + idlist + ")");
  
  //再转为list类型
                list = DataTableToList(ds.Tables[0]);
            }
            return list;
        }
那么在页面只需下面的绑定
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                var cid = Request["cid"] == null ? 6 : int.Parse(Request["cid"]);
                var list = new Lvegu.BLL.Category().GetEachTop(cid);
                repSiteMap.DataSource = list;
                repSiteMap.DataBind();
            }
            catch (Exception)
            {
            }
        }
    }
此处用用户控件(可以重复使用,方便)
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UCMapSite1.ascx.cs" Inherits="Controls_UCMapSite1" %>
<div class="the">
    <span>您当前的位置 &nbsp;-&nbsp;</span><a href="index.aspx">首页</a>
    <asp:Repeater ID="repSiteMap" runat="server">
        <ItemTemplate>
            &nbsp;-&nbsp;<a href='About.aspx?cid=<%#Eval("CateId") %>'><%#Eval("CateName") %></a>
        </ItemTemplate>
    </asp:Repeater>
</div>

关于左侧菜单:
(左侧菜单一般是根据首页点击导航到内页,点击不同导航,则内页的左侧菜单也跟着变化)

1.根据传过来的分类获取该分类的顶级分类
2.再根据这个顶级分类的id获取他下面的所有自分类(递归获取)

private void BindMenu()
    {

        try
        {
            //根据传过来的cid获取顶级分类id
            var cid = Request["cid"] == null ? 6 : int.Parse(Request["cid"]);
    
            var model = cate.GetModel(cid);
     //若传过来的分类不存在返回
            if (model == null)
            {
                return;
            }
            else
     {
  //CatePath的格式0,1,4,8  其实就是所有的父级分类的id/
  //去掉0,取第二个就是最顶级的分类id了。
  //此处不能用model.CatePath.Substring(2, 1)这种方法
  //因为第二个有可能是1位数、也有可能是2位数,还有可能是3位数
                String strtopid = model.CatePath.Split(new Char[] { ',' })[1].ToString();

  //如果model的父级id为0,则传过来的是最顶级的分类,如不是则用strtopid
                var topid = model.CatePId == 0 ? model.CateId : int.Parse(strtopid);//顶级id
  
                var dt = cate.GetMyList("CatePId=" + topid.ToString() + " order by CateSort asc");

                //左侧菜单标题
                menuTitle.Text = model.CatePId == 0 ? model.CateName : cate.GetModel(topid).CateName;

                repMenu.DataSource = dt;
                repMenu.DataBind();
            }
        }
        catch (Exception)
        {

        }
    }


/**
 * 截取指定长度的字符串,超出部分用“...”代替。中文字符算两个字节
 * @param   {string} raw    待截取的字符串,不能包含html
 * @param   {number} len    要截取的字节长度
 * @param   {string} tail   要来替代的字符
 * @returns {string}
 */
function cutstring(raw, len, tail) {

	var array  = new Array(),
		length = 0,
		_chars = raw.split("");
	for (var i in _chars) {

		if (length >= len) {
			array.push(tail || "...")
			break;
		}
		//如果是中文,length+2
		if (_chars[i].charCodeAt(0) > 225) {
			length += 2;
		} else {
			length++;
		}
		array.push(_chars[i]);
	}

	return array.join("");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值