首先声明这只是我的学习笔记,很普通没技术含量,希望大家别笑话
APPlication AutoEventWireup Cookie PageLifeCycle (页面生命周期) RegisterClientScript (向客户端注册脚本) Request_Form(表单集合) Request.QueryString(学习Request.QueryString) Response Session 等相关知识的介绍
APPlication:lablInfo.text="访问次数"+Application["VisitToal"].Tostring();
AutoEventWireup:this.label.text=DateTime.Now.ToLongDateString()+""DateTime.Now.ToShortTimeString();
Cookie://创建名称为:LastTime的Cookie对象
HttpCookie cookie=new HttpCookie("LastTime");
//设置过期时间为30天
cookie.Expires=DateTime.Now.AddDays(30);
//设置Cookie中存放的直
cookie.Value=DateTime.Now.ToLongTimeString();
//保存Cookie对象
Response.Cookies.Add(cookie);
//加载是调用
if(!IsPostBack)
{
if(Request.Cookies["LastTime==null"])
{
this.lblInfo.Text="欢迎来到";
}else
{
lblInfo.Text=string.Format("您上次访问的时间是:{0}",Request.Cookies["LastTime"].Value);
}
}
VisitCount:
if(!IsPostBack)
{
int count=0;
if (Request.Cookies["VisitCount"]==null)
{
this.Lael1.Text="您是第一次访问该网站";
}else
{
count= int.Parse(Request.Cookies[VisiCount].Value);
this.Label.Text=string.Format("您是第{0}次访问该网站",count);
}
HttpCookie cookie=new HttpCookie("VisitCount");
cookie.Value=(count+1).Tostring();
Response.Cookies.Add(cookie);
}
PageLifeCycle (页面生命周期)
//预初始化
protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("Page_PreInit<br/>");
}
//初始化
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Page_Init<br/>");
}
//初始化完成
protected void Page_InitComplete(object sender, EventArgs e)
{
Response.Write("Page_InitComplete<br/>");
}
//预加载
protected void Page_PreLoad(object sender, EventArgs e)
{
Response.Write("Page_PreLoad<br/>");
}
//加载
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load<br/>");
}
//加载完成
protected void Page_LoadComplete(object sender, EventArgs e)
{
Response.Write("Page_LoadComplete<br/>");
}
//预呈现
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Page_PreRender<br/>");
}
protected void Page_Unload(object sender, EventArgs e)
{
//Response.Write("Page_Unload");//这行代码能取消注释吗?为什么呢?
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "控件事件";
Response.Write("<span style='color:red'>Button事件触发!</span><br/>");
}
RegisterClientScript (向客户端注册脚本)
if(!IsPostBack)
{
//理解每个参数的含义
Page.ClientScript.RegisterStartupScript(this.GetType(),"test","<script>var btn=document.getElementById('Button1');alert(btn.value);</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "showmessage",
"alert('我是由RegisterStartupScript方法注册的!')", true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "showmessage3",
"alert('我是由RegisterClientScriptBlock方法注册的!')", true);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script> alert('提示框');</script>");
}
Request_Form(表单集合)
if(!IsPostback)
{
this.lblMsg.Text=Request.Form["txtUserName"]//从后置代码中根据表单中的文本框ID 获得输入的值
}
Request.QueryString(学习Request.QueryString)
原: <asp:HyperLink ID="HyperLink1" Font-Size="20pt" runat="server" NavigateUrl="~/Request_QueryString/Target.aspx?username=eagle">
我是超链接</asp:HyperLink>
目标页:两种方式
if(!IsPostBack)
{
(1):Response.Write("QueryString:");
Response.Write(Request.QueryString["username"]);
(2):Response.Write("Params:");
Response.Write(Request.Params[username]);
}
Response:
原页面:加载是调用
{
Response.Write("大家下午好");
}
protected void Button1_Click(object sender, EventArgs e)//点击按钮是跳转到另一个页面
{
Response.Redirect("Default2.aspx");
}
Session
protected void btnLogin_Click(object sender, EventArgs e)
{
string userName = this.txtUserName.Text.Trim();
string password = this.txtPassword.Text.Trim();
if (userName.Equals("accp") && password.Equals("accp"))
{
Session["UserName"] = userName;//登录成功后,把用户名保存至Session
//Session.Timeout = 1;// 如果这行不注释掉,那么Session在1分钟后就过期了。
Response.Redirect("Default.aspx");
}
else
{
lblMsg.Text = "请重新输入!";
}
}
目标页面:调用
if (Session["UserName"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
string userName = Session["UserName"].ToString();
lblInfo.Text = "欢迎您!" + userName;
Session.Clear();//清空所有Session
Session["UserName"] = null;
}
坚持我的目标 熟悉所学知识,这就是胜利