游客:
注册
|
登录
|
帮助
网友俱乐部
»
网络编程
»
NET教程
» ASP.NET:处理session
网友生活网
|
网友学堂
如何获得金币?
诚招斑竹
IP归属地查询 PR查询 收录查询 whois查询
免费超短2级域名,your.jpy.cc
传世私服
|
传奇世界私服
注册亚洲交友中心,找个人来同居吧!
7CODE查杀恶意代码,守护您的电脑
‹‹ 上一主题
|
下一主题 ››
投票
交易
悬赏
活动
打印
|
推荐
|
订阅
|
收藏
标题: ASP.NET:处理session
问天
元帅
UID 11493
精华
186
积分 34130
帖子 33347
威望 88
金币 13137
热心 2611
阅读权限 100
注册 2006-4-7
状态 离线
#1
使用道具
发表于 2007-2-20 04:00
资料
个人空间
主页
短消息
加为好友
ASP.NET:处理session
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" 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>
网友
问天
签名 - 网友社区
请您回个帖。谢谢
PR查询
免费域名
免费空间
[广告]
免费域名(Free Subdomain)
免费空间(Free hosting)
PR查询(Google Pagerank)
投票
交易
悬赏
活动
控制面板首页
编辑个人资料
积分交易
公众用户组
好友列表
个人空间管理
基本概况
流量统计
客户软件
发帖量记录
论坛排行
主题排行
发帖排行
积分排行
在线时间
管理团队
管理统计
当前时区 GMT+8, 现在时间是 2008-9-6 02:51
信产部ICP备案:
京ICP备05066424号
北京市公安局网监备案:1101050648号
Powered by
Discuz!
5.5.0
TOP
清除 Cookies
-
联系我们
-
网友俱乐部
-
Archiver
-
WAP