2012年9月23日 星期日
在TextBox按Enter時呼叫特定Button
在TextBox的KeyDown事件裡面判斷到Enter時呼叫Button
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1.Focus();
button1_Click(sender,e);
}
}
2012年9月8日 星期六
Cookie應用
功能說明:
1.畫面上放一個TextBox、CheckBox、Button
2.第一次登入時若勾選CheckBox且TextBox的值正確,將TextBox的值寫入Cookie
3.登入時檢查Cookie使否有值,若有、自動將Cookie值複製到TextBox,且將CheckBox打勾
4.登入時若將CheckBox勾選取消、會連同Cookie資料清除
5.按下Button後檢查Textbox是否正確,若正確、Button.Text="OKOK"
6.若TextBox值不正確、Cookie資料清除
程式範例:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//取cookie資料
if (Request.Cookies["txt1"] != null)
{
CheckBox1.Checked = true;
TextBox2.Text = (Request.Cookies["txt1"].Value.ToString());
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//狀況1:若已勾記住帳號,重新寫入cookie
if (CheckBox1.Checked)
{
if (TextBox2.Text == "aa")
{
Button1.Text = "OKOK!";
//將正確資料寫入cookie
HttpCookie cookie = new HttpCookie("txt1");
cookie.Value = TextBox2.Text;
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);
}
}
//狀況2:取消勾選cookie且cookie若有資料須清空
//因為不能直接刪除cookie,只要讓cookie過期變成失效,系統會自行刪除cookie
if(!CheckBox1.Checked)
{
if (Request.Cookies["txt1"] != null)
{
HttpCookie cookie = new HttpCookie("txt1");
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);
}
if (TextBox2.Text == "aa")
Button1.Text = "OKOK!";
}
}
1.畫面上放一個TextBox、CheckBox、Button
2.第一次登入時若勾選CheckBox且TextBox的值正確,將TextBox的值寫入Cookie
3.登入時檢查Cookie使否有值,若有、自動將Cookie值複製到TextBox,且將CheckBox打勾
4.登入時若將CheckBox勾選取消、會連同Cookie資料清除
5.按下Button後檢查Textbox是否正確,若正確、Button.Text="OKOK"
6.若TextBox值不正確、Cookie資料清除
程式範例:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//取cookie資料
if (Request.Cookies["txt1"] != null)
{
CheckBox1.Checked = true;
TextBox2.Text = (Request.Cookies["txt1"].Value.ToString());
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//狀況1:若已勾記住帳號,重新寫入cookie
if (CheckBox1.Checked)
{
if (TextBox2.Text == "aa")
{
Button1.Text = "OKOK!";
//將正確資料寫入cookie
HttpCookie cookie = new HttpCookie("txt1");
cookie.Value = TextBox2.Text;
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);
}
}
//狀況2:取消勾選cookie且cookie若有資料須清空
//因為不能直接刪除cookie,只要讓cookie過期變成失效,系統會自行刪除cookie
if(!CheckBox1.Checked)
{
if (Request.Cookies["txt1"] != null)
{
HttpCookie cookie = new HttpCookie("txt1");
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);
}
if (TextBox2.Text == "aa")
Button1.Text = "OKOK!";
}
}
2012年8月24日 星期五
MasterPage 簡單練習 Part1
我想要設計一個這樣的網頁:
- 畫面切割為2塊,上下各一塊,上半部約占畫面5分之1高度,放公司Logo
- 首頁會有帳號、密碼欄位
- 若帳號密碼正確,登入後將網頁轉到另一資料畫面,上半部秀出登入者帳號
作法:
- create new MasterPage.
- delete the default ContenePlaceHolder.
- insert Table at one Column and on Row ,Writing something at Header.
- insert ContenePlaceHolder.
- create new web page name Login.aspx and locate to MasterPage .
- at Login.aspx insert a new ContenePlaceHolder、TextBox、Button .
- create new Web Page name Welcome.aspx and locate to MasterPage .
- at Welcome.aspx,write something for user login.
- You can at MasterPage header add two HyperLink,first one connect to 「 Login.aspx 」,second connect to 「 Welcome.aspx 」 .
2012年8月19日 星期日
登入時的驗證碼
在google上面搜尋"asp.net 驗證碼",前面2頁,找到關於驗證碼的方法有很多種,有簡單的,有複雜的,還探討到所謂的OCR( 光學辨識技術)。
我想,大部分的人看到這名詞就會先跳過吧!很幸運的找到一篇文章值得推薦,原來在CodePlex竟然有驗證碼的控制項可直接下載來使用!
Msdn 裡面有教學文件 http://msdn.microsoft.com/zh-tw/ee854995.aspx, 裡面有提到要編輯web.config,在區段加入一些東西,嚴格的說,是放在 -> 裡面,可是我不知道這元件產生的驗證碼,能不能避開OCR....
我想,大部分的人看到這名詞就會先跳過吧!很幸運的找到一篇文章值得推薦,原來在CodePlex竟然有驗證碼的控制項可直接下載來使用!
Msdn 裡面有教學文件 http://msdn.microsoft.com/zh-tw/ee854995.aspx, 裡面有提到要編輯web.config,在
2012年6月18日 星期一
建立一個可回傳字串陣列的Method
//=== 定義 ===
static string[] GetData(string sName)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=xx;Integrated Security=True");//HR
conn.Open();
string sDate = Convert.ToString(iDate); //日期
string cmd = "select A,B FROM TABLE WHERE C = @para ";
SqlCommand sqlcmd = new SqlCommand(cmd, conn);
sqlcmd.Parameters.Add(new SqlParameter("@para", sName));
SqlDataReader dr = sqlcmd.ExecuteReader();
string[] ret = new string[2];
if (dr.HasRows)
{
while (dr.Read())
{
ret[0] = dr[0].ToString();
ret[1] = dr[1].ToString();
}
}
dr.Close();
conn.Close();
return ret;
}
//=== 呼叫陣列 ===
string[] aGetList;
aGetList = GetData("Name"); //帶入帳號,for 回傳陣列資料
2012年4月6日 星期五
符合條件的結果集,將特定欄位的字串相加
以AdvantureWorks為DB,使用Person.Contact Table
-- 目的:取出LastName='Ferrier'的人,將其FirstName字串相加
--1.計算LastName='Ferrier'的筆數
--2.建立變數:NameCount(筆數)、n(取n筆資料的第一筆)、st(存放字串)
--3.跑回圈、累加字串
declare @NameCount int --取筆數
set @NameCount = (select count(*) from Person.Contact where LastName='Ferrier')
--select @NameCount
declare @n int --第n筆
set @n = 2
declare @st varchar(1000) --名字累加
set @st = (select top 1 FirstName from Person.Contact where LastName='Ferrier' order by FirstName)
while(@n <=@NameCount)
begin
set @st = @st + ';' + (select top 1 FirstName from
(select top(@n) FirstName from Person.Contact where LastName='Ferrier' order by FirstName asc)
tmp order by FirstName desc)
set @n =@n +1
end
select @st
訂閱:
文章 (Atom)