zoukankan      html  css  js  c++  java
  • Asp.net.WebForm页面跳转的传值的几种方式

    最近在做asp.net.WebForm页面的项目,记录下webForm页面跳转到其他页面的方式(带参数)

    其中Sesion传值出现获取不到(未解决),Cookie传值出现中文乱码(已解决)

    1、不带参数的跳转

            /// <summary>
            /// 登录事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void LoginClick(object sender, EventArgs e)
            {
                   //跳转到指定页面
                   Response.Redirect("../home/index.aspx");
             }

    2、带参数的跳转

    2.1、使用queryString传值,跳转到指定页面

    2.1.1)a.aspx的C#代码(存放)

             /// <summary>
            /// 登录事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void LoginClick(object sender, EventArgs e)
            {
                //获取页面的用户、密码
                string userCode = this.txtUser.Text.Trim();
                string userPwd = this.txtPwd.Text.Trim();
               
                //方式一:使用queryString传值,跳转到指定页面
                Response.Redirect("../home/index.aspx"+"?userCode="+ userCode+"&&userPwd="+ userPwd;
            }

    2.1.2)b.aspx的C#代码(读取)

            protected void Page_Load(object sender, EventArgs e)
            {
                //通过QueryString ,获取登录名
                this.loginUserName.Text = Request.QueryString["userCode"];
            }

    2.2、使用Application传值,跳转到指定页面

    2.2.1)a.aspx的C#代码(存放)

            /// <summary>
            /// 登录事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void LoginClick(object sender, EventArgs e)
            {
                //获取页面的用户、密码
                string userCode = this.txtUser.Text.Trim();
                string userPwd = this.txtPwd.Text.Trim();
        
                //方式二:使用Application传值,跳转到指定页面
                Application["userCode"] = userCode;
                Application["userPwd"] = userPwd;
                Response.Redirect("../home/index.aspx");
            }

    2.2.2)b.aspx的C#代码(读取)

            protected void Page_Load(object sender, EventArgs e)
            {
                //通过 Application ,获取登录名
                this.loginUserName.Text = Application["userCode"].ToString();
            }

    2.3、使用Seesion传值,跳转到指定页面(报错,跳转到下一页面,查找不到Session的值)

    2.3.1)a.aspx的C#代码(存放)

            /// <summary>
            /// 登录事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void LoginClick(object sender, EventArgs e)
            {
                //获取页面的用户、密码
                string userCode = this.txtUser.Text.Trim();
                string userPwd = this.txtPwd.Text.Trim();
        
                 //方式三:使用Seesion传值,跳转到指定页面(报错,跳转到下一页面,查找不到Session的值)
                 Session["userCode"] = userCode;
                 Session["userPwd"] = userPwd;
                 Response.Redirect("../home/index.aspx");
            }

    2.3.2)b.aspx的C#代码(读取)

            protected void Page_Load(object sender, EventArgs e)
            {
                //通过 Session ,获取登录名
                this.loginUserName.Text = Session["userCode"].ToString();
            }

    2.4、使用cookie传值,跳转到指定页面(cookie存放中文会乱码,存放cookie时,需编码)  

    2.4.1)a.aspx的C#代码(存放)

          /// <summary>
            /// 登录事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void LoginClick(object sender, EventArgs e)
            {
                //获取页面的用户、密码
                string userCode = this.txtUser.Text.Trim();
                string userPwd = this.txtPwd.Text.Trim();
        
                //方式四:使用cookie传值,跳转到指定页面(cookie存放中文会乱码,存放cookie时,需编码)            
                 HttpCookie cookie1 = new HttpCookie("userCode", HttpUtility.UrlEncode(userCode));
                 HttpCookie cookie2 = new HttpCookie("userPwd",HttpUtility.UrlEncode(userPwd));
                 Response.Cookies.Add(cookie1);
                 Response.Cookies.Add(cookie2);
                 Response.Redirect("../home/index.aspx");
            }

    2.4.2)b.aspx的C#代码(读取)

            protected void Page_Load(object sender, EventArgs e)
            {
                 //通过cookie,获取登录名(cookie存放中文会乱码,读取cookie时,需解码)
                this.loginUserName.Text = HttpUtility.UrlDecode(Request.Cookies["userCode"].Value);
            }

    参考网址:

    https://blog.csdn.net/zzzzzzzert/article/details/8486143

    https://www.cnblogs.com/qq450867541/p/7600345.html

    平时多记记,到用时才能看看,记录你的进步,分享你的成果
  • 相关阅读:
    缓存架构设计细节二三事
    啥,又要为表增加一列属性?
    SpringMvc4.x---快捷的ViewController
    SpringMvc4.x--@ControllerAdvice注解
    SpringMvc4.x--Spring MVC的常用注解
    解决svn--Unable to connect to a repository at URL ‘https://xxxxxx’ 问题
    或许你不知道的10条SQL技巧
    Java 基础-运算符
    Java 运算符 % 和 /
    Java基础-注释
  • 原文地址:https://www.cnblogs.com/xielong/p/15215129.html
Copyright © 2011-2022 走看看