如何使用母版页中的cookie将用户名发送到chaild页面 [英] how to send username to chaild pages by using cookies in master pages

查看:116
本文介绍了如何使用母版页中的cookie将用户名发送到chaild页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过在母版页中使用cookie将用户名发送到chaild页面我的用户名是在母版页中,它应该显示在我的母版页上每个chaild页面...请帮我解决这个问题



提前感谢..

how to send username to chaild pages by using cookies in master pages my username is in master page and it should be displayed on my master page for every chaild pages...please help me how to solve this

thanks in advance..

推荐答案

假设您在母版页中有用户名值。

这是我给你的示例代码。

在主页面加载事件中编写此代码。并设置Cookie值。

Assuming that you have Username value in Master Page.
This is the sample code i give you.
Write this code in the Master Page Load Event. And set the Cookie value.
protected void Page_Load(object sender, EventArgs e)
{
    HttpCookie MyCookie = new HttpCookie("UserName");
    MyCookie.Value = lblUsername.Text; //set the user name value here.
    MyCookie.Expires = DateTime.Now.AddDays(1); //include cookie expiration time
    Response.Cookies.Add(MyCookie);
}





并从子页面中的cookie检索数据

在Page_Load事件中写入



And to retrieve data from cookie in child page
In the Page_Load event write

protected void Page_Load(object sender, EventArgs e)
{
    string userName = Request.Cookies["UserName"].Value;
}


这篇关于如何使用母版页中的cookie将用户名发送到chaild页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