I want to update master page hyperlinks from child page. Here is the code i written to update
master page elements.
HyperLink h1 = this.Master.FindControl("AnLogin") as HyperLink;
h1.NavigateUrl = "#";
h1.Text = Session["UserName"].ToString();
HyperLink h2 = this.Master.FindControl("AnLogout") as HyperLink;
h2.Text = "Logout";
h2.NavigateUrl = "~/Logout.aspx";
if (Session["UserType"].ToString() == "Admin")
{
Response.Redirect("~/Admin.aspx");
}
Master page is updating only when i am not redirecting to another page. If i am redirecting to another page, that hyperlinks remain same as static.
Here i need to update master page hyperlinks that should be same for all pages further i can traverse. How to accomplish this?
Here i suggest not to post back the page for such small operations.
You can set this condition on aspx page like :-
<% if (Session["UserType"].ToString() == "Admin") {%>
// do something
<%} else { %>
// do something
<%} %>
Related
I'm looking to execute code in my code behind on my Masterpage, and use it on the .aspx page of child pages like Default.aspx, without having to call it through the Default.aspx.cs page.
This is my attempt by accessing it like so <% MasterPage.getPlanCost() %>, however, this does not work. As there's "no definition" for getPlanCost()
Master Page code behind:
public string getPlanCost()
{
var country = getCountry();
string gbp = "£5.99";
string euro = "€6.99";
string usd = "$8.99";
var currencyCost = usd;
if (country == "United Kingdom") // gbp
{
currencyCost = gbp;
}
else if (country == "United States" || country == "Canada" || country == "Australia" || country == "New Zealand") // usd
{
currencyCost = usd;
}
else // euro
{
currencyCost = euro;
}
return currencyCost;
}
Default.aspx page:
<p class="text-center under-title text-muted"><%=MasterPage.getPlanCost() %> Cancel Anytime.</p>
What is the quickest / most efficient way of achieving this? Furthermore, I have tried to use alternate methods seen on StackOverflow, using get and set however I was unable to get this working. Fairly new to C# so I apologise.
Although you have found a workaround, it is possible to access master page methods from child web forms, useful in cases where want your child page to affect the master page in some way. You can do this through the Page.Master property, but you will first have to register the type or cast it.
Method 1: Registering Master Type
Web Form:
<%# Page Language="C#" MasterPageFile="~/Example.Master" ... %>
<%# MasterType VirtualPath="~/Example.Master" %>
Code Behind:
Page.Master.getPlanCost();
Method 2: Casting Master Property
Code Behind:
((Example)Page.Master).getPlanCost();
To anybody wondering, I created a class called Utilities.cs
Then called it directly from here from my Default.aspx page instead.
<%=Utilities.getPlanCost()%>
I'd also like to thank #Joel Coehoorn for his comments which got me halfway there.
I use code below to add several "MyControl.ascx" controls to my page, I have also used outputcache direction on .ascx file.
After the controls are cached, their output html will be the same!!!
How could I vary them?
Output cache direction:
<%# OutputCache Duration="120" VaryByParam="None" %>
C# Code:
for(int i=0;i<2;i++)
{
Control control = Page.LoadControl("MyControl.ascx");
control.ID = Guid.NewID().ToString();
Page.Controls.Add(control);
if(control is PartialCachingControl)
{
if(PartialCachingControl.CacheControl != null)
{
if(i==0)
((MyControl)control).style("color:yellow");
else
((MyControl)control).style("color:blue");
((MyControl)control).setTime(DateTime.Now.ToString());
}
}
}
I work with a master page in which there is one HyperLink. I set that HyperLink NavigateUrl form (master page's) child page.
for that i use this code in child page.
HyperLink hl = (HyperLink)this.Master.FindControl("linkviewmysite");
hl.NavigateUrl = "../" + ds.Tables[0].Rows[0]["username"].ToString();
Response.Redirect("Siteadmindata.aspx", false);
here linkviewmysite is the id of master page's HyperLink.
now problem is when i set NavigateUrl it's not work.
But when is remove this line (Response.Redirect("Siteadmindata.aspx", false);) from code than navigateurl set and HyperLink is work fine for me.
so now what should i do for it with Response.Redirect.
i got the answer
HyperLink hl = (HyperLink)this.Master.FindControl("linkviewmysite");
session["user_name"] = ds.Tables[0].Rows[0]["username"].ToString();
Response.Redirect("Siteadmindata.aspx", false);
now in master page page load
if(!postback)
{
if(session["user_name"]!=null)
{
linkviewmysite.navigationurl="../"+session["user_name"].tostring();
}
}
My master page contains this:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Label ID="User" runat="server" Text="Welcome, " Visible="false"></asp:Label>
</asp:ContentPlaceHolder>
I'm trying to access the label like this:
Label welcomeLabel;
ContentPlaceHolder cPlaceHolder;
cPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if (cPlaceHolder != null)
{
welcomeLabel = (Label)cPlaceHolder.FindControl("User");
if (welcomeLabel != null)
{
welcomeLabel.Text = "Welcome, " + u.Name;
welcomeLabel.Visible = true;
}
}
I've checked that ContentPlaceHolder1 is actually found and using HasControls() on it returns true but I can't seem to access the label.
Help me?
I also having same prblem.
what i have done is
MasterPage ctl00 = FindControl("ctl00") as MasterPage;
ContentPlaceHolder cplacehld = ctl00.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
Label label1= cplacehld.FindControl("User") as Label;
try with:
((Label)this.Page.Master.FindControl("User")).Text = "Welcome, " + u.Name;
((Label)this.Page.Master.FindControl("User")).Visible = true;
This issue dogged me for about two hours until I realized that if I have a asp:Content tag on my page with the contentplaceholderID set to the ID for the contentplaceholder in the master page that I will never be able to access any controls in the contenplacholder. The asp:Content page is always merged with the MasterPage content, even if the asp:Content tag is empty on your content page. To provide for the default content, I moved my label outside of the contentplaceholder tag and set the visibility to false. If I then dynamically determined that I didn't have any content for my asp:Content tag I would then just set the label visibility to true to display my default content. Not exactly elegant, but it works.
I want to get the page title in c# master page code behind.
I tried using Page.Header.Title; property but it return null.
Please help me to get the title.
Thanks in advance.
Shibin V.M
In your page head include runat="server" then you can easily get the Page title by
string Title = Page.Title;
EDIT:
Using the Title property of Page requires a header control on the page. (e.g. <head runat="server" />).
You can set the page title on Page_Load Event like this
protected void Page_Load(object sender, EventArgs e)
{ Page.Title = "Page Title"; }
and get the page title using
string title = Page.Title;
Set Title attribute of content pages <%# Page Title="Contact us" ../> and handle the Master page's Load or Init event to get Page.Title or Page.Header.Title.
try this in your master page
string Title = "Your Site: " + this.Page.Title;
Use this in your html tag, it has worked for me <%: Page.Title %>.
For eg: <li class="active"><%: Page.Title %></li>.