Link in master page - c#

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");

Related

How to pass value from user control to aspx page on pageload?

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

How to set value of Session to LoginName without Membership in Asp.Net

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;

to click link button it will change the label description using asp.net

I am new to ASP.NET and C# and have one doubt.
I have one link button in master page.
When I click that button it will change the label description in the content page.
Can anyone help, it's very useful for me?
Thank you
on Link Button click event of master page you can find control of your content page using below mentioned code.
protected void lnk_Click(object sender, EventArgs e)
{
Label lbl = (Label)(this.ContentPlaceHolder1.FindControl("lbl"));
lbl.Text = "Test";
}
You find what your looking for here http://www.snippetbank.net/detail/snippetdetail/9-aspcsharp/8-miscellaneous/556-Update-Content-Label-Text-From-Master-Page.html
For this follow these steps:
Add an event in the master page class using below code:
public event EventHandler MasteridButton_Click;
Write this code on button click:
public void idButton_Click(object sender, EventArgs e)
{
if (MasteridButton_Click != null)
{
MasteridButton_Click(sender, e);
}
}
Add this page directive in the page which you want to get this event
<%# MasterType VirtualPath="~/[YourMasterPageName].Master" %>
Add this code to your page class
protected void Page_Init(object sender, EventArgs e)
{
this.Master.MasteridButton_Click +=new EventHandler(Master_MasteridButton_Click);
}
protected void Master_MasteridButton_Click(object sender, EventArgs e)
{
this.lblMyLable.Text = "My Text";
}

ASP.net master page/content page simple design problem

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());
}

using sessions variable

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?

Categories