关于 "UserControl" 和 "Session" 的问题,应该很简单,但我不会,高分!!!!50分奉送~
一.我的自定义用户登陆控件user.ascx:
<%@ Control Language= "C# " AutoEventWireup= "true " Inherits= "come " CodeFile= "bin/public.cs "%>
....... <% Response.Write(Session[ "username "]); %> .....
二.我的public.cs:
.......
public partial class come : System.Web.UI.UserControl
......
Session[ "username "] = dr[ "name "].ToString();
三.我的index.aspx:
<form id= "form1 " runat= "server "> <mycontrol:come runat= "server " /> </form>
现在关键问题是:Session[ "username "]并没有被生成,我Response.Write(Session[ "username "])也为空,请问是什么原因?我该如何做?谢谢,50分表心意~
------解决方案--------------------这样看看
if (!mydr.HasRows)
{
mydr.Close();
conn.Close();
Response.Redirect( "111.aspx ");
}
else
{
if(mydr.Read())
{
Session[ "username "] = mydr[ "identity "].ToString();
Session[ "password "] = mydr[ "thekey "].ToString();
Session[ "userclass "] = mydr[ "class "].ToString();
string sqlip;
sqlip = "insert into ipaddress ([identity],[ipaddress],[logintime]) values ( ' " + useroftextbox + " ', ' " + Request.ServerVariables[ "REMOTE_ADDR "] + " ', ' " + DateTime.Now + " ') ";
OleDbCommand commip = new OleDbCommand(sqlip,conn);
commip.ExecuteNonQuery();
}
mydr.Close();
conn.Close();
}
不过DataReader是独占连接的,你在做查询的时候未必能插入记录成功
------解决方案--------------------Response.Write(Session[ "username "])
session取出的是object,Response.Write传入的应该是string