2014年11月25日 星期二

Binding DropDownList Using List Collection, Enum and DataSet in ASP.NET

資料來源:http://www.codeproject.com/Tips/303564/Binding-DropDownList-Using-List-Collection-Enum-an

Binding DropDownList with List

In this case, the DropDownList both 'Value' and 'Text' field are same.
DropDownList ddl = new DropDownList(); List countries = new List(); countries.Add("USA"); countries.Add("India"); ddl.DataSource = countries; ddl.DataBind();

Binding DropDownList with Dictionary

Dictionary States = new Dictionary(); States.Add("-1","-Select State-"); States.Add("AP", "Andhra Predesh"); States.Add("KA", "Karnataka"); States.Add("TN", "Tamilnadu"); States.Add("KL", "Kerala"); ddl.DataSource = States; ddl.DataValueField = "Key"; ddl.DataTextField = "Value"; ddl.DataBind();

Binding DropDownList with DataSet

My DataSet contains a Class table(class_id,class_name,description).
ddl.DataSource = dataset.Tables[0].DefaultView; ddl.DataValueField = "class_id"; ddl.DataTextField = "class_name"; ddl.DataBind(); ListItem item = new ListItem("-Select Class-", "-1"); ddl.Items.Insert(0, item);

Binding DropDownList with Enum

Let’s take Countries ‘enum’ as follows: enum enCountries:int{India=0,USA,UK,UAE}; Let's see how to bind the DropDownList With Enum:
ddlEnumBind.Items.Add("--Select Country--"); //get enum items to get the respective enum value string[] enumNames=Enum.GetNames(typeof(enCountries)); foreach (string item in enumNames) { //get the enum item value int value = (int)Enum.Parse(typeof(enCountries), item); ListItem listItem = new ListItem(item, value.ToString()); ddlEnumBind.Items.Add(listItem); }

2014年9月30日 星期二

[jQuery]jQuery取得JSON資料

原文:http://blog.xuite.net/ahdaa/blog1/30797195

2014年9月18日 星期四

.NET 連結 mySQL 的中文亂碼問題

資料來源:http://www.allenkuo.com/EBook5/view.aspx?TreeNodeID=13&id=1124

解法
connection string:
server=localhost;user id=[帳號]; password=[密碼];database=[資料庫]; pooling=false;CharSet=utf8;

2014年6月17日 星期二

Java中日期格式化

轉貼:http://www.blogjava.net/kelly/archive/2006/12/18/88497.html

時間格式化 
一、JAVA中日期的獲取、設置和格式化
1)JAVA提供了3個日期類:Date、Calendar和DateFormat。
Date()方法主要用於創建日期對象並獲取日期;
Calendar()方法主要用於獲取和設置日期;
DateFormat()方法主要用於創建日期格式化器,然後再由格式化器將日期轉換為各種日期格式串輸出。
2)JAVA語言規定的基準日期為格林尼治標準時間1970.1.1.00:00:00,當前日期是由基準日期開始所經歷的毫秒數轉換出來的。
3)DateFomat類在java.text包中,Date和Calendar類在java.util包中。
4)實例如下:
import java.util.*;
import java.text.*;
public class DisplayDate {
public static void main(String[] args){
Date today;
Calendar now;
DateFormat f1,f2;
String s1,s2;
System.out.println("\n顯示Date類的相關用法");
today = new Date();
System.out.println("new Date()= \t"+ today);
System.out.println("\n用DateFormat類顯示各種日期格式");
//顯示各種日期格式
f1 = DateFormat.getDateInstance();
s1 = f1.format(today);
System.out.println("DateFormat.getDateInstance()= \t"+s1);
f1 = DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA);
s1 = f1.format(today);
System.out.println("DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA)= \t"+ s1);
f1 = DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.CHINA);
s1 = f1.format(today);
System.out.println("DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.CHINA)= \t"+ s1);
f1 = DateFormat.getDateInstance(DateFormat.SHORT,Locale.CHINA);
s1 = f1.format(today);
System.out.println("DateFormat.getDateInstance(DateFormat.SHORT,Locale.CHINA)= \t" + s1);
System.out.println("\n用DateFormat類顯示各種時間格式");
//顯示各種時間格式
f1 = DateFormat.getTimeInstance();
s1 = f1.format(today);
System.out.println("DateFormat.getTimeInstance()= \t"+s1);
f1 = DateFormat.getTimeInstance(DateFormat.LONG,Locale.CHINA);
s1 = f1.format(today);
System.out.println("DateFormat.getTimeInstance(DateFormat.LONG,Locale.CHINA)= \t"+s1);
f1 = DateFormat.getTimeInstance(DateFormat.MEDIUM,Locale.CHINA);
s1 = f1.format(today);
System.out.println("DateFormat.getTimeInstance(DateFormat.MEDIUM,Locale.CHINA)= \t"+s1);
f1 = DateFormat.getTimeInstance(DateFormat.SHORT,Locale.CHINA);
s1 = f1.format(today);
System.out.println("DateFormat.getTimeInstance(DateFormat.SHORT,Locale.CHINA)= \t"+s1);
System.out.println("\n顯示Calendar的相關時間用法");
now = Calendar.getInstance();
today = now.getTime();
System.out.println("Calendar.getInstance().getTime()= \t"+ today.toString());
}
}
程式運行結果顯示如下:
顯示Date類的相關用法
new Date()= Fri May 02 13:29:32 CST 2003
用DateFormat類顯示各種日期格式
DateFormat.getDateInstance()= 2003-5-2
DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA)= 2003年5月2日
DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.CHINA)= 2003-5-2
DateFormat.getDateInstance(DateFormat.SHORT,Locale.CHINA)= 03-5-2
用DateFormat類顯示各種時間格式
DateFormat.getTimeInstance()= 13:29:32
DateFormat.getTimeInstance(DateFormat.LONG,Locale.CHINA)= 下午01時29分32秒
DateFormat.getTimeInstance(DateFormat.MEDIUM,Locale.CHINA)= 13:29:32
DateFormat.getTimeInstance(DateFormat.SHORT,Locale.CHINA)= 下午1:29
顯示Calendar的相關時間用法
Calendar.getInstance().getTime()= Fri May 02 13:29:33 CST 2003

