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.
Related
I am trying to create a session when a user clicks a button. I want to update controls on the master.aspx page after the session is created from the login button being clicked. However the master.aspx page_load is being called before the button click function. Any ideas on how I could work around this?
Default.aspx.cs
protected void ButtonLoginIn_Click(object sender, EventArgs e)
{
Session["username"] = "Joe";
}
Master.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
LinkLoginInOut.Text = "Sign In";
}
else
{
string sessionId = Session["username"].ToString();
LabelDbUserName.Text = "Joe";
LinkLoginInOut.Text = "Log Out";
}
}
You can simply use FindControl on the Master
LinkButton lb = Master.FindControl("LinkButton1") as LinkButton;
lb.Text = "LinkButton Master";
You can try to move the code in the Page_Load event of the masterpage to an event, that is called after the button_click event. That way the changes to the session occur before the updating of the masterpage. You can try the Page_LoadComplete event. Please check the "asp.net page life cycle" documentation for the sequence, in which the different page events are called.
I have dynamically created button in c#, button is bind to page on page load event of page
Now, when I press the button from them then page loading twice, it is like page refreshing
How can I stop that re-binding of control when I click button?
my code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {}
else
{
fillquotes();
}
}
fillquotes()
{
//dynamicaaly create the button
msg.OnClientClick="lnkmsg(" + i + ")";
}
btnMsg_Click(object sender, EventArgs e)
{
------
}
client side:
function lnkmsg(id)
{
__doPostBack('<%= btnMsg.UniqueID %>', "");
}
when msg button clicks
pageload
msg_click()
pageload
msg_click()
You can prevent it by setting condition for checking Request.Form["__EventTarget"]. Check the spelling and syntax for it. May this solve your problem.
Enjoy Coding!!!
1the source page has a page load method like below:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Today.AddDays(1).ToShortDateString();
}
it will result a textbox1.text to display tomorrow's date when the source page is rendered. I have this source page cross post back to a target page, and in the target page load event i have
if (Page.PreviousPage != null && PreviousPage.IsCrossPagePostBack == true)
{
TextBox SourceTextBox1 = (TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox1 != null)
{
Label1.Text = SourceTextBox1.Text;
}
}
the problem is if the user changes the content of textbox1, supposely, the label1 on target page should catch the user input and display it, but now it only displays whatever i set in the source page load event. I understand the self page post back life cycle, but this is cross page post back. IMO, the source page load event has nothing to do with this, but why it overrides the user input?? Any idea.
Just surround this with a if(!IsPostBack) check:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TextBox1.Text = DateTime.Today.AddDays(1).ToShortDateString();
}
}
Otherwise the value will be overwritten on every postback. So when you Server.Transfer it to the other page it is already changed.
I hope you can help me. First, I'd like to tell you I am a desktop app guy, which means I mostly develop my apps in desktop. Now I am trying to build some web app but it leads me to am not sure if confusion or just am doing it wrong.
I have a code here that it populates a menu at runtime. Runtime because they the menu items are populated at code behind and the items are fetched in database.
here's the code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Menus menu = new Menus();
imgMainLogo.ImageUrl = VARIABLES.MainLogoImage;
menu.PopulateMenuControl(ref mainmenu, 2);
menu.PopulateMenuControl(ref footermenu, 9);
}
else
{
System.Diagnostics.Debug.WriteLine("link: " + footermenu.SelectedValue);
if (footermenu.SelectedValue != null)
{
Response.Redirect(footermenu.SelectedValue, true);
}
}
}
and the code in PopulateMenuControl
public void PopulateMenuControl(ref Menu menucontrol, int menuparentid)
{
//menucontrol.Items.Clear();
foreach (MenuFields mf in GetMenusByParentID(menuparentid))
{
MenuItem menuitem = new MenuItem(mf.MenuName, ReplaceSystemNameLink(mf.Link));
menucontrol.Items.Add(menuitem);
foreach (MenuFields cmf in GetMenusByParentID(mf.MenuID))
{
MenuItem childmenuitem = new MenuItem(cmf.MenuName, ReplaceSystemNameLink(cmf.Link));
menuitem.ChildItems.Add(childmenuitem);
}
}
}
So Page.IsPostBack is the very basic thing I should learn when doing something in a page. But the problem here is, one of my menu item in "footermenu" has an offsite link, and it should redirect the page into my blog.. but what's happening was, footermenu.SelectedValue is empty once I clicked on the "Blog" link.
What's going on?
UPDATE
I have updated the code still stuck, the SelectedValue is still empty
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Page_Load IsPostBack: " + Page.IsPostBack.ToString());
if (Page.IsPostBack)
{
if(footermenu.SelectedValue != null)
{
System.Diagnostics.Debug.WriteLine("link: " + footermenu.SelectedValue);
}
}
}
protected void Page_Init(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Page_Init IsPostBack: " + Page.IsPostBack.ToString());
if (!Page.IsPostBack)
{
Menus menu = new Menus();
imgMainLogo.ImageUrl = VARIABLES.MainLogoImage;
menu.PopulateMenuControl(ref mainmenu, 2);
menu.PopulateMenuControl(ref footermenu, 9);
}
}
You need to learn about the page lifecycle.
With dynamic controls (created and added in code), you need to re-create them on every page load - this is best done in the init event handler.
I have a page Product.aspx,there I have a user control ProductDisplay.ascx which has been created by drag and drop.
Now when a button is clicked in ProductDisplay.ascx,I want a logging function to be called which is in Product.aspx.
To achieve this I have used delegates
on ProductDisplay.ascx
public delegate void LogUserActivity(ProductService objService);
public event LogUserActivity ActivityLog;
on Product.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ProductDisp.ActivityLog += new User_UserControl_ProductDisplayBox.LogUserActivity(LogProduct);
}
}
Now button click event of ProductDisplay.ascx
protected void imgBtnBuyNow_Click(object sender, ImageClickEventArgs e)
{
if (ActivityLog != null)
{
ActivityLog(Product);
}
Response.Redirect("~/User/ShoppingCart.aspx");
}
My problem is that whenever i click this button ActivityLog is null.Why is it null?
My idea is that once i click this button,page posts back and its previous state is lost.
Please help me out with a reason and solution.
Secondly,I want to do away with null checking
**if (ActivityLog != null)**
{
ActivityLog(Product);
}
I saw some code which instantiates a delegate with a default value the moment it is declared,but i was not able to find it.Please help.
I have found solution to first problem
if (!IsPostBack)
{
ProductDisp.ActivityLog += new User_UserControl_ProductDisplayBox.LogUserActivity(LogProduct);
}
This was causing the issue.Move this line
ProductDisp.ActivityLog += new User_UserControl_ProductDisplayBox.LogUserActivity(LogProduct);
out of if (!IsPostBack)