顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2018年5月7日 星期一

C#字串插入符號

\' for a single quote.
\" for a double quote.
\\ for a backslash.
\0 for a null character.
\a for an alert character.
\b for a backspace.
\f for a form feed.
\n for a new line.
\r for a carriage return.
\t for a horizontal tab.
\v for a vertical tab.
\uxxxx for a unicode character hex value (e.g. \u0020).
\x is the same as \u, but you don't need leading zeroes (e.g. \x20).
\Uxxxxxxxx for a unicode character hex value (longer form needed for generating surrogates).

2016年11月6日 星期日

Asp.Net取得瀏覽器版本及OS版本

取得瀏覽器版本

System.Web.HttpBrowserCapabilities browser = Request.Browser;
    string s = "Browser Capabilities\n"
        + "Type = "                    + browser.Type + "\n"
        + "Name = "                    + browser.Browser + "\n"
        + "Version = "                 + browser.Version + "\n"
        + "Major Version = "           + browser.MajorVersion + "\n"
        + "Minor Version = "           + browser.MinorVersion + "\n"
        + "Platform = "                + browser.Platform + "\n"
        + "Is Beta = "                 + browser.Beta + "\n"
        + "Is Crawler = "              + browser.Crawler + "\n"
        + "Is AOL = "                  + browser.AOL + "\n"
        + "Is Win16 = "                + browser.Win16 + "\n"
        + "Is Win32 = "                + browser.Win32 + "\n"
        + "Supports Frames = "         + browser.Frames + "\n"
        + "Supports Tables = "         + browser.Tables + "\n"
        + "Supports Cookies = "        + browser.Cookies + "\n"
        + "Supports VBScript = "       + browser.VBScript + "\n"
        + "Supports JavaScript = "     + 
            browser.EcmaScriptVersion.ToString() + "\n"
        + "Supports Java Applets = "   + browser.JavaApplets + "\n"
        + "Supports ActiveX Controls = " + browser.ActiveXControls 
              + "\n";



取得OS版本
Use Request.UserAgent
if (Request.UserAgent.IndexOf("Windows NT 5.1") > 0)
{
//xp
}
else if (Request.UserAgent.IndexOf("Windows NT 6.0") > 0)
{
//VISTA
}
else if (Request.UserAgent.IndexOf("Windows NT 6.1") > 0)
{
//7
}
else if (Request.UserAgent.IndexOf("Windows NT 6.2") > 0) 
{ 
//8
}
else if (Request.UserAgent.IndexOf("Windows NT 6.3") > 0) 
{ 
//8.1
}
else if (Request.UserAgent.IndexOf("Windows NT 10.0") > 0) 
{ 
//10
}

2015年7月21日 星期二

C#民國年西元年轉換

西元年轉民國年時,如果使用addyear(-1911)的方式,會有閏年的問題,例如2012/2/29使用addyear(-1911)會變成101/2/28,使用字串拆解的方式又過於冗長, NET Framework有提供方便的轉換方式,來解決這問題

西元年轉民國年
System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar();
DateTime dt = DateTime.Now;
Response.Write(dt.ToString(tc));


西元年日期驗證
 DateTime dt = DateTime.Now;
 DateTime.TryParse("2015/7/21", out dt) ;


民國年轉西元年 
System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar(); Response.Write(DateTime.Parse("104/7/21",tc).Date.ToString("d"));



民國年日期驗證
System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar();
DateTime result;
try
 {
     result = DateTime.ParseExact("104/7/21", "d", tc);
     Response.Write("Ok");
 }
   catch (FormatException)
{
   Response.Write("Wrong");
 }

2015年6月23日 星期二

Asp.Net DropDownList在有連接DataSourceID時,如果預設要使用自定的ListItem

首先新增預設要顯示的ListItem,然後在DropDownList中的AppendDataBoundItems="True"即可,範例如下 請選擇

2015年1月25日 星期日

要以程式的方式觸發某個事件

例如:在按鈕中要觸發另一PictureBox的Click事件 pictureBox1_Click(null, null); 或是 pictureBox1_Click(null,new EventArgs()); 如果是按鈕的話可用 Button1.PerformClick();

要判斷某個按鈕是否被按下

在KeyDown中按下某個按鈕才可執行某個動作,可用 //例如:要知道是否按下空白建 if (e.KeyCode == Keys.Space) { //要執行動作 } 如果是組合按鈕,例如:Ctrl+Alt+Q結束程式 if (e.Control == true && e.Alt==true && e.KeyCode==Keys.Q) { Application.Exit(); }

2014年12月8日 星期一

Repeater在後端時,可用foreach來取得所有Repeater裡的物件

foreach (RepeaterItem RPItem in this.Repeater.Items) { ((TextBox)RPItem.FindControl("TextBox")).Text="aa"; }

DropDownList動態給值

在後端處理中,要動態給予DropDownList的值時,需注意以下幾點 1.需先行清除已選定的值: DropDownList.ClearSelection(); 2.找出Value或Text值給予設定 DropDownList.Items.FindByValue(要設定的值).Selected = true; DropDownList.Items.FindByText(要設定的值).Selected = true;