Net常用代码总结

六逸

11-10 14:10

DropDownList绑定多个字段

string sql_ch = string.Format("select C_ID,C_name from T_Channel");
DataTable dt_ch = DBUtility.DbHelperSQL.Query(sql_ch).Tables[0];
if (dt_ch != null && dt_ch.Rows.Count > 0)
{
       for (int i = 0; i < dt_ch.Rows.Count; i++)
            {
                string strText = "[" + dt_ch.Rows[i]["C_ID"].ToString() + "]" + dt_ch.Rows[i]["C_name"].ToString();
                string strValue = dt_ch.Rows[i]["C_ID"].ToString(); ;
                ListItem listItem = new ListItem
                {
                    Text = strText,
                    Value = strValue
                };

                this.DropDownList1.Items.Add(listItem);
            }
}

取得HTML中所有图片的 URL

#region 取得HTML中所有图片的 URL。
/// 取得HTML中所有图片的 URL。    
/// HTML代码    
///图片的URL列表public static string[] GetHtmlImageUrlList(string sHtmlText)
{
    // 定义正则表达式用来匹配 img 标签    
    Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
    // 搜索匹配的字符串    
    MatchCollection matches = regImg.Matches(sHtmlText);
    int i = 0;
    string[] sUrlList = new string[matches.Count];
    // 取得匹配项列表    
    foreach (Match match in matches)
        sUrlList[i++] = match.Groups["imgUrl"].Value;
    return sUrlList;
}
#endregion
//方法一,调取第一张图片
string str = dr["Organ_Intro"].ToString();
string[] imgs = GetHtmlImageUrlList(str);
dr["Organ_IntroShow"] = imgs[0];
//方法二,调取所有图片
string str = model.Organ_Intro;

缓存

protected void Page_Load(object sender, EventArgs e)
{
    string ids="";
    Maticsoft.BLL.ScriptsBak bll = new Maticsoft.BLL.ScriptsBak();
    Listlist = new List();
    list = bll.GetAll();
    for (int i = 0; i < list.Count; i++)
    {
        ids += list[i].ScriptId.ToString()+"--";
    }
    ids = ids + "完";  //这里的ids为从数据库中读取表中的id值然后用--链接起来的一个字符串
    if (Cache["key"] == null)
    {
        Cache.Insert("key", ids, null, DateTime.Now.AddSeconds(40), System.Web.Caching.Cache.NoSlidingExpiration);  //这里给数据加缓存,设置缓存时间
        //"key"为缓存的键,ids为缓存起来的值,null为缓存依赖项,这里没有使用缓存依赖项,所以为null,下面会详细介绍缓存依赖项
           //null后面为缓存的时间为40秒
          //最后一个参数为设置时间的格式,ASP.NET允许你设置一个绝对过期时间或滑动过期时间,但不能同时使用,
          //我们这里设置的为绝对过期时间,也就是没刷新一次页面后缓存数据为40秒,40秒后会从数据库中重新获取。
        Response.Write("cache加载为---" + Cache["key"] + "
");
    }
    else
    {
        Response.Write("cache加载为---" + Cache["key"] + "
");
    }
    Response.Write("直接加载为---" + ids + "
");
}

清除缓存

System.Web.Caching.Cache _cache = HttpRuntime.Cache;
_cache.Remove("date");


版权申明:本文地址 Net常用代码总结 ,本博客所有文章除特别声明外,均属于原创,未经许可,请勿转载,请尊重博主的劳动成果!
点赞 打赏
请作者喝杯咖啡 ×
个人中心设置的支付宝收款码个人中心设置的微信收款码
网友评论请遵守相关法律与法规,文明评论。O(∩_∩)O~~条评论
  • 发表评论
正在加载
查看更多评论
六逸

思想决定心态,心态决定一切。

假如生活欺骗了你,

不要悲伤,不要心急!

忧郁的日子里须要镇静,

相信吧,快乐的日子将会来临!

心儿永远向往着未来;

现在却常是忧郁。

一切都是瞬息,一切都将会过去;

而那过去了的,将会成为亲切的怀恋。

  • ...

    博文

  • ...

    评论

  • ...

    运行

  • ...

    浏览

切换注册

登录

切换登录

注册