Java處理日期時間 相加減大全

轉貼:http://www.blogjava.net/kelly/archive/2006/12/18/88497.html

java處理日期時間 相加減
JAVA
處理日期時間常用方法:

1.java.util.Calendar
Calendar
類是一個抽象類別,它為特定瞬間與一組諸如 YEARMONTHDAY_OF_MONTHHOUR 日曆欄位之間的轉換提供了一些方法,並為操作日曆欄位(例如獲得下星期的日期)提供了一些方法。瞬間可用毫秒值來表示,它是距曆元(即格林威治標準時間 1970 1 1 日的 00:00:00.000,格裡高利曆)的偏移量。

:
Java
代碼
1. Calendar cal = Calendar.getInstance();//
使用預設時區和語言環境獲得一個日曆。 
2. cal.add(Calendar.DAY_OF_MONTH, -1);//
取當前日期的前一天
3.
4. cal.add(Calendar.DAY_OF_MONTH, +1);//
取當前日期的後一天
5.
6. //
通過格式化輸出日期 
7. java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); 
8.
9. System.out.println("Today is:"+format.format(Calendar.getInstance().getTime())); 
10.
11. System.out.println("yesterday is:"+format.format(cal.getTime()));


得到2007-12-25日期:
Java
代碼
1. Calendar calendar = new GregorianCalendar(2007, 11, 25,0,0,0); 
2. Date date = calendar.getTime(); 
3. System.out.println("2007 Christmas is:"+format.format(date));

java
月份是從0-11,月份設置時要減1.

GregorianCalendar
構造方法參數依次為:年,月-1,日,時,分,秒.

取日期的部分:
Java
代碼
1. int year =calendar.get(Calendar.YEAR); 
2.
3. int month=calendar.get(Calendar.MONTH)+1; 
4.
5. int day =calendar.get(Calendar.DAY_OF_MONTH); 
6.
7. int hour =calendar.get(Calendar.HOUR_OF_DAY); 
8.
9. int minute =calendar.get(Calendar.MINUTE); 
10.
11. int seconds =calendar.get(Calendar.SECOND);


取月份要加1.

判斷當前月份的最大天數:
Java
代碼
1. Calendar cal = Calendar.getInstance(); 
2. int day=cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
3. System.out.println(day);


2.java.util.Date
Java
代碼
1. java.util.Date today=new java.util.Date(); 
2. System.out.println("Today is "+formats.format(today));


取當月的第一天:
Java
代碼
1. java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-01"); 
2. java.util.Date firstDay=new java.util.Date(); 
3. System.out.println("the month first day is "+formats.format(firstDay));

