這幾天發現一個小套件對Debug還蠻實用的,可以將程式碼摺疊起來,看程式邏輯時就會比較方便
安裝方式可透過Nuget
Visual Studo 2010/2012也有支援,詳細效果與版本介紹可擴充功能頁面查看
http://visualstudiogallery.msdn.microsoft.com/6c3c5dec-1534-4c42-81b1-cfd4615fd0e9
至於Javascript則可下載 JavaScript outlining
ASP.NET、C#、MVC、Angular、jQuery…Sharing what I Know !
Share your knowledge. It’s a way to achieve immortality.
My Website:http://kyleshen.com
這幾天發現一個小套件對Debug還蠻實用的,可以將程式碼摺疊起來,看程式邏輯時就會比較方便
安裝方式可透過Nuget
Visual Studo 2010/2012也有支援,詳細效果與版本介紹可擴充功能頁面查看
http://visualstudiogallery.msdn.microsoft.com/6c3c5dec-1534-4c42-81b1-cfd4615fd0e9
至於Javascript則可下載 JavaScript outlining
public Form1()
{
InitializeComponent();
foreach (var control in this.Controls)
{
if (control.GetType() == typeof(Button))
{
Button btn = (Button)control;
btn.GotFocus += btn_GotFocus;
btn.LostFocus += btn_LostFocus;
}
}
}
void btn_LostFocus(object sender, EventArgs e)
{
//改變該Button的樣式
Button btn = (Button)sender;
btn.BackColor = SystemColors.Control;
}
void btn_GotFocus(object sender, EventArgs e)
{
//改變該Button的樣式
Button btn = (Button)sender;
btn.BackColor = System.Drawing.Color.Yellow; //變為黃色
}
這算是一個小記錄,起因是在Page Load的時候有的textbox要用JavaScript來做focus,但發現IE只要點選頁面其他元素時,在按F5(重新整理),則focus就會失效:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js "></script>
</head>
<body>
<input id="txtName" type="text" />
</body>
<script type="text/javascript">
$('#txtName').focus();
</script>
</html>
以上程式在Firefox和Chome都沒問題,但在IE Reflash就會失效,看起來好像IE載入元素、JavaScirpt的順序跟其他瀏覽器不太一樣(待確認),導致focus()事件在html document完成之前就先載入了,所以發現主控台會有以下錯誤
最後解決的方式是加個timeout,延遲執行focus
<script type="text/javascript">
setTimeout(function () {
$('#txtName').focus();
}, 500);
</script>
早期.Net開發者習慣使用ADO.NET來時做資料儲存,故像DataReader or DataTable來儲存資料其實還蠻常見的,但這種都屬於弱型別,個人覺得在維護上其實還蠻困難的,故Entity Freamwork出來後改變了很多開發模式,將資料物件化也變成一件很正常的事情,現在在維護舊的WebForm系統,即使使用ADO.NET Fill一個DataTable,我還是會將這些資料轉換成自定義的Class,也許多了一道工,但我覺得差這一點點時間,讓維護上更直覺是很重要的事,畢竟程式不是永遠都你一個人在維護而已阿 ! 故此篇文章將會將實務使用的function來做分享。
自定義的class
而這是取得DataTable資料的function,為求Demo而塞一些假資料
而實際轉換的function如下,而通常我會新增一個class,讓其他程式能共用
public static class DataTableExtensions
{
public static IList<T> ToList<T>(this DataTable table) where T : new()
{
IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
IList<T> result = new List<T>();
//取得DataTable所有的row data
foreach (var row in table.Rows)
{
var item = MappingItem<T>((DataRow)row, properties);
result.Add(item);
}
return result;
}
private static T MappingItem<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
{
T item = new T();
foreach (var property in properties)
{
if (row.Table.Columns.Contains(property.Name))
{
//針對欄位的型態去轉換
if (property.PropertyType == typeof(DateTime))
{
DateTime dt = new DateTime();
if (DateTime.TryParse(row[property.Name].ToString(), out dt))
{
property.SetValue(item, dt, null);
}
else
{
property.SetValue(item, null, null);
}
}
else if (property.PropertyType == typeof(decimal))
{
decimal val = new decimal();
decimal.TryParse(row[property.Name].ToString(), out val);
property.SetValue(item, val, null);
}
else if(property.PropertyType == typeof(double))
{
double val = new double();
double.TryParse(row[property.Name].ToString(), out val);
property.SetValue(item, val, null);
}
else if (property.PropertyType == typeof(int))
{
int val = new int();
int.TryParse(row[property.Name].ToString(), out val);
property.SetValue(item, val, null);
}
else
{
if (row[property.Name] != DBNull.Value)
{
property.SetValue(item, row[property.Name], null);
}
}
}
}
return item;
}
}
使用方式:
List<UserInfo> list = new List<UserInfo>();
DataTable dt = GetDataTable();
var result = DataTableExtensions.ToList<UserInfo>(dt).ToList();
list = result.OrderBy(c => c.UpdateDate).ToList();
此.cs檔可至此連結下載 (如有Bug請見諒,或自行擴充~)
$('#ddlSkills').change(function(){
if($(this).val() == 'Others') {
sessionStorage.setItem('ShowOthers', "Y");
$('#divOthers').show();
}
else {
sessionStorage.setItem('ShowOthers', "N");
$('#divOthers').hide();
}
});
$(function(){
//如果SessionStorage為Y時,秀出textarea
if (sessionStorage.getItem("ShowOthers") == "Y") {
$('#divOthers').show();
}
});
$(function(){
//新增資料到陣列
var Skills = new Array();
Skills.push('C#');
Skills.push('ASP.NET');
Skills.push('JQuery');
var Json = JSON.stringify(Skills);
sessionStorage.setItem('JsonStr', Json);
$('#DivJson').html(Json); //秀出Json字串的Div
});
工欲善其事,必先利其器,自從Chome出現之後已經取代我用firefox的習慣,當做主要開發的瀏覽器,而Chome其實不只是只有案F12開啟開發人員工具那麼簡單,還能下載很多的應用程式來搭配使用,以下就分享一些我常用的應用程式,文章會不定期更新!
安裝方式很簡單,點選連結後安裝即可。(如連結失效,請到Google商店輸入關鍵字)
以上,隨著更新越來越多我會在分門別類的整理,也歡迎留言告知我。
回答論壇問題順便做個紀錄,「SQL字串找尋問題」
如何將下列資料用select語法取出B開頭的資料呢?
| ID | Data |
| 001 | A000,B000,C000 |
| 002 | B000,D000,E000 |
希望結果:
001 B000
002 B000
最主要先切割字串來分析,首先搭配之前寫的備忘錄,通常我會在SQL建立一個切割字串的函數「在T-SQL裡面切割字串(Spilt String)」
接著就能將這個函數回傳的table join起來,再進行篩選
Declare @table Table
(
ID VARCHAR(3),
data NVARCHAR(15)
)
Insert INTO @table values ('001','A000,B000,C000')
Insert INTO @table values ('002','B000,D000,E000')
select tb.id,tb2.data from @table tb
CROSS APPLY Split(data,',') AS tb2
where tb2.data like 'B%'
特別APPLY是SQL 2005之後才提供的語法,所以要注意版本相容性
APPLY又分為CROSS APPLY和OUTER APPLY,
CROSS APPLY 可以把發成我們常用的inner join ,會查詢有交集的結果
OUTER APPLY就相當於left join,會以左邊的資料表為主
詳細的範例可以以上面的MSDN連結延伸閱讀。
執行結果: