国产探花免费观看_亚洲丰满少妇自慰呻吟_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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沁阳市| 尼木县| 中西区| 衡东县| 基隆市| 临洮县| 民权县| 定襄县| 乌兰浩特市| 武山县| 南靖县| 论坛| 专栏| 平果县| 五指山市| 济源市| 肥城市| 礼泉县| 嘉禾县| 辽阳市| 城口县| 灵宝市| 收藏| 鸡泽县| 桂东县| 鹤山市| 东乌珠穆沁旗| 台东县| 清水县| 海口市| 神农架林区| 清水河县| 连云港市| 宝兴县| 荆州市| 彭水| 齐河县| 留坝县| 毕节市| 邓州市| 瑞丽市|