取當月的最後一天:
Java
代碼
1.  
2. Calendar cal = Calendar.getInstance(); 
3. int maxDay=cals.getActualMaximum(Calendar.DAY_OF_MONTH); 
4. java.text.Format formatter3=new java.text.SimpleDateFormat("yyyy-MM-"+maxDay); 
5. System.out.println(formatter3.format(cal.getTime()));


求兩個日期之間相隔的天數:
Java
代碼
1. java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); 
2. java.util.Date beginDate= format.parse("2007-12-24"); 
3. java.util.Date endDate= format.parse("2007-12-25"); 
4. long day=(date.getTime()-mydate.getTime())/(24*60*60*1000); 
5. System.out.println("
相隔的天數="+day);

一年前的日期:
Java
代碼
1. java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd"); 
2. java.util.Date todayDate=new java.util.Date(); 
3. long beforeTime=(todayDate.getTime()/1000)-60*60*24*365; 
4. todayDate.setTime(beforeTime*1000); 
5. String beforeDate=formatter.format(todayDate); 
6. System.out.println(beforeDate);

一年後的日期:
Java
代碼
1. java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd"); 
2. java.util.Date todayDate=new java.util.Date(); 
3. long afterTime=(todayDate.getTime()/1000)+60*60*24*365; 
4. todayDate.setTime(afterTime*1000); 
5. String afterDate=formatter.format(todayDate); 
6. System.out.println(afterDate);

10小時後的時間
Java
代碼
1. java.util.Calendar Cal=java.util.Calendar.getInstance(); 
2. Cal.setTime(dateOper); 
3. Cal.add(java.util.Calendar.HOUR_OF_DAY,10); 
4. System.out.println("date:"+forma.format(Cal.getTime()));

10小時前的時間
Java
代碼
1. java.util.Calendar Cal=java.util.Calendar.getInstance(); 
2. Cal.setTime(dateOper); 
3. Cal.add(java.util.Calendar.HOUR_OF_DAY,-10); 
4. System.out.println("date:"+forma.format(Cal.getTime()));

3.java.sql.Date
繼承自java.util.Date,是操作資料庫用的日期類型
Java
代碼
1. java.sql.Date sqlDate = new java.sql.Date(java.sql.Date.valueOf("2007-12-25").getTime());

日期比較:簡單的比較可以以字串的形式直接比較,也可使用
java.sql.Date.valueOf("2007-03-08").compareTo(java.sql.Date.valueOf("2007-03-18"))
方式來比較日期的大小.也可使用java.util.Date.after(java.util.Date)來比較.

相差時間:
long difference=c2.getTimeInMillis()-c1.getTimeInMillis();
相差天數:long day=difference/(3600*24*1000)
相差小時:long hour=difference/(3600*1000)
相差分鐘:long minute=difference/(60*1000)
相差秒: long second=difference/1000

補充:
Java
代碼
1. DateFormat df=new SimpleDateFormat("yyyy-MM-dd EE hh:mm:ss"); 
2. System.out.println(df.format(new Date())); 
3. Date date = new Date(); 
4. DateFormat shortDate=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); 
5. DateFormat mediumDate =DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); 
6. DateFormat longDate =DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); 
7. DateFormat fullDate =DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); 
8.
9. system.out.println(shortDate.format(date)); 
10. System.out.println(mediumDate.format(date)); 
11. System.out.println(longDate.format(date)); 
12. System.out.println(fullDate.format(date)); 
13.
14. 08-4-15
下午3:24 
15. 2008-4-15 15:24:31 
16. 2008
415 下午032431 
17. 2008
415 星期二 下午032431CST 
18.
19.
20. Calendar c = Calendar.getInstance(); 
21.
22. c.add(Calendar.MONTH, 1); //
目前時間加1個月 
23. System.out.println(df.format(c.getTime())); 
24.
25. c.add(Calendar.HOUR, 3); //
目前時間加3小時 
26. System.out.println(df.format(c.getTime())); 
27.
28. c.add(Calendar.YEAR, -2); //
目前時間減2 
29. System.out.println(df.format(c.getTime())); 
30.
31. c.add(Calendar.DAY_OF_WEEK, 7); //
目前的時間加7 
32. System.out.println(df.format(c.getTime())); 


2014年6月7日 星期六

一些常用的CSS整理


  1. 固定頁首功能列


  • 樣式設定為 position:fixed 並設定 top:0px 即可。

