新闻视频 36:整合首页 用到 Repeater 主要用gridview /gridview去掉边框用到 BorderWidth=”0” inner join和 left

来看首页的 类别

image   要用循环么?不用  用数据绑定就可以了(实际也是循环)  我们用 repeater

image

这个Repeater 不会生成多余的html代码

image

image

在写sql语句的时候,如果用到多表查询,那么能写 inner join 的  就写出来。因为当我们有时候,需要修改,比如把内连接(两张表都必须有符合逻辑的数据),改成 左连接 (比如 显示新闻和新闻评论个数,有的评论个数是0,如果右表新闻评论不存在,如果用的是内连接,则没有了显示。这个时候就应该用左连接)

ALTER proc [dbo].[news_SelectHotNews]
as
begin
select top 5 a.id,a.title,a.createtime,  c.name,  COUNT(b.id) as comCount
		from news a,comment b,category c
		where a.id=b.newsid and a.caId=c.id
		group by a.id,a.title,a.createtime,  c.name
		order by comCount desc
		 
end

改成  inner join 的写法是

ALTER proc [dbo].[news_SelectHotNews]
as
begin
		----select top 5 a.id,a.title,a.createtime,  c.name,  COUNT(b.id) as comCount
		----from news a,comment b,category c
		----where a.id=b.newsid and a.caId=c.id
		----group by a.id,a.title,a.createtime,  c.name
		----order by comCount desc
		
		select top 5 a.id,a.title,a.createtime,  c.name,  COUNT(b.id) as comCount
		from news a
		inner join category c on a.caId=c.id
		inner join comment b on b.newsid=a.id
		
		group by a.id,a.title,a.createtime,c.name
		order by comCount desc 
		 
end
显示结果都是一样的   image

如果将 评论表改成左连接,看看

image

执行之后

image

就把评论为0 的 新闻 也显示出来了。  下面继续完成首页的修改。

image

我们用 gridview 来显示。

我们先把原来的表格 注释掉,然后拖一个 gridview 控件进去

1:现在后台绑定数据源

image

image

生成如下

image

那么 ,如何在 gridview 里面修改? 涉及到一个 自定义模板

1:禁用 自动生成列 

image

image

选择编辑列之后,添加3个  绑定字段  BoundField 

image

image

image

image

这个时候,我们看看源代码,会发现生成了3个 绑定列  他们的HeaderText 变成了 我们想要的  类别 标题 和时间

image

我们运行看看,发现绑定数据后的表格和下面的表格样式有区别,如何把 th_category 加入到表格呢?我们不能单纯的使用 绑定字段,而是要使用 自定义模板列

image

image

image

把3个都选择为  模板列 TemplateField   ,然后转到  代码界面。会发现生成了很多代码,但是有一些代码  是不需要的,我们删掉。

image

image

这个时候 我们 th 的样式就解决了,如何来解决 td 里面的 文字,以及 加入链接等改变呢?

td我们使用 ItemStyle-CssClass

image 

截取一个字符串,调用一个静态函数,不需要另外的生成,直接调用

  ///   <summary> 
    ///   将指定字符串按指定长度进行剪切, 
    ///   </summary> 
    ///   <param   name= "oldStr "> 需要截断的字符串 </param> 
    ///   <param   name= "maxLength "> 字符串的最大长度 </param> 
    ///   <param   name= "endWith "> 超过长度的后缀 </param> 
    ///   <returns> 如果超过长度,返回截断后的新字符串加上后缀,否则,返回原字符串 </returns> 
    public static string StringTruncat(string oldStr, int maxLength, string endWith)
    {
        if (string.IsNullOrEmpty(oldStr))
            //   throw   new   NullReferenceException( "原字符串不能为空 "); 
            return oldStr + endWith;
        if (maxLength < 1)
            throw new Exception("返回的字符串长度必须大于[0] ");
        if (oldStr.Length > maxLength)
        {
            string strTmp = oldStr.Substring(0, maxLength);
            if (string.IsNullOrEmpty(endWith))
                return strTmp;
            else
                return strTmp + endWith;
        }
        return oldStr;
    } 

image

这个实际上是错的。正确的写法如下

image

再来将 Lable换成 超链接 href

image

gridview 默认会给边框加1个像素,如何调整呢?我们用到 BorderWidth=”0” 即可

image

原文地址:https://www.cnblogs.com/iceicebaby/p/2244806.html