ASP.NET中GridView绑定..

wuhongyao3 2008-09-24 11:26:25
现在有这样一个需求,将一个表绑定到中,其中有一列是绑定的标题,我想这一列就显示10个字符,如果超过10个字符就截取10个字符然后+.... 。 然后想到了gvdongtai_RowDataBound 这个事件 。 于是


<asp:GridView ID="gvdongtai" AutoGenerateColumns="False" runat="server" Width="170px"
GridLines="None" OnRowDataBound="gvdongtai_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<li><a href='DynamicNews.aspx?DynamicID=<%#Eval("ID")%>' class="c">
<%#Eval("Title")%>
</a></li>
<asp:Label ID="test" runat="server" Visible="false" Text='<%#Eval("Title") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

这个是GridView代码

protected void gvdongtai_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
1种 //string s = e.Row.Cells[0].Text;
//Label mylabel = new Label();
//mylabel = (Label)e.Row.FindControl("Test");
2种//string s = mylabel.Text.Trim();
3种string s = DataBinder.Eval(e.Row.DataItem, "Title").ToString().Trim();
//e.Row.Cells[0].Text = s.Length > 20 ? s.Substring(0, 20) : s.Trim();
}
}

三种修改的方法 ,其中第一种 ,根本获得不到值 ,我现在也没搞懂为什么获取的都是“”
第二种,第三种都可以正常获得值 。但是再重新修改e.Row.Cells[0].Text 的时候,我本来这一个字段是超链接字段,这样修改以后,超链接没有了。

实在不知道改怎么做了。 所以来求助一下大家 ,请帮下忙。。 去睡觉了。 明天来看帖子 谢谢了

哦。难道在 e.Row.Cells[0].Text= 要加上<a href..... ... 么。。
...全文
200 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengxj85 2008-09-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 namhyuk 的回复:]
<%# GetSubString(Eval("NewsTitle").ToString(), 10)%>

public static string GetSubString(string str, int n)
{
return (str.Length > n) ? str.Substring(0, n - 1) + "..." : str;
}
[/Quote]
比较好的方法
waltguo 2008-09-25
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
/// <summary>
/// Strings 的摘要说明
/// </summary>
public class Strings
{
public Strings()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string CutString(string inputString, int len)
{

ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
}

try
{
tempString += inputString.Substring(i, 1);
}
catch
{
break;
}

if (tempLen > len)
break;
}
//如果截过则加上半个省略号
byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
if (mybyte.Length > len)
tempString += "…";

return tempString;
}
}


<%# Strings.CutString(Eval("NewsTitle").ToString(),10)%>//这是前台,aspx页面里的,调用,一Strings类的CutString方法,这样就可以实现了。,
wuhongyao3 2008-09-25
  • 打赏
  • 举报
回复
结贴吧。 呵呵
tkscascor 2008-09-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 namhyuk 的回复:]
<%# GetSubString(Eval("NewsTitle").ToString(), 10)%>

public static string GetSubString(string str, int n)
{
return (str.Length > n) ? str.Substring(0, n - 1) + "..." : str;
}
[/Quote]
来晚拉 这个好!
cfreez 2008-09-25
  • 打赏
  • 举报
回复
1种 //string s = e.Row.Cells[0].Text;

这个获取的是单元格的值,不是单元格里面label的text
wuhongyao3 2008-09-25
  • 打赏
  • 举报
回复
因为在MSDN上看到解释


呈现 GridView 控件之前,该控件中的每一行必须绑定到数据源中的一条记录。将某个数据行(用 GridViewRow 对象表示)绑定到 GridView 控件中的数据以后,将引发 RowDataBound 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如修改绑定到该行的数据的值)。
而且例子::
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";

}

}
wuhongyao3 2008-09-25
  • 打赏
  • 举报
回复
啊。忘记了。。 还有一点不明白

1种 //string s = e.Row.Cells[0].Text;
//Label mylabel = new Label();
//mylabel = (Label)e.Row.FindControl("Test");
2种//string s = mylabel.Text.Trim();
3种string s = DataBinder.Eval(e.Row.DataItem, "Title").ToString().Trim();
//e.Row.Cells[0].Text = s.Length > 20 ? s.Substring(0, 20) : s.Trim();

这三种。

为什么我第一种在RowDataBound 事件中 是空值呢。 其他两种却可以拿到呢。。
wuhongyao3 2008-09-25
  • 打赏
  • 举报
回复
1,2,3楼的都可以 。 呵呵
谢谢大家的帮助。
结贴了。
skyblackhole 2008-09-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 waltguo 的回复:]
C# codeusing System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
/// <summary>
/// Strings 的摘要说明
/// </summary>
public class Strings
{
public Strings()
{
//

[/Quote]
来晚了……
推荐使用以上写的方法,因其对中英/文进行过处理!
waltguo 2008-09-25
  • 打赏
  • 举报
回复
看到美女程序员了,真不容易、就是不知道是真的相片还是假的。
namhyuk 2008-09-24
  • 打赏
  • 举报
回复
<%# GetSubString(Eval("NewsTitle").ToString(), 10)%>

public static string GetSubString(string str, int n)
{
return (str.Length > n) ? str.Substring(0, n - 1) + "..." : str;
}
kaigefengchen 2008-09-24
  • 打赏
  • 举报
回复
第一种当你为s赋值时,其实数据还没有绑定到Gridview上。
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Info").ToString().Substring(0,10)%>
</ItemTemplate>
你这样试试,只显示前是个字符
我曾经这样写过

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