Wednesday, March 28, 2012

updates, both session and deserialisation

hi,

I'm new to the forum and Asp.net ajax but have been a c# dev for a while. My question is regarding using the session from within Ajax, and also to boot reading in a file.

the following pseudocode outlines my problem, the button and label are both in update panel. which itself is working fine (as on the 2nd click the session variable is shown)

basically,

button1 click set session["test"] = hello;

in the page load event have

if(session != null) label1 = session[hello];

what gets me is why when clicking a link (still within the update panel) the session variable is shown, but not through the original request. As I thought the entire page cycle is processed at the server and only the items in the update panel (in this case update mode is always) are refreshed?

the same happens with deserialization when used from the wizard in an update panel, it loads up the previous file rather than the new one but i imagine its somthing I have done wrong...

I have a quick fix with a timer set to 1second and calling my refresh function. I've also looked at using the Ajax profile for storing data i would else have stored in the session.

I was just wondering if anybody could point out my mistake or offer any alternatives. (using the membership and roles isnt feasable on this project unfortunatly)

well cheers and im sure it must be just somthing i have missed!

initiagroup:

As I thought the entire page cycle is processed at the server and only the items in the update panel (in this case update mode is always) are refreshed?

That's correct.

Your problem sounds like it almost has to be a page life cycle issue. Can you post a little bit more code?


thanks for your quick reply. Here is an extremly simple code snippit that shows my problem... its ok i think i have worked it out. Am i right in thinking that even without the update panel that code wouldnt work?

1<%@. Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>23<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">45<html xmlns="http://www.w3.org/1999/xhtml" >6<head runat="server">7 <title>Untitled Page</title>8</head>9<body>10 <form id="form1" runat="server">11 <asp:ScriptManager ID="ScriptManager1" runat="server">12 </asp:ScriptManager>13 <div>14 <asp:UpdatePanel ID="upd1" runat="server">15 <ContentTemplate>16 <asp:label ID="lbl1" runat="server"></asp:label>17 <asp:Button ID="btn1" runat="server" Text="click" OnClick="btn1_Click" />18 </ContentTemplate>19 </asp:UpdatePanel>20 </div>21 </form>22</body>23</html>

and the code behind

1using System;2using System.Data;3using System.Configuration;4using System.Collections;5using System.Web;6using System.Web.Security;7using System.Web.UI;8using System.Web.UI.WebControls;9using System.Web.UI.WebControls.WebParts;10using System.Web.UI.HtmlControls;1112public partialclass test : System.Web.UI.Page13{14protected void Page_Load(object sender, EventArgs e)15 {16if (Session["test"] !=null)17 {18 lbl1.Text = Session["test"].ToString();19 }20 }21protected void btn1_Click(object sender, EventArgs e)22 {23 Session["test"] ="test";24 }25}26

as you see very easy just click the button and see what happens. In debug it hits the page load event after the button is pressed.

oh and its just a standard ajax template from the new website dialog box.

Cheers.

No comments:

Post a Comment