论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > asp教程
Tag:入门,文摘,实例,技巧,iis,表单,对象,上传,数据库,记录集,session,cookies,存储过程,注入,分页,安全,优化,xmlhttp,fso,jmail,application,防盗链,stream,组件,md5,乱码,缓存,加密,验证码,算法,ubb,正则表达式,水印,,日志,压缩,url重写,控件,函数,破解,触发器,socket,ADO,初学,聊天室,留言本,视频教程

ASP.NET:处理session

文章类别:asp | 发表日期:2008-10-5 20:51:11

Shivani

Introduction

Hi When I started working with this technology I faced a problem dealing with session as in any transaction or Database oriented portal this is a must requirement to deal with.

Here is a simple example showing the way to maintain session as in the first piece of code it is taking the Author first name then opening the database i am taking authors LastName and AuthorID which is there to put in session which i can access in the next Page. It will be redirected to the next Page and in the Page_Load function only i am printing the Author full name (First and Last name) and Author ID.The only thing to be taken care is give the name which are already there in the DataBase as author first name (Example Jhonson)

Source Code PutSession.aspx, GetSession.aspx

PutSession.aspx

<%@ Page language="C#" enablesessionstate=true%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>


<script language="javascript"
src="/_aspx/1.0.2204/script/WebUIValidation.js">
</script>

<script language = "C#" runat ="server">


public String Mystr;
public String adminTypeID;
public String associationID;
public SQLDataReader myReader;


public void SubmitBtn_Click(Object sender, EventArgs e) {

if (Page.IsValid) {

SQLConnection myConnection = new SQLConnection("server=localhost;uid=sa;pwd=;database=pubs");
SQLCommand myCommand = new SQLCommand("Select au_lname,au_id from Authors where au_fname ='"+firstname.Text+"'", myConnection);

try
{
myConnection.Open();
myCommand.Execute(out myReader);
Session["aufname"] = firstname.Text;

while (myReader.Read())
{


Session["aulname"] = myReader["au_lname"];
Session["auid"] = myReader["au_id"];
Response.Redirect("GetSession.aspx");


}

}
catch(InvalidCastException exp)
{
Response.Write(exp.ToString());
}
}
}

String GetSession(String key) {
return Session[key].ToString();
}

</script>


<html>
<title>
Maintaining Session
</title>
<body bgcolor=#CCFFFF>
<form action="PutSession.aspx" method="post" runat="server">
<center>
<table width="360" border="1" cellspacing="0" cellpadding="2">
<tr bgcolor="#eeeeee">
<td>Hi U R First Name as Registered is(Only for those who r already there)</td>
<td><asp:TextBox size="25" id="firstname" value ="" runat="server"/></td>
<td> <asp:RequiredFieldValidator ControlToValidate="firstname" Display="Dynamic" errormessage="You must enter your name!" runat=server/> </td>

</tr>
<td align="right">
<asp:button type=submit text="GoGetIt" OnClick="SubmitBtn_Click" runat="server"/>
</center>
</form>
</body>
</html>

// GetSession.aspx

<%@ Page language="C#" enablesessionstate=true%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>


<script language="javascript"
src="/_aspx/1.0.2204/script/WebUIValidation.js">
</script>

<script language = "C#" runat ="server">

public string AuthorFirstName;
public string AuthorLastName;
public string AuthorID;


public void Page_Load(Object sender, EventArgs e) {

if (Page.IsValid)
{
AuthorFirstName = GetSession("aufname");
AuthorLastName = GetSession("aulname");
AuthorID = GetSession("auid");
}
}
String GetSession(String key) {
return Session[key].ToString();
}

</script>

<html>
<title>
Maintaining Session
</title>
<body bgcolor=#CCFFFF>
<center>
<p>
Hi Welcome <%=AuthorFirstName%><%=AuthorLastName%>
</p>
<p>

U r Author ID is <%=AuthorID%>

</p>
</center>
</body>
</html>


视频教程列表
文章教程搜索
 
Asp推荐教程
Asp热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058