IE8 does not keep Session Variables - iis-7

If I host an ASP.NET page with:
<%# Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void btn_Click(object sender, EventArgs e)
{
lbl.Text = HttpContext.Current.Session["a"] == null ?
"null" :
HttpContext.Current.Session["a"].ToString();
}
protected void btn_Click2(object sender, EventArgs e)
{
lbl.Text = HttpContext.Current.Cache["a"] == null ?
"null" :
HttpContext.Current.Cache["a"].ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HttpContext.Current.Session["a"] = "CBA";
lbl.Text = "assigned Session Variable";
HttpContext.Current.Cache.Add(
"a", "ABC", null,
DateTime.Now.AddHours(2), TimeSpan.Zero,
CacheItemPriority.NotRemovable, null);
}
}
</script>
<html>
<head>
<title>Testing Session</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn" runat="server" Text="read Session" OnClick="btn_Click" />
<asp:Button ID="btn2" runat="server" Text="read Cache" OnClick="btn_Click2" />
<hr />
<asp:Label ID="lbl" runat="server" />
</div>
</form>
</body>
</html>
on the first run I do get the assigned Session Variable text, but upon click the Session object is always null
Id there an option I need to turn on/off to use the normal Session Variables ?
works fine on IIS 6.0 and Cassini (under VS 2008 and 2010).
I'm starting to be without ideas on what's going on :o(
Any help is greatly appreciated!
the process of the example page above
More tests shows that this only happens in IE (ie8 in this case), Firefox, Safari, Opera, Chrome they all give the correct "answer"
check the screen cast of the situation

the problem may be the underscores in the domain. remove the _ and see if the same thing happens

Session ID's are maintained via a locally stored cookie by the browser.
Are you running in some sort of protected mode that prevents IE8 from storing cookies? There should be a red warning icon on the status bar.. something along the "...prevented this site from storing a cookie"

Related

asp LinkButton inside html Button tag

I have some Html code that contains the button tag "<button></button>".
I have noticed that if I place a linkbutton inside this element, the postback of the linkbutton will never occur.
So this is not working :
<button><asp:LinkButton ID="lnkExample" OnClick="lnkExampleBtn_Click"runat="server">Text</asp:LinkButton></button>
The server method "lnkExampleBtn_Click" will never be launched.
How come the postback doesn't work ?
Is there a way to have a linkbutton work inside the "button" tag ?
I also tried putting an href element with javascript _doPostback specific method, but that is not working either.
I've tried to reproduce your case.
Here's code behind:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
lblTest.Text = "Test";
}
protected void btnTest2_Click(object sender, EventArgs e)
{
lblTest.Text = "Test2";
}
}
And here's aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication1.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="btnTest" runat="server" OnClick="btnTest_Click">Test</asp:LinkButton>
<button><asp:LinkButton ID="btnTest2" runat="server" OnClick="btnTest2_Click">Test2</asp:LinkButton></button>
<button>test</button>
<asp:Label ID="lblTest" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
What I found is - it depends on browser. In Chrome it's possible to place LinkButton inside tags. In Firefox and IE 10 it's not.
The reason why it's not working is simple - it's not allowed by the HTML 5 standard. You just can't embed a link inside a button.
http://www.w3.org/TR/2011/WD-html5-20110525/the-button-element.html#the-button-element
Link inside a button not working in Firefox

ASP.NET webforms postback weirdness with Safari 5.1.7 on Windows [duplicate]

