http://www.dotblogs.com.tw/puma/archive/2008/10/19/5724.aspx
介紹兩種方法...直接看範例吧..
asp.net(c#)
GridViewHyperLink.aspx
01
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewHyperLink.aspx.cs"
02
Inherits="GridViewHyperLink" %>
03
04
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05
<html xmlns="http://www.w3.org/1999/xhtml">
06
<head runat="server">
07
<title>GridViewHyperLink</title>
08
</head>
09
<body>
10
<form id="form1" runat="server">
11
<div>
12
原始資料:<br />
13
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" CellPadding="4"
14
DataKeyNames="id" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
15
<RowStyle BackColor="#EFF3FB" />
16
<Columns>
17
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
18
SortExpression="id" />
19
<asp:TemplateField HeaderText="title" SortExpression="title">
20
<ItemTemplate>
21
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("url") %>' Text='<%# Eval("title") %>'></asp:HyperLink>
22
</ItemTemplate>
23
<HeaderStyle HorizontalAlign="Left" />
24
</asp:TemplateField>
25
</Columns>
26
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
27
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
28
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
29
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
30
<EditRowStyle BackColor="#2461BF" />
31
<AlternatingRowStyle BackColor="White" />
32
</asp:GridView>
33
<br />
34
方法1:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
35
DataKeyNames="id" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
36
<RowStyle BackColor="#EFF3FB" />
37
<Columns>
38
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
39
SortExpression="id" />
40
<asp:TemplateField HeaderText="title" SortExpression="title">
41
<ItemTemplate>
42
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("url") %>' Text='<%# CheckLength(Eval("title").ToString()) %>'></asp:HyperLink>
43
</ItemTemplate>
44
<HeaderStyle HorizontalAlign="Left" />
45
</asp:TemplateField>
46
</Columns>
47
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
48
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
49
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
50
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
51
<EditRowStyle BackColor="#2461BF" />
52
<AlternatingRowStyle BackColor="White" />
53
</asp:GridView>
54
<br />
55
方法2:
56
<br />
57
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CellPadding="4"
58
DataKeyNames="id" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" OnRowDataBound="GridView2_RowDataBound">
59
<RowStyle BackColor="#EFF3FB" />
60
<Columns>
61
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
62
SortExpression="id" />
63
<asp:TemplateField HeaderText="title" SortExpression="title">
64
<ItemTemplate>
65
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("url") %>' Text='<%# Eval("title") %>'></asp:HyperLink>
66
</ItemTemplate>
67
<HeaderStyle HorizontalAlign="Left" />
68
</asp:TemplateField>
69
</Columns>
70
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
71
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
72
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
73
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
74
<EditRowStyle BackColor="#2461BF" />
75
<AlternatingRowStyle BackColor="White" />
76
</asp:GridView>
77
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
78
SelectCommand="SELECT * FROM [Table2]"></asp:SqlDataSource>
79
</div>
80
</form>
81
</body>
82
</html>
GridViewHyperLink.aspx.cs
01
using System;
02
using System.Data;
03
using System.Configuration;
04
using System.Collections;
05
using System.Web;
06
using System.Web.Security;
07
using System.Web.UI;
08
using System.Web.UI.WebControls;
09
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
public partial class GridViewHyperLink : System.Web.UI.Page
13
{
14
protected void Page_Load(object sender, EventArgs e)
15
{
16
}
17
18
public string CheckLength(string str)
19
{
20
int limit = 40;//長度限制40
21
22
if (str.Length > limit)
23
{
24
return str.Substring(0, limit) + "...";
25
}
26
else
27
{
28
return str;
29
}
30
}
31
32
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
33
{
34
if (e.Row.RowType == DataControlRowType.DataRow)
35
{
36
HyperLink h = (HyperLink)e.Row.FindControl("HyperLink1");
37
38
h.Text = CheckLength(h.Text);
39
}
40
}
41
}
執行結果:
沒有留言:
張貼留言