new starting point new beginning
学习、合作、共赢
近日做了个论坛,其中首页有块想显示幅图片旁边跟点内容。经过CSDN网友的帮助和老大的反复测试终于实现。费了这么多周折,本人觉得很有必要将其记下来。下面是效果:


实现代码:
 1//1. 在数据库里用charindex取出有图片的数据集
 2public void DoGridViewBindImages(DataList dlMain, SqlDatabaseConnection sqlConn, int paraCatalogCode, int paraCount)
 3        {
 4            string strSQL = "p_BBS_Title_PictureShow '" + (paraCatalogCode.ToString() == "0" ? "" : paraCatalogCode.ToString()) + "', '" + paraCount.ToString() + "','0','22'";
 5            DoGridViewBindImage(dlMain, sqlConn, strSQL);
 6        }

 7
 8//2.使用正则表达式取出图片路径和部分内容
 9private void DoGridViewBindImage(DataList dlMain, SqlDatabaseConnection sqlConn, string paraSQL)
10        {
11            string strTemp = "";
12
13            DataSet dsMain = sqlConn.ExecuteDataSet(paraSQL, "_aaa");
14            dsMain.Tables[0].Columns.Add(new DataColumn("cImage"));
15            dsMain.Tables[0].Columns.Add(new DataColumn("cContentSub"));
16
17            foreach (DataRow drRow in dsMain.Tables[0].Rows)
18            {
19                MatchCollection mc = Regex.Matches(drRow["cContent"].ToString(), "<img.+?src=\"(?<url>.+?)\".+?>", RegexOptions.IgnoreCase);
20                for (int i = 0; i < mc.Count; i++)
21                {
22                    if (mc[i].Groups["url"].Value.Trim() != "")
23                    {
24                        drRow["cImage"= mc[0].Groups["url"].Value;
25                        break;
26                    }

27                }

28
29//<.*?>
30                strTemp = Regex.Replace(drRow["cContent"].ToString(), "<[^>]*>""", RegexOptions.IgnoreCase).Trim();
31
32                strTemp = strTemp.Replace("&nbsp;""").Replace(" """).Replace("&ldquo;""\"").Replace("    """).Replace((char)13, (char)32).Replace((char)10, (char)32);
33
34                if (strTemp.Length > 100)
35                {
36                    drRow["cContentSub"= strTemp.Substring(090).Replace("<""&lt;").Replace(">""&gt;"+ "";
37                }

38                else
39                {
40                    drRow["cContentSub"= strTemp.Replace("<""&lt;").Replace(">""&gt;");
41                }

42            }

43
44            dlMain.DataSource = dsMain.Tables[0].DefaultView;
45            dlMain.DataBind();
46        }
posted on 2006-12-14 16:43  xpengfee  阅读(738)  评论(0)    收藏  举报