Problems with Session losing its data in .Net - c#

I have allready asked this question before, but now I have found more details and some more weird stuf.
I have a web project that has 2 .aspx files that connect data with sessions. Without any extra code this works, when i add my extra code the session no longer works. Does anybody have an idea why?
Code where sessions work:
Form1:
protected void Page_Load(object sender, EventArgs e)
{
Session["data"] = "5";
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // 5 when i look while debugging
Response.Redirect("~/Form2.aspx", false);
}
Form 2:
protected void Page_Load(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = 5 when i look while debugging
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = 5 when i look while debugging
}
Code where sessions don't work: (i use 2 classlibraries (logic and dataAccess where I get data from json webservice and parse it to my forms).
Form1:
protected void Page_Load(object sender, EventArgs e)
{
Logic logic = new Logic();
logic.login(credentials);
List<AppointmentExtParticipant> opleidingVolgers = logic.getOpleidingVolgers();
foreach (AppointmentExtParticipant app in opleidingVolgers)
{
if (app.contact != null)
{
Relation rel = logic.getRelationData(app.contact.FK_RELATION);
DropDownListUsers.Items.Add(app.ToString() + " " + rel.ToString());
}
}
Session["data"] = "5";
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["opleidingvolger"]); // s = 5 when i look while debugging
Response.Redirect("~/Form2.aspx", false);
}
Form 2:
protected void Page_Load(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = null when i look while debugging
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = null when i look while debugging
}
Ofcourse I simplified the names here a bit so people could understand, thnx!
edit:
logic login:
dataaccess login:
Here i get data from my webservice hosted on another url.

Sessions generally work by using cookies.You are using HttpWebRequests which creates new sessionSo you need to preserve session cookie between requests. Thats why we use CookieContainer so add
CookieContainer container = new CookieContainer();
HttpWebRequest req = WebRequest.Create(
"") as HttpWebRequest;
req.CookieContainer = container;

Related

Asp.net c# button clicked event does not add new string to arraylist

I am creating shopping cart and I am having problems to add new string to arraylist. It only adds once. I have tried declare arraylist in load page method and also tried to assign to session variable, as in code bellow.
Result: I get only one string in arraylist even button been pressed manytimes.
ArrayList test;
protected void Page_Load(object sender, EventArgs e)
{
test = new ArrayList();
Session["array"] = test;
}
protected void Button1_Click(object sender, EventArgs e)
{
ArrayList test = (ArrayList)Session["array"];
test.Add("a");
label1.Text = test.Count +"";
Session["array"] = test;
}
Is because in every postback your Page_Load method is called, so test is always set as new ArrayList();
try this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
test = new ArrayList();
Session["array"] = test;
}
}

A little help in asp.net C# jumping from method to method

My problem is that I can get from page_load to show_books function
but I cant get from show_books to book_detail.
Every time I try to fire book_details it gets me to page_load.
my code is this:
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(book_show);
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(book_details);
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
I'm sorry if my question is annoyingly newbie
but I would like some help .
I just started to learn asp.net
well i don't understand why you need to do this,but code below works,hope it helps
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(this.book_show);
lnk_button.CommandName = "testClick";
lnk_button.CommandArgument = "1";
lnk_button.ID = "11";
lnk_button.Text = "blah";
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
public void book_show()
{
book_details() ;
}
public void book_details()
{
}

How to set website theme to all webpages using a drop down list?

I am trying to use a drop down list on my homepage to select and set the theme for all webpages. It sets it for the homepage but when I go to any other page it has no theme. This is my code for my homepage:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string selectedTheme = Page.Theme;
HttpCookie userSelectedTheme =
Request.Cookies.Get("UserSelectedTheme");
if (userSelectedTheme != null)
{
selectedTheme = userSelectedTheme.Value;
}
if (!string.IsNullOrEmpty(selectedTheme) &&
ddlSetTheme.Items.FindByValue(selectedTheme) != null)
{
ddlSetTheme.Items.FindByValue(selectedTheme).Selected =
true;
}
}
}
protected void ddlSetTheme_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie userSelectedTheme = new
HttpCookie("UserSelectedTheme");
userSelectedTheme.Expires = DateTime.Now.AddMonths(6);
userSelectedTheme.Value = ddlSetTheme.SelectedValue;
Response.Cookies.Add(userSelectedTheme);
Response.Redirect(Request.Url.ToString());
}
private void Page_PreInit(object sender, EventArgs e)
{
HttpCookie setTheme = Request.Cookies.Get("UserSelectedTheme");
if (setTheme != null)
{
Page.Theme = setTheme.Value;
}
}
I am thinking that the code I have is only sufficient to apply the theme to one page, so how do I apply it to all pages?
By default, your HttpCookie's scope is the page you're on.
If you want it to be the entire domain, you need to set the Path to be the entire site, probably like this:
userSelectedTheme.Path = "/";
More info:
http://msdn.microsoft.com/en-us/library/system.web.httpcookie.path(v=vs.110).aspx
All I had to do was put the preinit code in the code files of the other pages.

Why # not work in query string?

In cs.aspx page i have a button with following code:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/cs.aspx?p=ali#25");
}
In page_load i get query string and display it:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["p"] != null)
{
string p = Request.QueryString["p"];
Response.Write("p= "+p);
}
}
in query string:
p = ali#25
but in run time display
p = ali
why string after # not shown.
found a solution. use Server.UrlEncode:
Response.Redirect("~/cs.aspx?pass="+Server.UrlEncode("a#25"));

Web browser control not working to get text

I'm trying to get source code of a webpage and save it to richtextbox and that web browser control navigates to new URL. But I'm getting a blank rich text box. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
webBrowser2.Navigate("http://www.gmail.com");
}
private void timer1_Tick(object sender, EventArgs e)
{
if(webBrowser2.ReadyState == WebBrowserReadyState.Complete)
{
timer1.Enabled = false;
richTextBox1.Text = webBrowser2.DocumentText;
webBrowser2.Navigate("new URL");
}
}
I see two alternatives to what you're doing.
1) Using DocumentCompleted:
private void Form_Load(object sender, EventArgs e) {
webBrowser.Navigate("www.google.com");
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
richTextBox.Text = webBrowser.DocumentText;
}
2) Using the WebClient:
private void Form_Load(object sender, EventArgs e) {
using (System.Net.WebClient wc = new System.Net.WebClient()) {
richTextBox.Text = wc.DownloadString("http://www.google.com");
}
}

Categories

Resources