My master page:
public partial class MasterPages_Main : System.Web.UI.MasterPage
{
public bool IsLoggedIn;
protected void Page_Load(object sender, EventArgs e)
{
// Check login
LoggedInUser ThisUser = new LoggedInUser();
IsLoggedIn = ThisUser.IsLoggedIn;
Response.Write("Master" + IsLoggedIn.ToString());
}
This outputs 'True', we are logged in.
On my content page I do:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("CONTENT:" + Master.IsLoggedIn.ToString());
}
But this outputs 'False'.
So the actual page output is:
Content:False
Master:True
On my content page I need to redirect if the user is logged in, but this value is always false from the content pages point of view! How can I resolve this?
Content Page load event occurs before Master Load (from here). So you probably need to change the logic, and maybe call some content page's methods from master Page_Load. Or set IsLoggedIn inside Master Init event handler.
Change Master Page_Load to Page_Init, this will force it to execute before the content page.
The master page is called after your code for Page_Load(). Try this:
Protected void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender,e);
Response.Write("CONTENT:" + Master.IsLoggedIn.ToString());
}
Related
The scenario is very simple. I have an aspx page with a user control. I want to set a value and get a response from usercontrol on aspx page's pageload. The SET job is done, but can't GET the response. It's always empty. I tried two methods, but none of them worked.
ASPX PAGE
<uc1:ucContent ID="Content1" runat="server" />
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
// SET job is working without any problem
Content1.ItemName = "X";
//METHOD ONE:
Page.Title = Content1.MetaPageTitle;
//METHOD TWO:
HiddenField hdnPageTitle = (HiddenField)Content1.FindControl("hdnMetaPageTitle");
Page.Title = hdnPageTitle.Value;
}
USER CONTROL
protected void Page_Load(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(itemName))
{
// GET DATA FROM DB
// METHOD ONE:
hdnTitle.Value = DB.DATA;
// METHOD TWO:
metaPageTitle = DB.DATA;
}
}
private string metaPageTitle;
public string MetaPageTitle
{
// METHOD ONE:
get { return metaPageTitle; }
// METHOD TWO:
get { return hdnTitle.value; }
}
EDIT
itemName is a UserControl property to get a value from Parent Page:
private string itemName;
public string ItemName
{
set { itemName = value; }
}
Thanks for your kind help in advance!
I think that the problem is that the page's Page_Load is triggered before the UserControl(Have a look: asp.net: what's the page life cycle order of a control/page compared to a user contorl inside it?).
So you could set the property in Page_init:
In your UserControl:
protected void Page_Init(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(itemName))
{
// GET DATA FROM DB
hdnTitle.Value = DB.DATA;
}
}
Now this should work in your page's Page_Load:
Page.Title = Content1.MetaPageTitle;
Normally i would prefer another kind of communication between a Page and a UserControl. It's called event-driven communication and means that you should trigger a custom event in your UC when the MetaPageTitle changed (so in the appopriate event-handler).
Then the page can handle this specific event and react accordingly.
Mastering Page-UserControl Communication - event driven communication
I have MasterRoot.Master and LoginPage.aspx
How can I set value of Session to HeadLoginName after user success Login.
Login.aspx.cs
protected void btnLogin_Click(object sender, EventArgs e) {
UserClass user=new UserClass();
user.LoginProcess(txtUserName.Text,txtPass.Text);
if(user.loginsuccess) {
Session["UserName"]=user.username.ToString();
}
}
MasterRoot.Master.cs
protected void Page_Load(object sender, EventArgs e) {
// Like this HeadLoginName.Text=Session["UserName"].ToString();
}
The Load() event in the master page is executed before you assigns the Session["UserName"] and therefor it is empty after the user clicked on the Login. You either need to use Response.Redirect to redirect to some page and "refresh" a page or assign the value of the Label on master page directly from content page
if(user.loginsuccess) {
Session["UserName"]=user.username.ToString();
// Gets a reference to a Label control that not in a ContentPlaceHolder
Label mpLabel = (Label) Master.FindControl("HeadLoginName");
if(mpLabel != null)
Label1.Text = user.username;
}
Read more about how to access Master page from Content page and about Events in ASP.NET Master and Content Pages
if you just wanted to assign the session value with out any exception you can do like this in master page
HeadLoginName.Text=(Session["username"]==null)?"some value you like":Session["username"];
and when the user logged in successfully you can give like this in child page
Session["username"]=usernametxt.Text;
Then the value will be assigned in your master page on the page postback;
How I can make a link (button) that is located in a master page to redirect to two different url's depending if it is clicked from diferent .aspx pages ?
you can do something like this in your aspx page load method,
protected void Page_Load(object sender, EventArgs e)
{
try
{
/// Access Master Page control
LinkButton lnkButton = (LinkButton)this.Master.FindControl("lnkButtonName");
/// set postback url or whatever
lnkButton.PostBackUrl = "";
}
catch(Execption e)
{}
}
You could set it from the Master's Page_Init event handler and use HttpContext.CurrentHandler to get the page reference:
protected void Page_Init(Object sender, EventArgs e)
{
var page = HttpContext.Current.Handler as Page;
if (page is WebForm1)
LinkButton1.PostBackUrl = "WebForm2.aspx";
else if (page is WebForm2)
LinkButton1.PostBackUrl = "WebForm1.aspx";
else
LinkButton1.PostBackUrl = "WebForm3.aspx";
}
Do something like this (untested)...
Inside master page (for this example, it's named siteMaster), add public property:
public void SetButtonUrl(string NewUrl)
{
this.btnMyButton.NavigateURL = NewUrl;
}
In the codebehind of your aspx pages, add your desired url in each:
((siteMaster)Master).SetButtonUrl("~/someurl");
In my master page, I'm loading a variable in the session like this:
public partial class TheMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewUserPreferences SessionUserPreferences = new ViewUserPreferences();
SessionUserPreferences = UserPreferences.GetUserPreferencesFromDB(6);
}
}
}
Then, in the code behind of a file, I have this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var test = Session["SessionUserPreferences"];
}
}
But when I debug, test is null. What's causing the problem?
Also, if I put a break point in the master page, it doesn't trigger when I run the aspx page; is this normal?
Thanks.
First thing you are missing the assignment part for UserPreferences.GetUserPreferencesFromDB(6) to the Session object. (I read the comments for #Greg's answer and you mentioned that even after that it is not working.)
Second, Master Page's Page_Load Event is triggered after the Current Page's Page_Load Event, hence the value of Session["SessionUserPreferences"] is null in Current Page's Page Load event since it is not set yet.
Check this link for further information on Page Events:
http://msdn.microsoft.com/en-us/library/dct97kc3.aspx
You have to do Session["SessionUserPreferences"] = something; somewhere before you attempt to retrieve that. Are you setting it somewhere else that you didn't show?
Here's the problem:
I have an (almost) empty aspx page and I want to insert a certain number of the same user control in this page.
The fact is that I don't know how many of them I will need.
I tried to add them from the CodeBehind but it seems that the UserControls are completely empty.
In the main page (MainDiv is a div with runat="server"):
protected void Page_Init(object sender, EventArgs e)
{
WebUserControl1 uc = new WebUserControl1();
WebUserControl1 uc1 = new WebUserControl1();
MainDiv.Controls.Add(uc);
MainDiv.Controls.Add(uc1);
}
(it doesn't work if i put this code on Page_Init, Page_Load or Page_PreRender)
UserControl (gw is a Gridview contained in the UserControl):
protected void Page_PreRender(object sender, EventArgs e)
{
if (_data != null)
{
gw.DataSource = _data;
gw.DataBind();
}
}
when I arrive there, gw is null (this.Controls.Count is 0).
How can I solve this?
That's what the repeater control is for.