网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.ASP中FSO对象对IIS WEB服务器数据.
.如何安裝 IIS 5.0 ?.
.ServerVariables 对路径的操作.
.ASP.Net中程序构架与程序代码的分.
.如何在Web页面中集成文件上传功能.
.显示左边的n个字符(自动识别汉字.
.ASP中实现的类似URLEncode的编码.
.分栏显示记录集的表格演示,并实.
.automation服务器不能创建对象.
.ASP接收Ajax 在POST方式下发送的.
.了解MSMQ,控制ASP进程 (一).
.Varchar与char的区别.
.在ASP中优化数据库处理.
.优化MICROSOFT ACCESS提高速度.
.ASP,PHP与.NET伪造HTTP-REFERER方.
.ASP中有关timeout超时的体会&nbs.
.初学ASP编程易犯的一个错误要注意.
.ASP错误的几种常规处理方式.
.提供一种“间接防止另存为”的方.
.将连接数据库的代码隐藏在DLL中.

ASP.NET:处理session

发表日期:2001-5-21


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>


上一篇:纯猝使用VBScript来实现加密 人气:10483
下一篇:输入显示框中循环出现文字 人气:12623
浏览全部的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