.net如何改变datalist,datagrid标题长度,类似标题+...

datalist html代码:

<asp:DataList id="DataList1" runat="server">

<ItemTemplate>
<asp:HyperLink id=title runat="server" NavigateUrl='<%# "~/szdw_detail.aspx?feedbackid="+ DataBinder.Eval(Container.DataItem, "feedbackid") %>'>
          <%#   DataBinder.Eval(Container.DataItem,"subject") %>
        </asp:HyperLink>
</ItemTemplate>
</asp:DataList>




后台代码:

void show_datagrid()
    {
      mycmd=new SqlCommand("select top 8 * from feedback  order by feedBackid desc",Con);
      da = new SqlDataAdapter(mycmd);
      ds =new DataSet();
      da.Fill(ds, "FeedBack");
    
      DataList1.DataSource = ds.Tables["FeedBack"].DefaultView;
      DataList1.DataBind();

      for(int i=0;i<ds.Tables["FeedBack"].Rows.Count;i++)
      {
        HyperLink title=(HyperLink)DataList1.Items[i].FindControl("title");
        
        s=ds.Tables["FeedBack"].Rows[i]["subject"].ToString();
        if(s.Length<15)

          s=s.ToString();
        else
          s=s.Substring(0,15)+"...";
        title.Text=s;
          
          
      }



datagrid 模板列实现比较简单,绑定列实现要对sql语句进行修改,如下是模板列 html代码:

<asp:DataGrid id="DataGrid1" Width="184px" runat="server"  AutoGenerateColumns="False"
  ShowHeader="False">
  <Columns>
    <asp:TemplateColumn>
      <ItemTemplate>
        <asp:HyperLink id=title runat="server" NavigateUrl='<%# "~/szdw_detail.aspx?feedbackid="+ DataBinder.Eval(Container.DataItem, "feedbackid") %>'>
          <%#   DataBinder.Eval(Container.DataItem,"subject") %>
        </asp:HyperLink>
      </ItemTemplate>
    </asp:TemplateColumn>
  </Columns>
</asp:DataGrid>



后台代码:datalist改成对应的datagrid即可

如果是绑定列,则对sql代码进行修改,后台代码如下:


    void show_datagrid()
    {
      mycmd=new SqlCommand("select feedBackid, case when len(subject)>15 then left(subject,15)+'...' else subject end as subject from feedback order by feedBackid desc",Con);
      //mycmd=new SqlCommand("select top 8 * from feedback  order by feedBackid desc",Con);
      da = new SqlDataAdapter(mycmd);
      ds =new DataSet();
      da.Fill(ds, "FeedBack");
    
      DataGrid1.DataSource = ds.Tables["FeedBack"].DefaultView;
      DataGrid1.DataBind();
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:

评论: 0 | 引用: 0 | 查看次数: 7737
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启