国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET:處理session

2024-07-10 13:07:47
字體:
來源:轉載
供稿:網友
國內最大的酷站演示中心!
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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 保德县| 凉城县| 墨竹工卡县| 韶山市| 汝南县| 南漳县| 弋阳县| 栾城县| 巴林左旗| 镶黄旗| 东乡族自治县| 南雄市| 綦江县| 武城县| 荣成市| 高淳县| 林口县| 富川| 资阳市| 曲阜市| 龙口市| 四子王旗| 乌拉特后旗| 随州市| 菏泽市| 定南县| 紫金县| 扎囊县| 城步| 富宁县| 元阳县| 新津县| 丹江口市| 东台市| 福海县| 大田县| 金沙县| 黎城县| 顺昌县| 长岛县| 武安市|