This question already has an answer here:
Postback is not working in Safari in Windows 7
(1 answer)
Closed 3 years ago.
I have a simple ASP.NET webforms page with a button and a label. When using Safari 5.1.7 on Windows, clicking the button causes a postback, but does NOT actually cause the button click event to fire for some bizarre reason.
There are no javascript errors. There are no UpdatePanels or AJAX of any kind. This is literally the page I'm testing with:
<!doctype html>
<html lang="en">
<head runat="server">
<meta charset="utf-8">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
And the code behind is simply:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "test";
}
When the button is clicked, the Page_Load event does fire one time, but Page.IsPostBack returns FALSE instead of true, and the Label1.Text is never set.
Note this problem ONLY happens in Safari. Everything works fine in IE8+, FF15+, Chrome 21+, and Opera 12+.
I've read numerous posts about Safari weirdness regarding hidden field size limitations (and therefore viewstate) - but this page should barely even be using viewstate (if at all). There are also many posts about javascript errors relating to UpdatePanels and/or AJAX calls, but as you can see there are none of those here.
I believe this question is also related, but I wanted to start a new one with example html/cs so you could reproduce the problem.
Am I missing something or is Safari on Windows really this weird?
I cannot duplicate this in Safari 5.1.7. Can you provide more information on how you are binding your events? Your manual assignment of onclick="Button1_Click" implies you are doing manual event wire up. Does your page declaration match this?
Are you sure your Page_Load event is firing; your example has it empty.
Maybe a more verbose event binding is in order, the below worked for me:
ASPX test page:
<%# Page Language="C#" AutoEventWireup="false"
CodeFile="default.aspx.cs" Inherits="PgSafari" %>
<!doctype html>
<html lang="en">
<head runat="server">
<meta charset="utf-8">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div><asp:Literal runat="server" ID="litTimeStamp" EnableViewState="false" /></div>
<div><asp:Button ID="Button1" runat="server" Text="Button" /></div>
<div><asp:Label ID="Label1" runat="server" Text="Default Label Text" /></div>
<div>
<h6>Log:</h6>
<asp:PlaceHolder runat="server" ID="plcOut" />
</div>
</form>
</body>
</html>
Code behind:
using System;
using System.Web.UI;
public partial class PgSafari : System.Web.UI.Page
{
public PgSafari()
{
//Bind Page Events
this.Init += new EventHandler(PgSafari_Init);
this.Load += new EventHandler(PgSafari_Load);
}
protected void PgSafari_Init(object sender, EventArgs e)
{
Log("_Init()");
//Bind Page Control Events
Button1.Click += new EventHandler(Button1_Click);
}
protected void PgSafari_Load(object sender, EventArgs e)
{
Log("_Load()");
Log("Page.IsPostBack = " + (Page.IsPostBack));
litTimeStamp.Text = "TimeStamp: " + DateTime.Now.ToString("mm:ss.fff");
}
protected void Button1_Click(object sender, EventArgs e)
{
Log("Button1_Click()");
Label1.Text = "test";
}
//===============\\
private void Log(String msg)
{
plcOut.Controls.Add(new LiteralControl("<div>" + msg + "</div>"));
}
}
The initial page hit in Safari looked like it should:
TimeStamp: 58:44.452
<button>
Default Label Text
Log:
_Init()
_Load()
Page.IsPostBack = False
Clicking the it looked like it should also:
TimeStamp: 59:48.869
<button>
TEST!
Log:
_Init()
_Load()
Page.IsPostBack = True
Button1_Click()

How to change pictures in asp.net to Combo box Selected Index Change

I just want to change my panel background image according to the selected index in ComboBox in asp.net web site. But the images not loading. This is my code in .aspx page
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
<div id="divx" style="height: 250px">
<asp:Panel ID="Panel1" runat="server" Height="242px">
</asp:Panel>
</div>
And this is code behind..
protected void Page_Load(object sender, EventArgs e)
{ }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 1)
{
Panel1.BackImageUrl="C:\\Users\\Laksh\\Documents\\Visual Studio 2010\\WebSites\\WebSite2\\Pic\\Capture.JPG";
}
else if (DropDownList1.SelectedIndex == 2)
{
Panel1.BackImageUrl="C:\\Users\\Laksh\\Documents\\Visual Studio 2010\\WebSites\\WebSite2\\Pic\\erroe.JPG";
}
}
It is not possible to set an aboslute path, referring to a location on your local disk! this results in a code like ...
<!-- This imageURL won't work!! -->
<div id="Panel1" style="height:250px;background-image:url(c:%09emp%0demo.jpg);">
</div>
Use a relative path instead!
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Panel1.BackImageUrl = "~/pic/error.jpg";
}
If you are not sure what to do, you can always assign a backgroundImage to a panel using the design View of your aspx page. Just select your panel, go to Properties, Click property BackImageUrl and select an image within your project. VS will than add a perfectly working path to your image!
I agree with #Pilgerstorfer Franz, use a relative path i.e. ~/xyz... and do a server.mappath. Usually the web server doesn't have rights to the local machine outside of the server directory heirarchy so mapping directly isn't a great idea.
You might also try using jQuery to do the change, you might try something like the below. You'll need to have it pull your values from your ddl, but you can pass that into your function, or have it reference the ddl's value field maybe...
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js" ></script>
<script>
function doIt()
{
$('#divTryMe').css('background-image','url("test.jpg")');
}
</script>
</head>
<body>
<div id="divTryMe" onclick="doIt()">stuff</div>
</body>
</html>

