人生求的是什麼,平平淡淡的日子,快快樂樂的生活。 我只是一個平凡的小人物,熱愛每一天,但人生總有喜怒哀樂、 酸甜苦辣,不是想分什麼偉大經驗,只是認真的記錄我的生活。 用我的經驗與你交流。
2020年2月14日 星期五
[Asp.Net]CheckBoxList的應用
在存取 CheckBoxList 時,發現不是只用 SelectedIndexChanged 取的SelectedValue
就好,因為它有[覆選。
不能當做RadioButtonList 用
本來是想找找看有沒有checked 這樣的東西,看來是沒有
突然靈光一閃,應該是所有的選項 都要掃一遍。
所以重新整理如下:
private void chk_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList chk = (CheckBoxList)sender;
string bank_id = chk.ID;
//刪除所有存檔的資料
Transcript.Row_Del(string.Format(" bank_id = '{0}' ", bank_id));
foreach (ListItem s in chk.Items)
{
if (s.Selected) //s.Selected 就是html 的checked 用在這裡
{ //新增
add_transcript(bank_id, s.Value, "m");
}
}
}
2020年2月13日 星期四
[ASP.NET]在UpdatePanel中 動態產生Trigger
常常在網頁中 動能新增event
但遇上UpdatePanel 是一個困擾
當我動態新增Trigger,系統只會作用一次
後來參考ASP.net,C#,在UpdatePanel中動態加入Trigger
原來是因為Trigger掉了
所以重新將Trigger加入 PageLoad 即可
if (this.ScriptManager1.IsInAsyncPostBack)
{
UpdatePanel1.GetType().GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(UpdatePanel1, null);
}
//==========================================================
範列
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
CheckBoxList chk = new CheckBoxList();
chk.ID = ds["rowno"].ToString();
chk.RepeatDirection = RepeatDirection.Horizontal;
chk.AutoPostBack = true;
chk.SelectedIndexChanged += new EventHandler(chk_SelectedIndexChanged);
trigger.ControlID = chk.ID; //要偵測的物件ID
trigger.EventName = "SelectedIndexChanged"; //Button的觸發事件為Click
up_list.Triggers.Add(trigger); //UpdatePanel
string[] itmes = item.Split('|');
int i = 0;
foreach (string s in itmes)
{
string[] o = s.Split(',');
ListItem litem = new ListItem(o[0], o[1]);
if (i == 0)
{
litem.Selected = true;
}
if (getSelected(ds["rowno"].ToString(), o[1]))
{
litem.Selected = true;
}
chk.Items.Add(litem);
i++;
}
td2.Controls.Add(chk); //Panel
p_list.Controls.Add(td2); //Panel
但遇上UpdatePanel 是一個困擾
當我動態新增Trigger,系統只會作用一次
後來參考ASP.net,C#,在UpdatePanel中動態加入Trigger
原來是因為Trigger掉了
所以重新將Trigger加入 PageLoad 即可
if (this.ScriptManager1.IsInAsyncPostBack)
{
UpdatePanel1.GetType().GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(UpdatePanel1, null);
}
//==========================================================
範列
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
CheckBoxList chk = new CheckBoxList();
chk.ID = ds["rowno"].ToString();
chk.RepeatDirection = RepeatDirection.Horizontal;
chk.AutoPostBack = true;
chk.SelectedIndexChanged += new EventHandler(chk_SelectedIndexChanged);
trigger.ControlID = chk.ID; //要偵測的物件ID
trigger.EventName = "SelectedIndexChanged"; //Button的觸發事件為Click
up_list.Triggers.Add(trigger); //UpdatePanel
string[] itmes = item.Split('|');
int i = 0;
foreach (string s in itmes)
{
string[] o = s.Split(',');
ListItem litem = new ListItem(o[0], o[1]);
if (i == 0)
{
litem.Selected = true;
}
if (getSelected(ds["rowno"].ToString(), o[1]))
{
litem.Selected = true;
}
chk.Items.Add(litem);
i++;
}
td2.Controls.Add(chk); //Panel
p_list.Controls.Add(td2); //Panel
2019年12月16日 星期一
[C#] string.Format 輸出格式
- JAN 27 WED 2010 00:17
[C#] string.Format輸出格式
//數字字串不足,前面補0
Response.Write(String.Format("{0:00000}", 123)); // 輸出 00123
Response.Write(String.Format("{0:D5}", 123)); // 輸出 00123
Response.Write(String.Format("{0:00000}", 123)); // 輸出 00123
Response.Write(String.Format("{0:D5}", 123)); // 輸出 00123
//數字字串不足,前後都補0
Response.Write(String.Format("{0:00000.0000}", 123.45)); // 輸出 00125.4500
Response.Write(String.Format("{0:00000.0000}", 123.45)); // 輸出 00125.4500
//每3位數加逗號
Response.Write(String.Format("{0:0,0}", 0)); // 輸出 00
Response.Write(String.Format("{0:0,0}", 1234567)); // 輸出 1,234,567 //缺點:當數字=0時,會顯示 00
Response.Write(String.Format("{0:N}", 1234567)); // 輸出 1,234,567.00
Response.Write(String.Format("{0:N0}", 1234567)); // 輸出 1,234,567
Response.Write(String.Format("{0:N4}", 1234567)); // 輸出 1,234,567.0000
Response.Write(String.Format("{0:0,0}", 0)); // 輸出 00
Response.Write(String.Format("{0:0,0}", 1234567)); // 輸出 1,234,567 //缺點:當數字=0時,會顯示 00
Response.Write(String.Format("{0:N}", 1234567)); // 輸出 1,234,567.00
Response.Write(String.Format("{0:N0}", 1234567)); // 輸出 1,234,567
Response.Write(String.Format("{0:N4}", 1234567)); // 輸出 1,234,567.0000
//電話號碼
Response.Write(String.Format("{0:(###) ####-####}", 88012345678)); // 輸出(880)1234-5678
Response.Write(String.Format("{0:(###) ####-####}", 88012345678)); // 輸出(880)1234-5678
//金額表示方式
Response.Write(String.Format("{0:C}", 0)); // 輸出 NT$0.00
Response.Write(String.Format("{0:C}", 12345)); // 輸出 NT$12,345.00
Response.Write(String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 0)); // 輸出 Zero
Response.Write(String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 1234.50)); // 輸出 $1,234.50
Response.Write(String.Format("{0:C}", 0)); // 輸出 NT$0.00
Response.Write(String.Format("{0:C}", 12345)); // 輸出 NT$12,345.00
Response.Write(String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 0)); // 輸出 Zero
Response.Write(String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 1234.50)); // 輸出 $1,234.50
//百分比
Response.Write(String.Format("{0:0%}", 17 / (float)60)); // 輸出 28%
Response.Write(String.Format("{0:0%}", 17 / (float)60)); // 輸出 28%
//到小數2位的百分比
Response.Write(String.Format("{0:0.00%}", 17 / (float)60)); // 輸出 28.33%
Response.Write(String.Format("{0:0.00%}", 17 / (float)60)); // 輸出 28.33%
//取小數第4位,並對第5位做四捨五入
Response.Write(String.Format("{0:#,0.####}", 1234.56789)); // 1,234.5679
Response.Write(String.Format("{0:#,0.####}", 1234.56789)); // 1,234.5679
//小數點不足4位不補0
Response.Write(String.Format("{0:0.####}", 1234.567)); // 1234.567
Response.Write(String.Format("{0:0.####}", 1234.567)); // 1234.567
來源
[C#]正則表式
/// <summary>
/// 是否是有小數(不論正負)
/// </summary>
/// <param name="examine"></param>
/// <returns></returns>
public static bool IsFloat(string examine)
{
bool chk = false;
if (!string.IsNullOrEmpty(examine))
{
Regex NumberPattern = new Regex(@"^(-[0-9]*|[0-9]*)$|^(-[0-9]*|[0-9]*)(\.{1}\d*)$");
chk = NumberPattern.IsMatch(examine);
}
return chk;
}
public static bool getRegex(string examine, string regex)
{
Regex NumberPattern = new Regex(regex);
return NumberPattern.IsMatch(examine);
}
因為
c#並無判斷數字的功能,但網頁上 使用的正則正需求不符,還是自己寫比較快
"^(-[0-9]* | [0-9]*)$ | ^(-[0-9]* | [0-9]*)(\.{1}\d*)$"
^ :開頭 表示
$ :結束
| :或是
*:無限次重覆
( ): 類似範圍
"^(-[0-9]* | [0-9]*)$ | ^(-[0-9]* | [0-9]*)(\.{1}\d*)$"
可以解釋為當數字是
"^(-[1-9]* | [0-9]*)$ 整數 不論正負
| 或是
^(-[0-9]* | [0-9]*)(\.{1}\d*)$" 小數點 不論正負
[DB][ORACLE]當資料長度>欄位長度時
雖然這是一個很笨的方法
但是還是很好用
private void test()
{
sb.Append(" SELECT DATA_LENGTH FROM ALL_TAB_COLUMNS C ");
sb.Append(" JOIN ALL_TABLES T ON C.OWNER = T.OWNER AND C.TABLE_NAME = T.TABLE_NAME");
sb.Append(" where C.OWNER = 'MAY' AND C.TABLE_NAME = 'AZF_FILE' and C.COLUMN_NAME='column_name' ORDER BY C.TABLE_NAME, C.COLUMN_ID ");
Hashtable h = m2();
ICollection key = h.Keys;
foreach (string k in key)
{
confim(k.ToUpper(), h[k].ToString());
}
}
private void confim(string key, string value)
{
int i = 0;
try
{
Console.Write($" key={key} , value={value} ");
using (OracleConnection conn = new OracleConnection(connStr))
{
conn.Open();
using (OracleCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sb.ToString().Replace("column_name", key);
OracleDataReader reder = cmd.ExecuteReader();
if (reder.Read())
{
int col_length = int.Parse(reder.GetValue(0).ToString());
int str_length = value.Length;
if (str_length > col_length)
{
Console.Write($" NG ");
}
else
{
Console.Write($" OK ");
}
}
reder.Close();
}
}
Console.WriteLine(";");
}
catch (Exception ex)
{
throw ex;
}
}
private Hashtable Marger()
{
Hashtable h = new Hashtable();
h.Add("ima01", "1173LI-F4015045B");
h.Add("ima02", "EE INDUCTOR 1.5UH RDC=0.044OHM IRMS=4.0A 5.7*5.2*1 SMD");
h.Add("ima021", "GSTM5012P-1R5M");
h.Add("ima06", "1");
h.Add("ima08", "P");
h.Add("ima09", "");
h.Add("ima10", "");
h.Add("ima24", "Y");
h.Add("ima25", "PCS");
h.Add("ima131", "Y000");
h.Add("ima133", "");
h.Add("ta_ima02", "Z000");
h.Add("ta_ima03", "V100");
h.Add("ta_ima10", "NA");
h.Add("ta_ima17", "01");
h.Add("ta_ima24", "Y");
h.Add("imaacti", "Y");
h.Add("ima03", "");
h.Add("ima04", "");
h.Add("ima07", "A");
h.Add("ima11", "");
h.Add("ima12", "1");
h.Add("ima14", "N");
h.Add("ima15", "N");
h.Add("ima17", "");
h.Add("ima19", "");
h.Add("ima21", "");
h.Add("ima23", "");
h.Add("ima27", "0");
h.Add("ima28", "0");
h.Add("ima34", "");
h.Add("ima35", "A1");
h.Add("ima36", "");
h.Add("ima37", "2");
h.Add("ima38", "");
h.Add("ima39", "5112-07");
h.Add("ima42", "");
h.Add("ima43", "");
h.Add("ima45", "1");
h.Add("ima46", "0");
h.Add("ima47", "0");
h.Add("ima48", "0");
h.Add("ima49", "0");
h.Add("ima491", "0");
h.Add("ima50", "0");
h.Add("ima51", "0");
h.Add("ima52", "0");
h.Add("ima54", "");
h.Add("ima56", "1");
h.Add("ima561", "0");
h.Add("ima562", "");
h.Add("ima59", "");
h.Add("ima60", "");
h.Add("ima61", "");
h.Add("ima62", "");
h.Add("ima64", "1");
h.Add("ima641", "");
h.Add("ima65", "");
h.Add("ima66", "");
h.Add("ima67", "");
h.Add("ima68", "");
h.Add("ima69", "");
h.Add("ima70", "N");
h.Add("ima87", "");
h.Add("ima871", "");
h.Add("ima872", "");
h.Add("ima873", "");
h.Add("ima874", "");
h.Add("ima88", "");
h.Add("ima89", "");
h.Add("ima90", "");
h.Add("ima94", "");
h.Add("ima99", "");
h.Add("ima100", "N");
h.Add("ima101", "1");
h.Add("ima102", "1");
h.Add("ima103", "1");
h.Add("ima105", "N");
h.Add("ima106", "");
h.Add("ima107", "N");
h.Add("ima108", "N");
h.Add("ima109", "");
h.Add("ima110", "1");
h.Add("ima130", "1");
h.Add("ima132", "5112-07");
h.Add("ima134", "");
h.Add("ima147", "N");
h.Add("ima148", "");
h.Add("ima903", "N");
h.Add("ta_ima01", "N");
h.Add("ima16", "99");
h.Add("ima18", "0");
h.Add("ima26", "0");
h.Add("ima261", "0");
h.Add("ima262", "0");
h.Add("ima271", "0");
h.Add("ima31", "PCS");
h.Add("ima31_fac", "1");
h.Add("ima32", "0");
h.Add("ima33", "0");
h.Add("ima40", "0");
h.Add("ima41", "0");
h.Add("ima44", "PCS");
h.Add("ima44_fac", "1");
h.Add("ima53", "0");
h.Add("ima531", "0");
h.Add("ima55", "PCS");
h.Add("ima55_fac", "1");
h.Add("ima57", "0");
h.Add("ima58", "0");
h.Add("ima63", "PCS");
h.Add("ima63_fac", "1");
h.Add("ima71", "9999");
h.Add("ima72", "0");
h.Add("ima86", "PCS");
h.Add("ima86_fac", "1");
h.Add("ima91", "0");
h.Add("ima92", "N");
h.Add("ima93", "NNNNNNNN");
h.Add("ima95", "0");
h.Add("ima77", "0");
h.Add("ima78", "0");
h.Add("ima80", "0");
h.Add("ima81", "0");
h.Add("ima82", "0");
h.Add("ima83", "0");
h.Add("ima84", "0");
h.Add("ima85", "0");
h.Add("ima852", "N");
h.Add("ima853", "N");
h.Add("ima96", "0");
h.Add("ima97", "0");
h.Add("ima98", "0");
h.Add("ima104", "0");
h.Add("ima121", "0");
h.Add("ima122", "0");
h.Add("ima123", "0");
h.Add("ima124", "0");
h.Add("ima125", "0");
h.Add("ima126", "0");
h.Add("ima127", "0");
h.Add("ima128", "0");
h.Add("ima129", "0");
h.Add("ima139", "N");
h.Add("ima140", "N");
h.Add("ima141", "0");
h.Add("ima905", "N");
h.Add("imauser", "MIS");
h.Add("imamodu", "MIS");
h.Add("imagrup", "MIS");
h.Add("ta_ima04", "N");
h.Add("ta_ima05", "");
h.Add("ta_ima06", "");
h.Add("ta_ima07", "");
h.Add("ta_ima08", "");
h.Add("ta_ima12", "N");
h.Add("ta_ima13", "N");
h.Add("ta_ima23", "N");
h.Add("ta_ima25", "N");
h.Add("ima906", "Y");
return h;
}
private Hashtable m2()
{
Hashtable h = new Hashtable();
h.Add("AZF01", "1173LI-F4015045B");
h.Add("AZFACTI", "Y");
h.Add("AZFUSER", "MIS");
h.Add("TA_AZF01", "EE INDUCTOR 1.5UH RDC=0.044OHM IRMS=4.0A 5.7*5.2*1 SMD");
h.Add("TA_AZF02", "GSTM5012P-1R5M");
h.Add("AZF02", "");
return h;
}
但是還是很好用
private void test()
{
sb.Append(" SELECT DATA_LENGTH FROM ALL_TAB_COLUMNS C ");
sb.Append(" JOIN ALL_TABLES T ON C.OWNER = T.OWNER AND C.TABLE_NAME = T.TABLE_NAME");
sb.Append(" where C.OWNER = 'MAY' AND C.TABLE_NAME = 'AZF_FILE' and C.COLUMN_NAME='column_name' ORDER BY C.TABLE_NAME, C.COLUMN_ID ");
Hashtable h = m2();
ICollection key = h.Keys;
foreach (string k in key)
{
confim(k.ToUpper(), h[k].ToString());
}
}
private void confim(string key, string value)
{
int i = 0;
try
{
Console.Write($" key={key} , value={value} ");
using (OracleConnection conn = new OracleConnection(connStr))
{
conn.Open();
using (OracleCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sb.ToString().Replace("column_name", key);
OracleDataReader reder = cmd.ExecuteReader();
if (reder.Read())
{
int col_length = int.Parse(reder.GetValue(0).ToString());
int str_length = value.Length;
if (str_length > col_length)
{
Console.Write($" NG ");
}
else
{
Console.Write($" OK ");
}
}
reder.Close();
}
}
Console.WriteLine(";");
}
catch (Exception ex)
{
throw ex;
}
}
private Hashtable Marger()
{
Hashtable h = new Hashtable();
h.Add("ima01", "1173LI-F4015045B");
h.Add("ima02", "EE INDUCTOR 1.5UH RDC=0.044OHM IRMS=4.0A 5.7*5.2*1 SMD");
h.Add("ima021", "GSTM5012P-1R5M");
h.Add("ima06", "1");
h.Add("ima08", "P");
h.Add("ima09", "");
h.Add("ima10", "");
h.Add("ima24", "Y");
h.Add("ima25", "PCS");
h.Add("ima131", "Y000");
h.Add("ima133", "");
h.Add("ta_ima02", "Z000");
h.Add("ta_ima03", "V100");
h.Add("ta_ima10", "NA");
h.Add("ta_ima17", "01");
h.Add("ta_ima24", "Y");
h.Add("imaacti", "Y");
h.Add("ima03", "");
h.Add("ima04", "");
h.Add("ima07", "A");
h.Add("ima11", "");
h.Add("ima12", "1");
h.Add("ima14", "N");
h.Add("ima15", "N");
h.Add("ima17", "");
h.Add("ima19", "");
h.Add("ima21", "");
h.Add("ima23", "");
h.Add("ima27", "0");
h.Add("ima28", "0");
h.Add("ima34", "");
h.Add("ima35", "A1");
h.Add("ima36", "");
h.Add("ima37", "2");
h.Add("ima38", "");
h.Add("ima39", "5112-07");
h.Add("ima42", "");
h.Add("ima43", "");
h.Add("ima45", "1");
h.Add("ima46", "0");
h.Add("ima47", "0");
h.Add("ima48", "0");
h.Add("ima49", "0");
h.Add("ima491", "0");
h.Add("ima50", "0");
h.Add("ima51", "0");
h.Add("ima52", "0");
h.Add("ima54", "");
h.Add("ima56", "1");
h.Add("ima561", "0");
h.Add("ima562", "");
h.Add("ima59", "");
h.Add("ima60", "");
h.Add("ima61", "");
h.Add("ima62", "");
h.Add("ima64", "1");
h.Add("ima641", "");
h.Add("ima65", "");
h.Add("ima66", "");
h.Add("ima67", "");
h.Add("ima68", "");
h.Add("ima69", "");
h.Add("ima70", "N");
h.Add("ima87", "");
h.Add("ima871", "");
h.Add("ima872", "");
h.Add("ima873", "");
h.Add("ima874", "");
h.Add("ima88", "");
h.Add("ima89", "");
h.Add("ima90", "");
h.Add("ima94", "");
h.Add("ima99", "");
h.Add("ima100", "N");
h.Add("ima101", "1");
h.Add("ima102", "1");
h.Add("ima103", "1");
h.Add("ima105", "N");
h.Add("ima106", "");
h.Add("ima107", "N");
h.Add("ima108", "N");
h.Add("ima109", "");
h.Add("ima110", "1");
h.Add("ima130", "1");
h.Add("ima132", "5112-07");
h.Add("ima134", "");
h.Add("ima147", "N");
h.Add("ima148", "");
h.Add("ima903", "N");
h.Add("ta_ima01", "N");
h.Add("ima16", "99");
h.Add("ima18", "0");
h.Add("ima26", "0");
h.Add("ima261", "0");
h.Add("ima262", "0");
h.Add("ima271", "0");
h.Add("ima31", "PCS");
h.Add("ima31_fac", "1");
h.Add("ima32", "0");
h.Add("ima33", "0");
h.Add("ima40", "0");
h.Add("ima41", "0");
h.Add("ima44", "PCS");
h.Add("ima44_fac", "1");
h.Add("ima53", "0");
h.Add("ima531", "0");
h.Add("ima55", "PCS");
h.Add("ima55_fac", "1");
h.Add("ima57", "0");
h.Add("ima58", "0");
h.Add("ima63", "PCS");
h.Add("ima63_fac", "1");
h.Add("ima71", "9999");
h.Add("ima72", "0");
h.Add("ima86", "PCS");
h.Add("ima86_fac", "1");
h.Add("ima91", "0");
h.Add("ima92", "N");
h.Add("ima93", "NNNNNNNN");
h.Add("ima95", "0");
h.Add("ima77", "0");
h.Add("ima78", "0");
h.Add("ima80", "0");
h.Add("ima81", "0");
h.Add("ima82", "0");
h.Add("ima83", "0");
h.Add("ima84", "0");
h.Add("ima85", "0");
h.Add("ima852", "N");
h.Add("ima853", "N");
h.Add("ima96", "0");
h.Add("ima97", "0");
h.Add("ima98", "0");
h.Add("ima104", "0");
h.Add("ima121", "0");
h.Add("ima122", "0");
h.Add("ima123", "0");
h.Add("ima124", "0");
h.Add("ima125", "0");
h.Add("ima126", "0");
h.Add("ima127", "0");
h.Add("ima128", "0");
h.Add("ima129", "0");
h.Add("ima139", "N");
h.Add("ima140", "N");
h.Add("ima141", "0");
h.Add("ima905", "N");
h.Add("imauser", "MIS");
h.Add("imamodu", "MIS");
h.Add("imagrup", "MIS");
h.Add("ta_ima04", "N");
h.Add("ta_ima05", "");
h.Add("ta_ima06", "");
h.Add("ta_ima07", "");
h.Add("ta_ima08", "");
h.Add("ta_ima12", "N");
h.Add("ta_ima13", "N");
h.Add("ta_ima23", "N");
h.Add("ta_ima25", "N");
h.Add("ima906", "Y");
return h;
}
private Hashtable m2()
{
Hashtable h = new Hashtable();
h.Add("AZF01", "1173LI-F4015045B");
h.Add("AZFACTI", "Y");
h.Add("AZFUSER", "MIS");
h.Add("TA_AZF01", "EE INDUCTOR 1.5UH RDC=0.044OHM IRMS=4.0A 5.7*5.2*1 SMD");
h.Add("TA_AZF02", "GSTM5012P-1R5M");
h.Add("AZF02", "");
return h;
}
2019年11月4日 星期一
[asp.net]aspx檔案中CodeFile與CodeBehind的區別
在ASP.NET中Web程式設計時,aspx Web 窗體的@page 指令有三個屬性(Inherits、CodeFile、CodeBehind),我們在新增aspx頁面時,會預設在程式碼中使用CodeBehind。那麼,CodeFile與CodeBehind的區別究竟有哪些呢?
一、CodeBehind
1、使用方式
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="index.aspx.cs" Inherits="Community.IndexHomePage.index" %>
2、專案中的所有的Code-Behind 類檔案和獨立類檔案都被編譯成一個獨立的應用程式集,這個應用程式集被放在Bin目錄下
3、釋出的時候,不需要釋出.cs檔案
4、修改某個.cs檔案,需要重新編譯整個專案,重新整理才會看到效果
5、釋出時需要釋出整個專案編譯後的DLL檔案
二、CodeFile
1、使用方式
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="Community.IndexHomePage.index" %>
2、ASP.net 需要找 CodeFile 中指定的檔案,以便動態編譯,但是找不到,所以就報錯誤
3、CodeFile指向的檔案只會在執行這個頁面時才會編譯,速度上相比CodeBehind有一定的影響
4、釋出的時候,需要釋出.cs檔案
5、修改程式碼後不用整體編譯,只重新整理頁面就可以看到效果
6、釋出時只需要釋出 新增、修改的檔案,不需要釋出整個專案的DLL,避免因為釋出影響到其它的頁面的功能
2019年10月8日 星期二
[C#]檢視文件編碼
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Threading;
using System.Text;
namespace my
{
public partial class test : System.Web.UI.Page
{
private GetValue TempClass = new GetValue();
private string TempRole = "";
public string msg = "";
protected void Page_Load(object sender, System.EventArgs e)
{
dir_seach(@"\\192.168.0.240\E_Portal1");
dir_seach(@"D:\xml");
}
public void dir_seach(string dir)
{
getFile(dir);
foreach (string f in Directory.GetDirectories(dir))
{
getFile(f);
dir_seach(f);
}
}
private void getFile(string FilePath)
{
string [] f = Directory.GetFiles(FilePath, "*.aspx.cs");
foreach (string file in f)
{
FileInfo ff = new FileInfo(file);
System.Text.Encoding x= TextEncodingType.GetFileEncodeType(file);
string show = string.Format("{1}:{0}<br> ",x,file);
Utility.log(show);
}
}
public static class TextEncodingType
{
/// <summary>
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
/// </summary>
/// <param name=“fileName“>文件路径</param>
/// <returns>文件的编码类型</returns>
public static System.Text.Encoding GetType(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
Encoding r = GetType(fs);
fs.Close();
return r;
}
/// <summary>
/// 通过给定的文件流,判断文件的编码类型
/// </summary>
/// <param name=“fs“>文件流</param>
/// <returns>文件的编码类型</returns>
public static System.Text.Encoding GetType(FileStream fs)
{
byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
Encoding reVal = Encoding.Default;
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
int i;
int.TryParse(fs.Length.ToString(), out i);
byte[] ss = r.ReadBytes(i);
if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
{
reVal = Encoding.UTF8;
}
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
{
reVal = Encoding.Unicode;
}
r.Close();
return reVal;
}
/// <summary>
/// 判断是否是不带 BOM 的 UTF8 格式
/// </summary>
/// <param name=“data“></param>
/// <returns></returns>
private static bool IsUTF8Bytes(byte[] data)
{
int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
byte curByte; //当前分析的字节.
for (int i = 0; i < data.Length; i++)
{
curByte = data[i];
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
//判断当前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
//若是UTF-8 此时第一位必须为1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception("非预期的byte格式");
}
return true;
}
public static System.Text.Encoding GetFileEncodeType(string filename)
{
try
{
System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] buffer = br.ReadBytes(2);
if (buffer[0] >= 0xEF)
{
if (buffer[0] == 0xEF && buffer[1] == 0xBB)
{
return System.Text.Encoding.UTF8;
}
else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
{
return System.Text.Encoding.BigEndianUnicode;
}
else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
{
return System.Text.Encoding.Unicode;
}
else
{
return System.Text.Encoding.Default;
}
}
else
{
return System.Text.Encoding.Default;
}
}
catch (Exception ex)
{
throw new Exception(filename, ex);
}
}
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Threading;
using System.Text;
namespace my
{
public partial class test : System.Web.UI.Page
{
private GetValue TempClass = new GetValue();
private string TempRole = "";
public string msg = "";
protected void Page_Load(object sender, System.EventArgs e)
{
dir_seach(@"\\192.168.0.240\E_Portal1");
dir_seach(@"D:\xml");
}
public void dir_seach(string dir)
{
getFile(dir);
foreach (string f in Directory.GetDirectories(dir))
{
getFile(f);
dir_seach(f);
}
}
private void getFile(string FilePath)
{
string [] f = Directory.GetFiles(FilePath, "*.aspx.cs");
foreach (string file in f)
{
FileInfo ff = new FileInfo(file);
System.Text.Encoding x= TextEncodingType.GetFileEncodeType(file);
string show = string.Format("{1}:{0}<br> ",x,file);
Utility.log(show);
}
}
public static class TextEncodingType
{
/// <summary>
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
/// </summary>
/// <param name=“fileName“>文件路径</param>
/// <returns>文件的编码类型</returns>
public static System.Text.Encoding GetType(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
Encoding r = GetType(fs);
fs.Close();
return r;
}
/// <summary>
/// 通过给定的文件流,判断文件的编码类型
/// </summary>
/// <param name=“fs“>文件流</param>
/// <returns>文件的编码类型</returns>
public static System.Text.Encoding GetType(FileStream fs)
{
byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
Encoding reVal = Encoding.Default;
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
int i;
int.TryParse(fs.Length.ToString(), out i);
byte[] ss = r.ReadBytes(i);
if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
{
reVal = Encoding.UTF8;
}
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
{
reVal = Encoding.Unicode;
}
r.Close();
return reVal;
}
/// <summary>
/// 判断是否是不带 BOM 的 UTF8 格式
/// </summary>
/// <param name=“data“></param>
/// <returns></returns>
private static bool IsUTF8Bytes(byte[] data)
{
int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
byte curByte; //当前分析的字节.
for (int i = 0; i < data.Length; i++)
{
curByte = data[i];
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
//判断当前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
//若是UTF-8 此时第一位必须为1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception("非预期的byte格式");
}
return true;
}
public static System.Text.Encoding GetFileEncodeType(string filename)
{
try
{
System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] buffer = br.ReadBytes(2);
if (buffer[0] >= 0xEF)
{
if (buffer[0] == 0xEF && buffer[1] == 0xBB)
{
return System.Text.Encoding.UTF8;
}
else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
{
return System.Text.Encoding.BigEndianUnicode;
}
else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
{
return System.Text.Encoding.Unicode;
}
else
{
return System.Text.Encoding.Default;
}
}
else
{
return System.Text.Encoding.Default;
}
}
catch (Exception ex)
{
throw new Exception(filename, ex);
}
}
}
}
}
訂閱:
文章 (Atom)