• WebForm 内置对象、数据增删改、状态保持


    一、内置对象

    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); 只要让数值为负即可,表示已过期几天

     

  • 相关阅读:
    Tomcat工作原理
    刚刚大学毕业,自己搭网站遇到的问题 一:tomcat中同时部署两个项目的问题
    maven常用命令
    修改redis端口号
    assert的基本用法
    Linux下mysql新建账号及权限设置
    tomcat设置http自动跳转为https访问
    spring mvc 实现文件上传下载
    svn: E155004 is already locked 解决方案
    数据库连接池druid
  • 原文地址:https://www.cnblogs.com/ShenG1/p/5900690.html
Copyright © 2020-2023  润新知