Can I put a Timer in Ajax updatePannel in a masterpage?

I made a very simple pannel updating wich will query a database and show a message on masterpage through a label.
Thus, I've put an updatePannel with a label and a timer within (obviously with a scriptManager) in my site's masterpage.
However, when I try to interact with the timer1 object, I receive an error message: "object not set to an instance of an object". I not receive this message when placing the schema in a blank page (without masterpage).
I must to run the query in masterpage because the users need to receive information whatever they are in my site.
How can I correctly place the components to do this work? what I'm doing wrong?
Thanks!
I just tried it within a master page without a problem. Here is the relevant code:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
<asp:UpdatePanel ID="updMessage" runat="server">
<ContentTemplate>
<asp:Label ID="lblMessage" runat="server" />
<asp:Timer ID="tmrMessage" Interval="5000" ontick="tmrMessage_Tick" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
From master page code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["Message"] = 1;
}
}
protected void tmrMessage_Tick(object sender, EventArgs e)
{
int message = (int)Session["Message"];
lblMessage.Text = message.ToString();
Session["Message"] = ++message;
}
The problem was where I was putting the panel, label and timer. I put in contentPlaceHolder, where the problem occours. Now I put the control in the form tag, then the problem is solved.

Can I migrate session data from one browser to another? - Web Development

I need to transfer a user to a page which requires session data which is stored within a different session.
Is it possible in some cases to migrate the session data by setting the session cookie on the user's browser?
Currently I have no workaround, and using session data seems like the only option I have at the moment.
Is it possible in some cases to
migrate the session data by setting
the session cookie on the user's
browser?
Sure you can. Simply set a new cookie and redirect. You will have to do this server-side. Setting a session cookie client-side will be problematic.
const string sessionStateCookieName = "ASP.NET_SessionId";
string sessionId = "some session id";
HttpCookie cookie = new HttpCookie(sessionStateCookieName, sessionId) { Path = "/", HttpOnly = true };
Response.Cookies.Remove(sessionStateCookieName);
Response.Cookies.Add(cookie);
// force a redirect to complete session switch
Response.Redirect(Request.Path);
You can expand on this. Here is a working example .aspx page. To switch sessions, simply add a url param 'sessionId'.
<%# Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
// this is the method that does the magic
protected override void OnPreInit(EventArgs e)
{
var sessionId = Request["sessionId"];
if (!string.IsNullOrEmpty(sessionId))
{
// this could be found via reflection if different sessionstate cookie name is used in config
const string sessionStateCookieName = "ASP.NET_SessionId";
HttpCookie cookie = new HttpCookie(sessionStateCookieName, sessionId) { Path = "/", HttpOnly = true };
Response.Cookies.Remove(sessionStateCookieName);
Response.Cookies.Add(cookie);
// force a redirect to complete session switch
Response.Redirect(Request.Path);
}
}
// the rest of this script is just demo code
protected void Page_Load(object sender, EventArgs e)
{
CurrentSessionIdLabel.Text = Session.SessionID;
}
protected void SwitchSessionButton_Click(object sender, EventArgs e)
{
// this is the session we wish to switch to
string switchToSessionId = SessionIdTextBox.Text;
Response.Redirect(Request.Path + "?sessionId=" + Server.UrlEncode(switchToSessionId));
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<div>
Current SessionId<br />
<asp:Label ID="CurrentSessionIdLabel" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Desired SessionId"></asp:Label>
<br />
<asp:TextBox ID="SessionIdTextBox" runat="server"></asp:TextBox>
<br />
<asp:Button ID="SwitchSessionButton" runat="server" Text="Switch Session" OnClick="SwitchSessionButton_Click" />
</div>
</form>
</body>
</html>
No, browsers don't share cookies. Although if you use cookieless sessions it wiil work.
If you have a login mechanism, you can just associate the data with the logged-in user (in a database?) instead of putting it in the session.

Resources