以下範例為 C# Mail 發送
// 引用命名空間 System.Net.Mail;
using System.Net.Mail;
// 其他程式碼
...
// sender = 寄信者,reciver = 收信者
MailMessage message = new MailMessage(sender, reciver);
// 支援 HTML 語法
message.IsBodyHtml = true;
// E-Mail 編碼
message.BodyEncoding = System.Text.Encoding.UTF8;
// E-Mail 主旨 subject = 主旨
message.Subject = subject;
// E-Mail 內容 msg = 內容
message.Body = msg;
// 設定 SMTP Server 和 Port,此範例 SMTP Server = 127.0.0.1,Port = 25
SmtpClient smtpClient = new SmtpClient("127.0.0.1", 25);
// 設定 SMTP 帳號及密碼 smtpAccount = SMTP帳號,smtpPassword = SMTP密碼
smtpClient.Credentials = new System.Net.NetworkCredential(smtpAccount, smtpPassword);
// E-Mail 寄出
smtpClient.Send(message);
紅色字體為自定參數
流風羽 發表在 痞客邦 留言(0) 人氣(2,212)
在 C# ListView 中的 EmptyDataTemplate 中的控制項
以及在 LayoutTemplate 中的控制項
取法稍略不同
// EmptyDataTemplate
if (ListView1.Items.Count == 0)
{
string text = ((TextBox) ListView1.Controls[0].FindControl("TextBox1")).Text;
}
else
{
string text = ((TextBox) ListView1.FindControl("TextBox1")).Text;
}
由於 EmptyDataTemplate 是 ListView 無資料才會動態產生
故可以用 ListView.Items.Count 來判別是否有資料
EmptyDataTemplate 在尋找控制項時必須先指定 Controls[0]
才可以找到 EmptyDataTemplate 中的控制項
流風羽 發表在 痞客邦 留言(0) 人氣(1,079)
在 Upadtepanel 中
Fileupload 會無法正常運作
也就是說 Postback 時無法取得檔案
為了達到非同步
又希望可以有檔案上傳功能
我們可以把 Fileupload 寫在另外一頁 aspx 中
UploadPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadPage.aspx.cs" Inherits="Web.UploadPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" Width="30em" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
AjaxFileUpload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxFileUpload.aspx.cs" Inherits="Web.AjaxFileUpload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<asp:ScriptManager runat="server">
</asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<form id="form1" runat="server">
<div>
<iframe id="Iframe1" frameborder="0" name="I1" height="32px" width="100%" src="UploadPage.aspx"></iframe>
</div>
</form>
</ContentTemplate>
</asp:UpdatePanel>
</body>
</html>
利用 iFrame 將檔案上傳的 aspx 置入
如此一來可利用主視窗呼叫 iFrame 中的 Button Click
便可解決 FileUpload 在 UpdatePanel 中無效的問題
流風羽 發表在 痞客邦 留言(0) 人氣(1,168)
在開發 CSV 檔案下載功能時
明明指定了編碼為 UTF-8
怎麼下載下來都是亂碼
發現檔頭少了 BOM
加上 BOM 後 Excel 就可正確顯示中文
// 指定編碼
context.Response.HeaderEncoding = Encoding.UTF8;
// 加入BOM
context.Response.BinaryWrite(new byte[] { 0xEF, 0xBB, 0xBF });
流風羽 發表在 痞客邦 留言(0) 人氣(1,732)
當我們需要的數字格式為 0001, 0002...
如何使 1, 2 前面自動補零
使用 String.PadLeft 即可達成
使用方法
string.PadLeft([len], [char])
使用範例
string str = "1";
str = str.PadLeft(4, '0');
以上 可得到 "0004"
流風羽 發表在 痞客邦 留言(0) 人氣(811)