jQuery – 快速建立表格換行條紋樣式

原文:http://www.kangting.tw/2012/07/jquery.html

0. 建立 html table
   
       
            1
       
       
            HTML5 技術藍圖快速解構
       
       
            2
       
       
            建構版面  善用結構語意標籤
       
        
   

1. 現在建立換行樣式所需的 CSS :
<style>
.oddRow
{
    background:silver ;
}
.evenRow
{
    background:lightgray ;
}    
</style>

2. jQuery 程式碼:
<script>
    $(document).ready(function () {
        $('tr:odd').addClass('oddRow');
        $('tr:even').addClass('evenRow');        
    });
</script>

CSS 表格橫列樣式替換效果-Alternate table row

CSS 表格橫列樣式替換效果-Alternate table row

原文:http://www.kangting.tw/2013/10/css-alternate-table-row.html

 tr:nth-child(even) {
            background-color: #f7f7f7;
            color: black;
 }
tr:nth-child(odd) {
            background-color: silver;
            color: black;
 }

可也用jQuery實作

2014年6月6日 星期五

null或空值的判斷處理-java [轉貼 2013-7-3 17:22:41]

null或空值的判斷處理-java

(http://moodlove.blog.hexun.com.tw/86569593_d.html)


1,錯誤用法一: 

if (name == "") {
     //do something
}

2,錯誤用法二: 
if (name.equals("")) {
     //do something
}


3,錯誤用法三: 
if (!name.equals("")) {
     //do something
}


我們來解說一下:
上述錯誤用法1是初學者最容易犯,也最不容易被發現的錯誤,因為它們的語法本身沒問題,Java編譯器編譯時不報錯。但這種條件可能在運行時導致程序出現bug,永遠也不會為true,也就是時說,if塊裏的語句永遠也不會被執行。

上述用法二,用法三 的寫法,是包括很多Java熟手也很容易犯的錯誤,為什麽是錯誤的呢?也許你會感到納悶。
對,它們的寫法本身沒錯,但是,少了一個null判斷的條件,試想,如果name=null的情況下,會發生什麽後果呢?後果是,你的程序將拋出NullPointerException異常,系統將被掛起,不再提供正常服務。
當然,如果之前已經對name作了null判斷的情況例外。

正確的寫法應該先加上name != null的條件,如例:

if (name != null && !name.equals("")) {
     //do something
}

或者

if (!"".equals(name)) {//將""寫在前頭,這樣,不管name是否為null,都不會出錯。
     //do something
}


下面,我們舉一個簡單的例子:

TestNullOrEmpty.java
public class TestNullOrEmpty {

    public static void main(String[] args) {
         String value = null;
         testNullOrEmpty(value);
        
         value = "";
         testNullOrEmpty(value);
        
         value = " ";
         testNullOrEmpty(value);
        
         value = "hello me! ";
         testNullOrEmpty(value);
     }
    
    static void testNullOrEmpty(String value) {
        if (value == null ) { //正確的寫法             System.out.println("value is null. ");
         } else if ("".equals(value)) { //正確的寫法             System.out.println("value is blank but not null. ");
         } else {
             System.out.println("value is /" " + value + "/" ");
         }
        
        if (value == "") {  //NG 錯誤的寫法        //別用這種寫法         }
     }
}

編譯執行:
c:/>javac TestNullOrEmpty.java

c:/>java TestNullOrEmpty
value is null.
value is blank but not null.
value is " "
value is "hello me!"

2014年5月28日 星期三

ASP .NET menu loads slow

原文:http://www.itmasterservices.com/wordpress/2011/10/asp-net-menu-loads-slow/



  1. Replace the tag on your page with the following code:


  1. Next add a
    tag around the navigation bar:

2014年5月14日 星期三

android webview 縮放及換行

轉貼:http://blog.sina.com.cn/s/blog_4b00db8201013ypl.html

1、webview支援頁面縮放,一般通過以下設置就可滿足80%的要求
//縮放開關
webView.getSettings().setSupportZoom(true);
設置此屬性,僅支援按兩下縮放,不支持觸摸縮放(在android4.0是這樣,其他平臺沒試過)
// 設置是否可縮放
webView.getSettings().setBuiltInZoomControls(true);
如果設置了此屬性,那麼webView.getSettings().setSupportZoom(true);也默認設置為true
2、無限縮放
無限縮放,就需要使用大視圖模式,如下:
//無限縮放
webView.getSettings().setUseWideViewPort(true);設置此屬性,可任意比例縮放。
注:1、初始縮放值可這樣設置:webView.setInitialScale(initalValue);
    2、縮放後,要使內容適配螢幕,不超出螢幕外顯示,實現換行。這方面效果應該由html控制,而不是webview控制。例如
test
實現自動換行。

2014年5月5日 星期一

[.NET] 字串加密 MD5、SHA1

http://www.dotblogs.com.tw/marcus116/archive/2012/06/24/73009.aspx

2014年4月25日 星期五

MySQL SELECT INTO OUTFILE Export options



把DB資料存到檔案
http://dev.mysql.com/doc/refman/5.6/en/select-into.html

把檔案資料到進DB
http://dev.mysql.com/doc/refman/5.6/en/load-data.html

2014年4月16日 星期三

[ASP.NET]發送EMAIL詳細解說

http://www.dotblogs.com.tw/shinyo.her/archive/2011/08/29/34600.aspx

2014年4月9日 星期三

Horizontal Navigation Bar

資料來源:w3schools
<style> ul { list-style-type:none; margin:0; padding:0; overflow:hidden; } li { float:left; } a:link,a:visited { display:block; width:120px; font-weight:bold; color:#FFFFFF; background-color:#98bf21; text-align:center; padding:4px; text-decoration:none; text-transform:uppercase; } a:hover,a:active { background-color:#7A991A; } </style>
<ul> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul>

4 major principles of Object-Oriented Programming

轉貼:4 major principles of Object-Oriented Programming

Encapsulation
Accessor - An accessor is a method that is used to ask an object about itself. In
OOP, these are usually in the form of properties, which have, under
normal conditions, a get method, which is an accessor method.
Mutator - Mutators are public methods that are used to modify
the state of an object, while hiding the implementation of exactly how
the data gets modified.
Abstraction
data abstraction is nothing more than the implementation of
an object that contains the same essential properties and actions we
can find in the original object we are representing.
Inheritance
Objects can relate to eachotherwith either a “has a”, “uses a” or an “is a” relationship.
For example, LibraryAsset is a superclass, or base class, and that maintains
only the data and methods that are common to all loanable assets.
Book, magazine, audiocassette and microfilm will all be subclasses or derived classes
or the LibraryAsset class, and so they inherit these
characteristics.  The inheritance relationship is called the “is
a” relationship.
Polymorphism
Polymorphism means one name, many forms.  Polymorphism
manifests itself by having multiple methods all with the same name, but
slighty different functionality.
There are 2 basic types of polymorphism.  Overridding, also
called run-time polymorphism, and overloading, which is referred to as
compile-time polymorphism.

overriding : 字類別的方法蓋掉父類別的方法。
overloading : 同樣的方法名稱,但因帶的參數不同,而呼叫不同的方法內容。

2014年4月2日 星期三

TextBox.ReadOnly、Attributes["readonly"]及Disabled


http://blog.darkthread.net/post-2009-10-21-textbox-readonly-and-disabled.aspx

TextBox.ReadOnly = true;
TextBox.Attributes.Add("readonly", "readonly");
TextBox.Enabled = false;


在前端用 JavaScript 更改過值後,postback回server端接受的值三者皆不同,如果要收到為 JavaScript 更改過值者,應用 TextBox.Attributes.Add("readonly", "readonly"); 方式。

JavaScript Get Asp.net Textbox Value | Get Asp.net Label Value in JavaScript

http://www.aspdotnet-suresh.com/2013/10/javascript-get-aspnet-textbox-value-label-value.html

Get label value

var amount = document.getElementById("<%=lblAmount.ClientID %>").innerHTML

Get label value

var name = document.getElementById("<%=txtUserName.ClientID %>").value;


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Get Asp.net Textbox Value in JavaScript</title>
<script type="text/javascript">
function getvalues() {
var name = document.getElementById("<%=txtUserName.ClientID %>").value;
var amount = document.getElementById("<%=lblAmount.ClientID %>").innerHTML;
alert("Textbox Value: "+name+"\n" +"Label Value: "+ amount);
return false
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
UserName:<asp:TextBox ID="txtUserName" runat="server" /><br />
Amount:<asp:Label ID="lblAmount" runat="server" Text="10000"/><br />
<asp:Button ID="btnValidate" runat="server" Text="Get Values" OnClientClick="javascript:getvalues();" />
</div>
</form>
</body>
</html>