WebForm 內建對象、資料增刪改、狀態保持

來源:互聯網
上載者:User

標籤:

一、內建對象

1.Response對象:響應請求
Response.Write("<script>alert(‘添加成功!‘)</script>"); → 輸出
Response.Redirect("Default.aspx"); → 跳轉頁面

2.Request對象:擷取請求
Request["key"]來擷取傳遞過來的值 → key:定義的名字

3.QueryString:地址欄資料傳遞 ?key=value&key=value
注意事項:不需要保密的東西可以傳
不要傳過長東西,因為長度有限,過長會造成資料丟失

二、資料的增刪改

【添加】

在主介面添加一個“添加按鈕”串連到新視窗添加頁面

 

<input id="btn1" type="button" value="添加使用者" /><script>    document.getElementById("btn1").onclick = function () {         window.open("TianJia.aspx", "_blank");     };</script>

 

驗證兩次輸入的密碼是否一致 不一致把結果交給label1顯示,按鈕不提交

  <script type="text/javascript">        window.onload = function () {            document.getElementById("Button1").onclick = function () {                var pwd1 = document.getElementById("TextBox2").value;                var pwd2 = document.getElementById("TextBox3").value;                if (pwd1 != pwd2) {                    document.getElementById("Label1").innerText = "兩次密碼不一致!";                    return false;                }            };        };    </script>    <style type="text/css">        #Label1 {            color: red;        }    </style>

填寫完點擊“添加”提交 

this.opener.location.href=‘Default.aspx‘:跨介面重新整理首頁面

    void Button1_Click(object sender, EventArgs e)    {        //1、構建一個Users對象        Users u = new Users();        u.UserName = TextBox1.Text;        u.PassWord = TextBox3.Text;        u.NickName = TextBox4.Text;        u.Sex = Convert.ToBoolean(RadioButtonList1.SelectedItem.Value);        string date = DropDownList1.SelectedValue + "-" + DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue;        u.Birthday = Convert.ToDateTime(date);        u.Nation = DropDownList4.SelectedItem.Value;        //2、將此對象添加到資料庫去        bool ok = new UsersData().Insert(u);        //3、提示添加成功        if (ok)        {            Response.Write("<script>alert(‘添加成功!‘)</script>");            Response.Write("<script>this.opener.location.href=‘Default.aspx‘;this.close();</script>");        }        else        {            Response.Write("<script>alert(‘添加失敗!‘)</script>");        }    }

 

 

【刪除】

在首頁面資料顯示中添加一列刪除,點擊刪除,則當前視窗開啟Delete.aspx頁面執行刪除代碼後跳轉到首頁面,有種重新整理的效果

刪除列:<td><a href="Delete.aspx?un=<%#Eval("UserName") %>">刪除</a></td>

刪除頁面的代碼:

    protected void Page_Load(object sender, EventArgs e)    {        //1、獲得要刪除的主索引值,username        string Uname = Request["un"].ToString();        //2、刪除        new UsersData().Delete(Uname);        //3、調回顯示頁面        Response.Redirect("Default.aspx");    }

 

【修改】

首頁面資料顯示添加一列修改:<td><a href="XiuGai.aspx?un=<%#Eval("UserName") %>" target="_blank">修改</a></td>

修改頁面資料顯示:

    string pwd = "";    protected void Page_Load(object sender, EventArgs e)    {        //1、將傳過來的主索引值接收        string uname = Request["un"].ToString();        //2、通過主索引值將對象查出來        Users u = new UsersData().Select(uname);                pwd = u.PassWord;//定義一個變數=原密碼 當使用者不修改密碼時用        if (!IsPostBack)        {            //重新給下拉式清單填資料            for (int i = DateTime.Now.Year; i >= 1900; i--)            {                ListItem li = new ListItem(i.ToString(), i.ToString());                DropDownList1.Items.Add(li);            }            for (int i = 1; i <= 12; i++)            {                ListItem li = new ListItem(i.ToString(), i.ToString());                DropDownList2.Items.Add(li);            }            for (int i = 1; i <= 31; i++)            {                ListItem li = new ListItem(i.ToString(), i.ToString());                DropDownList3.Items.Add(li);            }            DropDownList4.DataSource = new NationData().Select();//民族            DropDownList4.DataTextField = "NationName";            DropDownList4.DataValueField = "NationCode";            DropDownList4.DataBind();                       //3、將對象中的資料繫結到每一個控制項上去            Label2.Text = u.UserName;//帳號 唯讀            TextBox4.Text = u.NickName;                        foreach (ListItem li in RadioButtonList1.Items)            {                if (u.Sex)                {                    if (li.Value == "True")                    {                        li.Selected = true;                    }                }                else                {                    if (li.Value == "False")                    {                        li.Selected = true;                    }                }            }            //選中年月日            DropDownList1.SelectedValue = u.Birthday.Year.ToString();            DropDownList2.SelectedValue = u.Birthday.Month.ToString();            DropDownList3.SelectedValue = u.Birthday.Day.ToString();            DropDownList4.SelectedValue = u.Nation;//取民族          }        Button1.Click += Button1_Click;//修改按鈕點擊事件    }

修改按鈕代碼:

    void Button1_Click(object sender, EventArgs e)    {        //1、構建一個Users對象        Users u = new Users();        u.UserName = Label2.Text;        if (TextBox2.Text == "" && TextBox3.Text == "")        {            u.PassWord = pwd;        }        else        {            u.PassWord = TextBox3.Text;        }        u.NickName = TextBox4.Text;        u.Sex = Convert.ToBoolean(RadioButtonList1.SelectedItem.Value);        string date = DropDownList1.SelectedValue + "-" + DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue;        u.Birthday = Convert.ToDateTime(date);        u.Nation = DropDownList4.SelectedItem.Value;        //2、將此對象添加到資料庫去        bool ok = new UsersData().Update(u);        //3、提示修改成功        if (ok)        {            Response.Write("<script>alert(‘修改成功!‘)</script>");            Response.Write("<script>this.opener.location.href=‘Default.aspx‘;this.close();</script>");        }        else        {            Response.Write("<script>alert(‘修改失敗!‘)</script>");        }    }

 

三、登陸狀態保持

【Cookies】

在使用者電腦的硬碟上儲存的一段文本

http協議包括瀏覽器,允許網站在使用者電腦上以Cookies的形式來臨時儲存資料

如果沒有設定儲存時間,會話cookies時:
1、如果你20分鐘內沒有再次重新整理頁面,那麼此cookies就會自動刪除掉
2、噹噹前訪問串連中斷,如關閉瀏覽器,那麼cookies會自動刪除

作用:保持使用者的登陸狀態

用法:

1、擷取帳號:Response.Cookies["user"].Value = TextBox1.Text;

2、給該帳號設定登入保持的到期時間:Response.Cookies["user"].Expires = DateTime.Now.AddDays(7);

3、清除cookies:Response.Cookies["user"].Expires = DateTime.Now.AddDays(-5); 只要讓數值為負即可,表示已到期幾天

 

 

WebForm 內建對象、資料增刪改、狀態保持

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.