How to clear a specific session when user left the page - asp.net

I have a page. There are some session variables those are used in a specified page only. How to clear those session variables when user left the page.
When user left the page using the menu/URL.
I have tried with
protected void Page_Unload(object sender, EventArgs e)
{
Session["Rules"] = null;
Session.Remove("Rules");
}
But it is now working :(
Please suggest.

Related

Browser back button lost session on asp.net

I'm clicking browser back button and then I try to go to any aspx page, I'm losting session on my Asp.net web project.
How can I solve this?
My master page load code like that;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["admin_id"] != null)
{
AdminName.Text = Session["admin_name"].ToString();
mybadge.Text = Session["my_badge"].ToString();
}
else
{
Response.Redirect("admin.aspx");
}
}
}
I think you are not touching/saving any data before navigating to another page. So that's why session gets blank when you press on back button.

Asp.net combobox - remember indexes or values in sessions

As I have problems loosing indexes after postbacks, is it beter to remember indexes or values in sessions?
Thanks.
I think if you simply make sure EnableViewState is not set to false for that specific control or on page level, then you will be able retain the indexes for selection done by user
I'd say store them in sessions, HOWEVER, do you make any selection stuff in the page load event, if so, have you checked so that it won't redo that population after a postback??
This could mess things up, this would set the time on every postback:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
This would however have your stuff remain intact in the viewstate and the time would only update if the page is loaded without a postback:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack != true)
{
Label1.Text = DateTime.Now.ToString();
}
}

ASP.net passing data between pages

I have a .aspx web page, with a html form within it, this also has two input boxes.
Whats the best way to take the input box data and pass it to a new .aspx page where it is dealt with by the request method.
Assuming that the data is not sensitive then the best method to pass it to your new page using Response.Redirect and the querystring using:
protected void MyFormSubmitButton_Click(Object sender, EventArgs e)
{
string value1 = txtValue1.Text;
string value2 = txtValue2.Text;
// create a querystring
string queryString = "x=" + value1 + "&y=" + value2;
// redirect to the encoded querystring
Response.Redirect("NewPage.aspx?" + Server.URLEncode(queryString));
}
This web page has a lot of information which you can use for passing the values from page to page.
http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx#Y1100
Try Server.Transfer:
Terminates execution of the current
page and starts execution of a new
page by using the specified URL path
of the page. Specifies whether to
clear the QueryString and Form
collections.
If you set the preserveForm parameter
to true, the target page will be able
to access the view state of the
previous page by using the
PreviousPage property.
Your main page:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
// ThreadAbortException occurs here.
// See http://support.microsoft.com/kb/312629 for more details.
Server.Transfer("AnotherPage.aspx", true);
}
}
"AnotherPage.aspx":
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
// Accessing previous page's controls
}
}

Link is not working as expectation

I have a user control of menu which have links on which user is redirected to the desired page. When I am using that user control in my aspx page, on first time when user click to the link he is redirected to the destination, but when user again click that link it is showing the page stating that "cannot find server", the same thing is happened to the other links also.
In my user control I am using:
Private void Link1_Click(Object sender, eventargs e)
{
Response.Redirect("Secondpage.aspx");
}
Private void Link2_Click(Object sender, eventargs e)
{
Response.Redirect("Thirdpage.aspx");
}
Unexpectdly, the same code is working fine on the production server but throwing issue in the developmwnt server.
I am not sure the cause of the error.. Thanks in advance.
your event is not found on the server as it is defined as Private
I think Private should be Protected
Protected void Link2_Click(Object sender, eventargs e)
{
Response.Redirect("Thirdpage.aspx");
}

Passing Value Between Web User Controls - DifferentQuestion

I want pass values between web user controls without writing any code to the main page which user controls are put on. I do something like that but after doing that I need to click double to pass the value.
The example of what I've done :
Department User Control (Code-Behind)
protected void Page_Load(object sender, EventArgs e)
{
int productId = ProductUserControl.selectedProductId;
... doing some bind work with productId
}
Product User Control
public static int selectedProductId;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void lvDepartments_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "selectDepartment")
{
selectedProductId = int.Parse(e.CommandArgument);
}
}
Thanks in advance...
In your Department User Control you are trying to get the value of selectedProductId before it is set in the Product User Control. That's why you don't get the value you expect until you postback twice.
You'll need to get it after the Product User Control sets it in the ItemCommand event. Perhaps placing the Department User Control code in the Page_LoadCompleted... though I'm not sure if that will work either.
Another way to do it is to have Product User Control set a public property in Department User Control instead of having Department User Control try to read a property in Product User Control.
The issue seems to be a Page Lifecycle issue.
http://www.robincurry.org/blog/content/binary/o_aspNet_Page_LifeCycle.jpg
I'm sure there's a better way than that as well.
Try using delegates to achieve this more cleanly, example here

Resources