2012年2月2日 星期四
[ASP.NET]限制GridView裡HyperLink文字的長度,超出長度以"..."表示
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
}
執行結果:
介紹兩種方法...直接看範例吧..
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
}
執行結果:
ASP.NET + Google Map
我是這樣做的(如上圖),點了地圖會開另一網頁show地圖,把地址傳到show圖的頁面(showmap.aspx)
showmap.aspx 如下
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Qaddress = Server.UrlDecode(Request.QueryString("ADD"))
address.Value = Qaddress
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "GetMap", "tranAddress(""" & Qaddress & """);", True)
End Sub
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/standard.css" rel="stylesheet" type="text/css" />
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">
function tranAddress(address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, Radd);
}
function Radd(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var fenway = new google.maps.LatLng(results[0].geometry.location);
var mapOptions = {
center: fenway,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: document.getElementById("address").value
});
}
else { alert(status); }
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="text" size="60" id="address" runat="server" readonly="readonly" />
<div name="map_canvas" id="map_canvas" style="width: 800px; height: 600px"></div>
</form>
</body>
</html>
如何使用Gmail幫我們發信
只要四行呼叫Gmail發信
- //建立 SmtpClient 物件 並設定 Gmail的smtp主機及Port
- System.Net.Mail.SmtpClient MySmtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
- //設定你的帳號密碼
- MySmtp.Credentials = new System.Net.NetworkCredential("YourID", "YourPassword");
- //Gmial 的 smtp 必需要使用 SSL
- MySmtp.EnableSsl = true;
- //發送Email
- MySmtp.Send("發送者, "收件者, "C# Gmail發信測試", "文件內容");
◆萬用型email發送方式(支援html)
- MailMessage mail = new MailMessage();
- //前面是發信email後面是顯示的名稱
- mail.From = new MailAddress("ooxx@gmail.com","demoshop");
- //收信者email
- mail.To.Add("useremail@mail.com");
- //設定優先權
- mail.Priority = MailPriority.Normal;
- //標題
- mail.Subject = "subject";
- //內容
- mail.Body = "body";
- //內容使用html
- mail.IsBodyHtml = true;
- //設定gmail的smtp
- SmtpClient MySmtp = new SmtpClient("smtp.gmail.com", 587);
- //您在gmail的帳號密碼
- MySmtp.Credentials = new System.Net.NetworkCredential("ooxx@gmail.com", "123456789");
- //開啟ssl
- MySmtp.EnableSsl = true;
- //發送郵件
- MySmtp.Send(mail);
- //放掉宣告出來的MySmtp
- MySmtp = null;
- //放掉宣告出來的mail
- mail.Dispose();
♥小提醒:如果您需要改變郵件編碼方式請修改以下部份(本範例編碼為簡體中文)
■將顯示名稱改變編碼
- mail.From = new MailAddress("ooxx@gmail.com", "demoshop", System.Text.Encoding.GetEncoding("GB2312"));
■將標題和內文改變編碼
訂閱:
文章 (Atom)