I am trying to pass four items using session state as follows:
protected void createFirstNameSessionVariable(object sender, EventArgs e)
{
Session["FirstName"] = firstName.Value;
Session.Timeout = 60;
TextBox1.Text = Session["FirstName"].ToString();
}
protected void createLastNameSessionVariable(object sender, EventArgs e)
{
Session["LastName"] = lastName.Value;
Session.Timeout = 60;
TextBox2.Text += Session["LastName"].ToString();
}
protected void createIdSessionVariable(object sender, EventArgs e)
{
Session["FacebookId"] = facebookId.Value;
Session.Timeout = 60;
TextBox3.Text += Session["FacebookId"].ToString();
}
protected void createEmailSessionVariable(object sender, EventArgs e)
{
Session["Email"] = email.Value;
Session.Timeout = 60;
TextBox4.Text += Session["Email"].ToString();
}
In Firefox and IE8, I can get them on another page using the following:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["FacebookId"] != null)
{
name = Session["FacebookId"].ToString();
studentButton.Text = name;
}
else
{
studentButton.Text = "fail";
}
}
In Chrome, however, the button label is set to fail because the session variable has a null value on the receiving end.
On IIS 7.0 Manager, the session state is currently set to "In Process"
Mode: Use Cookies
Name: ASP.NET_SessionId
Time out: 20 mins
Use hosting identity for impersonation is checked.
Thanks for your help.
Make sure that you have allowed cookies in Chrome.
Related
I was trying to perform Session State in ASP.NET, but it always start from 1 in different pages.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["count"] = 0;
}
int count = (int)Session["count"];
count++;
Label1.Text = count.ToString();
Session["count"] = count;
}
// In Global.asax
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
void Session_Start(object sender, EventArgs e)
{ // start of Session
Session["count"] = 0;
}
void Session_End(object sender, EventArgs e)
{
}
}
I expect the count continues but not starts from 1 after re-visit the webform. Thanks!
if you want to display the total active user on your site, use application object instead of session object and on session_start you increase count and on session_end, decrease count.
protected void Application_Start(object sender, EventArgs e)
{
Application["count"] = 0;
}
void Session_Start()
{ // start of Session
Application["count"] += 1;
}
void Session_End()
{
Application["count"] -= 1;
}
I have asmx web service hosted on IIS and its purpose is to authenticate logined user.
when I run my code using visual studio and debug service is successfully called and authenticate user from DB but it is unable to transfer control back to my code that has default page.
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
Response.Redirect("Default.aspx");
Response.Cache.SetNoStore();
if (!Page.IsPostBack)
{
Session["Uri"] = Request.UrlReferrer;
}
this.hdnLoginStatus.InnerHtml = "";
if (!Page.IsPostBack)
{
new DAS().AuthenticateRequest();
if (HttpContext.Current.Items["LoginStatus"] == null)
return;
var key = (AuthWS.LoginStatus)HttpContext.Current.Items["LoginStatus"];
string msg = (string)GetGlobalResourceObject("Message", key.ToString()) ?? "";
this.ShowMessage(msg, MessageType.Warning);
this.hdnLoginStatus.InnerHtml = "SignedOutForcefully";
}
}
protected void LoginUser_LoggedIn(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?key=" + (AuthWS.LoginStatus)HttpContext.Current.Items["LoginStatus"]);
}
I try to make a site with a asp.net web application, but I can't continue because, I can not set the value of TextBox to "finish" after the execution of a function file download.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = "start";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=print_ordre.pdf");
Response.TransmitFile(Server.MapPath("~/print_ordre.pdf"));
//Response.End();
TextBox1.Text = "finish";
}
I await your help ...
I am trying to send the data to serial port in ASP.net. After connecting to serial port Before postback data is being sent. But after postback i get exception while sending data.
'System.InvalidOperationException: The port is closed.'
I tried everything by connecting to port on pageload: ispostback, and disconnecting and connecting again. Still it shows same exception. Is there any way to retain the state of serial port..
here's my code. Please Help me Out...
public partial class _Default : System.Web.UI.Page
{
string indata;
public SerialPort sp = new SerialPort();
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
openPort("COM10");
disconnect();
connect();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//disconnect();
openPort("COM10");
connect();
check(TextBox1.Text); //Data Sending Successful but after postback even it doesnt work too.
}
public void connect()
{
try { sp.Open(); }
catch (Exception e1) { MessageBox.Show(e1.ToString()); }
}
public void disconnect()
{
try { sp.Close(); }
catch (Exception e1) { MessageBox.Show(e1.ToString()); }
}
public void openPort(string p)
{
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.DataBits = 8;
sp.Handshake = Handshake.None;
sp.PortName = p;
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
// sp.ReadTimeout = 200;
// sp.WriteTimeout = 200;
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
indata = sp.ReadExisting();
Debug.WriteLine(" Data Received:");
Debug.Write(" " + indata);
}
protected void Button4_Click(object sender, EventArgs e)
{
check("" + (char)26); //Exception in sending
}
protected void Button3_Click(object sender, EventArgs e)
{
check("\r\n"); //exception in sending
}
protected void Button2_Click(object sender, EventArgs e)
{
check(TextBox1.Text); // exception in sending
}
void check(string ss)
{
//sp.Dispose();
//openPort("COM10"); connect();
if (sp.IsOpen)
sp.Write(ss);
else
{
disconnect(); openPort("COM10"); connect();
sp.Write(ss);
}
}
}
I would simplify your code, so the port is configured on page load and the one handler deals with resetting your port. The disconnect, connect, I see is complicating it. Here I have given an example of using the button click event.
Please note the missing brace below.
public partial class _Default : System.Web.UI.Page
{
string indata;
public SerialPort sp = new SerialPort();
protected void Page_Load(object sender, EventArgs e)
{
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.DataBits = 8;
sp.Handshake = Handshake.None;
sp.PortName = p;
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
// sp.ReadTimeout = 200;
// sp.WriteTimeout = 200;
}
if (!Page.IsPostBack)
{
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.DataBits = 8;
sp.Handshake = Handshake.None;
sp.PortName = p;
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
// sp.ReadTimeout = 200;
// sp.WriteTimeout = 200;
}
protected void Button1_Click(object sender, EventArgs e)
if sp.IsOpen = False then
{
try { sp.Open(); }
catch (Exception e1) { MessageBox.Show(e1.ToString()); }
}
else
{
try { sp.Close(); }
catch (Exception e1) { MessageBox.Show(e1.ToString()); }
}
void check(string ss)
{
//sp.Dispose();
//openPort("COM10"); connect();
if (sp.IsOpen)
{//missing brace
sp.Write(ss);
}//missing brace
else
{
sp.Open();
sp.Write(ss);
}
}
}
Edit 2:
As I mentioned in the comments the code will only run once.
The following examples are provided from the link below.
Have you tried writing some codes under the !IsPostBack code block to
check if the codes hits there when it postbacks? try this below for
testing
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Write("First load");
}
else
{
Response.Write("Postback occurs");
}
}
OR
I will refer the code you want to run as One Time Code. For what you
are attempting to achieve, following should work. Please note that
sessions also expire. So after about 20 minutes (default value) of
inactivity, if the user comes back to the site/hits refresh, the One
Time Code will run again. If you want something more persistent than
20 minutes you can try using cookies, but if user clears their cookies
your One Time Code with run again.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["firsttimeuser"] == null)
{
//put code here for One Time Code;
Session["firsttimeuser"] = true;
}
}
Please see this link:
There is lengthy discussion about this.
http://forums.asp.net/t/1314918.aspx/1
You should be able to create a solution from this, please advise.
Edit 1
Please see MSDN for Get Port Names:
Use the GetPortNames method to query the current computer for a list
of valid serial port names. For example, you can use this method to
determine whether COM1 and COM2 are valid serial ports for the current
computer.
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames.aspx
And SerialPort.Open
_serialPort.PortName = SetPortName(_serialPort.PortName)
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.open.aspx
Edit 3
Try:
if (!IsPostBack) or
if(!Page.IsPostBack)
Please see:
Implementation of IsPostBack in page load
What is a postback?
and:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
I have 2 pages on my dummy site in asp.net, (default.aspx and default2.aspx), On default.aspx, i created session like below
protected void Page_Load(object sender, EventArgs e)
{
Session["MySession"] = "WELCOME";
Session.Timeout = 1;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("default2.aspx");
}
and on default2.aspx
if (Session["MySession"] != null)
Response.Write(Session["MySession"]);
else
Response.Write("Session Timed Out");
i was wondering that after 1 min the session will get erase, as i have timeout, but after one minute when i click on the button it redirected me to default2.aspx, and displayed a session value "WELCOME". can anyone tell me how to erase session value after particular duration
In your Default.aspx you have to check if it is not a post back otherwise the session will be initialized again for each button click
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.isPostBack())
{
Session["MySession"] = "WELCOME";
Session.Timeout = 1;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("default2.aspx");
}