1.TextMode讓Textbox選擇樣式

2.後台控制div關閉或開啟

3.後台眺轉頁面

4.讓GridView自動換行

5.判別是第一次進入網站則執行{}內程式碼

6.帳密確認,label點擊跳textbox

7.讓GridView正常顯示html標籤

8.密碼、信箱和手機的驗證

 

1.TextMode讓Textbox選擇樣式

html:

<asp:TextBox ID="TextBox2" Width="800px" Height="300px" runat="server" TextMode="DateTime"></asp:TextBox>

一般TextMode是SingleLine

而在這邊換成可選擇日期時間

但需要瀏覽器有支援才有

 

2.後台控制div關閉或開啟

html:

 <div id="chatdiv" runat="server">

</div>

id="chatdiv" 辨別用

runat="server" 把id傳到後台

cs:

 chatdiv.Style["display"] = "none"; 關閉
 chatdiv.Style["display"] = "block";開啟

主要利用css的display去做開關

 

3.後台眺轉頁面

cs:

 Response.Redirect("~/Chat.aspx");

 

4.讓GridView3自動換行

cs:

GridView3.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");

 

5.判別是第一次進入網站則執行{}內程式碼

cs:

 if (!IsPostBack)
{          
}

 

6.帳密確認,label點擊跳textbox

cs:

string small_name;
string pa;
string anpa;
string an;
string power;

static string connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["連結字串"].ConnectionString;
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);

        protected void Button1_Click1(object sender, EventArgs e)
        {
            an = TextBox1.Text;
            pa = TextBox2.Text;
            power = "";
            small_name = "";

           
            conn.Open();
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("Select TOP 1 暱稱,rtrim(密碼),權限 From member Where 帳號='" + an + "'", conn);

            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            try
            {
                if (an == "administer" && pa == "ar")
                {
                    Response.Redirect("RootAccount.aspx");
                }
                else
                {
                    if (dr.HasRows)
                {
                    dr.Read();
                    small_name = dr[0].ToString();
                    anpa = dr[1].ToString();
                    power = dr[2].ToString();
                    if (anpa == pa)
                    {
                        Session["name"] = small_name;
                        Response.Redirect("Default.aspx");
                    }                                       
                    else
                    {
                        Label1.Text = "您帳密有錯誤";
                    }
                }
              }
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }


解說:

small_name:撈取資料庫取得使用者暱稱

pa:讀取使用者所輸入的密碼

an:讀取使用者所輸入的帳號

anpa:撈取資料庫取得的密碼

rtrim(密碼) :清除撈到密碼資料右邊的空白

if (an == "administer" && pa == "ar"):綁死管理者的帳密去做登入

Session["name"] = small_name;:利用Session["name"]顯示暱稱

if (dr.HasRows):是否取得資料

 

html:

<label>帳號<asp:TextBox ID="TextBox1"  class="form-control"  placeholder="Account Number" runat="server"></asp:TextBox></label>

<label>密碼<asp:TextBox ID="TextBox2" type="password" class="form-control" placeholder="Password" runat="server"></asp:TextBox></label>

<asp:Button ID="Button1"  class="btn btn-success" runat="server" Text="登入" OnClick="Button1_Click1" />

<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br/>

<label>這樣寫,可以點擊文字就跳到textbox做輸入

 

7.讓GridView正常顯示html標籤

html:

<asp:BoundField DataField="內容" HeaderText="內容" SortExpression="內容" HtmlEncode="false" />

HtmlEncode="false" 加上這段即可

 

8.密碼、信箱和手機的驗證

html:
<label>密碼:<asp:TextBox ID="TextBox4" runat="server" class="form-control"></asp:TextBox></label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="※請輸入密碼" ControlToValidate="TextBOx4" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="※需要6字元" 
ValidationExpression="^.*(?=.{6,}).*$" ControlToValidate="TextBox4" Display="Dynamic"></asp:RegularExpressionValidator>


<label>信箱:<asp:TextBox ID="TextBox5" runat="server" class="form-control"></asp:TextBox></label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="※請輸入信箱" ControlToValidate="TextBox5" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="※需要是E-MAIL格式" 
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="TextBox5" Display="Dynamic"></asp:RegularExpressionValidator>

<label>手機:<asp:TextBox ID="TextBox7" runat="server" class="form-control"></asp:TextBox></label><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="※請輸入手機號碼" ControlToValidate="TextBox7" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="※需10碼" 
ValidationExpression="[0-9]{10}" ControlToValidate="TextBox7" Display="Dynamic"></asp:RegularExpressionValidator>

 

RequiredFieldValidator是驗證是否空白

RegularExpressionValidator是自訂格式驗證

ControlToValidate="TextBox4"只哪一個id要驗證

Display="Dynamic"假如有兩種驗證的話,有這個才不會空一格

ValidationExpression="" ""裡面的內容就是自訂內容,可以查怎麼編寫!

http://rubylin327.pixnet.net/blog/post/379548526-%E3%80%8A%E7%A8%8B%E5%BC%8F%E3%80%8Basp.net-c%23-%E5%88%9D%E5%AD%B8%E8%80%85%E7%AD%86%E8%A8%98-%E5%89%8D%E7%AB%AF%E9%A9%97%E8%AD%89validatio

如有不妥在煩請告知!

 

cs:

if (Page.IsValid==true)

{}

假如驗證通過執行{}內程式碼
 

 

arrow
arrow

    馬達加斯加 發表在 痞客邦 留言(0) 人氣()