ASP.NET:处理session
2001-05-21 10:18:51 来源:WEB开发网核心提示: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 thi
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>
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.net页面弄成伪静态页面
- ››Asp.net 中将汉字转换成拼音的方法
- ››ASP.NET及JS中的cookie基本用法
- ››ASP.NET获取MS SQL Server安装实例
- ››asp.net实现调用百度pai 在线翻译英文转中文
- ››ASP.NET页面选项进行提示判断
- ››Asp.net定时执行程序
- ››ASP.NET中利用DataList实现图片无缝滚动
- ››ASP.NET验证控件RequiredFieldValidator
- ››ASP.NET中使用System.Net.Mail发邮件
- ››ASP.NET中获取用户控件中控件的ID
- ››ASP.NET中FileBytes写成文件并存档
更多精彩
赞助商链接