Greetings,
I'm pretty new to the asp.net platform, and am having trouble doing the simplest of things.
As my title suggests, I'm wanting to dynamically change the body id on my master page in order to change the background and formatting of the page depending on what section of the website you're on. I have an idea on how to implement it, I'm just not versed enough with C# and asp.net to know how to code it.
I realize that people may say to utilize different methods than changing the body ID tag, but in my case with the liquid design of my website (full screen viewing), changing the body ID dynamically would be the easiest and most modular way for the way I have things laid out.
If anyone could shed some light, it would be much appreciated. As I'm a newbie and am still having trouble calling classes and passing information between pages, being as specific as possible would be also very much appreciated.
Thanks in advance.
Put an id for the body tag in your master page (I've named it body here)
<body runat="server" id="body">
Set the body id in your MasterPage class > Page_Load event (I added the following bit in Site.master.cs to set the body id to the content page's class name)
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// for page specific styling
body.ID = ContentPlaceHolder.Page.GetType().BaseType.Name;
}
}
Related
I have a page on a website that uses a master page and a child page. I would like to use the child page to create a new page on the website, but with a different master page.
Is it possible to do this without duplicating the code for the child page?
To make it clearer I have also added a link to a picture of the layouts used for the two pages.
In the picture, both children use the same code.
Edit: I have also thought about using a control for this, but I am not sure if this is the proper solution. The child page is pretty big and complex and also uses a lot of JavaScript.
When loading the child page, you can set the master page dynamically in code, in the PreInit event. Something like this:
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
That way your child page can set its master page based on whatever condition you would have in your site.
I'm a PHP/Rails developer and have inherited an ASP.NET application (and its maintenance). So I have a few simple questions.
1.) What's the makeup of a typical rendered(compiled?) HTML page in ASP.NET. That is, when a request is made what happens from the initial request to the time the HTML is displayed in the browser? I'm assuming some templates are combined and finally rendered but I'd like a more in-depth answer.
2.) I've been asked to remove a link from a Login form which is an aspx page. Looking at the aspx page itself it has an inherit statement, a link to the codebehind file, and links some other resources. Where do I actually remove the link from the Login page/template at? I've so far been unable to find exactly where the link is written so that I can remove it or comment it out.
Thank you!
That is, when a request is made what happens from the initial request
to the time the HTML is displayed in the browser?
I'd start learning about the ASP.Net Page Life Cycle.
I've so far been unable to find exactly where the link is written so
that I can remove it or comment it out.
I wouldn't do anything until you have at least a decent grasp of how ASP.Net works. It would be good to run through a few tutorials. ASP.Net has a nice Get Started section.
What's the makeup of a typical rendered(compiled?)
To give you a very simple instructions (trying) to help you fast understand it:
There is a page with the aspx tags, the asp.net is running the code behind and fill this tags with data.
After the filling with data on code behind, the asp.net is "running" the full page and if you have <% %> inside the aspx page, addition code runs that exist inside that.
This is a simple example:
public partial class Dokimes_StackOverFlow_Diafora : System.Web.UI.Page
{
public string cRenderMeAlso = "test";
protected void Page_Load(object sender, EventArgs e)
{
txtText.Text = "One Test";
}
}
<form id="form1" runat="server">
This will fill when the page is prepared
<asp:Literal runat="server" ID="txtText"></asp:Literal>
<br />
This will be render as the page reads out to send it to the browser
as php do
<%=cRenderMeAlso%>
</form>
Now in the place of the Literal control, you can have a full custom render control, that maybe a new complex part of a page with his elements and render.
Each page, master page, user control have a cycle of calls to help first pass all from Init() and prepare them, then pass all from Load(), and the other stage, giving the ability to initialize them in parallel - together.
Now, on PostBack the page have been keep some information's on ViewState that are posted together with the rest post data, and the Code behind use all that data to fill the controls. Also its fires on code behind any click event you have initialize on buttons and you can run some code there to do your work.
I've been asked to remove a link from a Login form
if you can not find that link is maybe on the standard login form that asp.net gives, the solution to that is to render the full template of the form, and remove it from there - but because there is the case to break the Login form, is better to not remove it and just hide it - because if you remove it and the code behind ask for it, it will throw an error - I mean for the standard asp.net forms login code that is part of the asp.net.
So if this is the case, render the login control as template (from design mode, do that on properties), see the link you search and ether make on code behind Link.Visible = false, ether remove it and delete on code behind all the reference on it.
I have a custom pager template on one gridview that the client now wants applied to several other gridviews within the same site. It seems like extending the GridView object makes the most sense, but I'm not clear on how to create the pager template dynamically.
Any recommendations on how to accomplish this?
Does this work:
gridview.PagerTemplate = Page.LoadTemplate("CustomPager.ascx");
http://msdn.microsoft.com/en-us/library/6d5z5yty(VS.80).aspx
UPDATE
For extension:
public class CustomGridView : GridView {
public override void OnInit(EventArgs e) {
base.OnInit(e);
this.PagerTemplate = Page.LoadTemplate("CustomPager.ascx");
}
}
I'm not sure why this article didn't turn up until now, but I found a nice tutorial on DotNetSlackers that got me going in the right direction.
http://dotnetslackers.com/articles/gridview/Custom-GridView-with-Paging-and-Filtering.aspx
Unfortunately, I did have to create the pager controls dynamically. If anyone knows a better way, I'm still all ears as I would much rather write HTML as is done in the PagerTemplate.
I want to assign one master page dynamically for a pure aspx file, Anybody can tell me, how to do this?
You can override OnPreInit in your default.aspx.cs and set the master page based on some value in your querystring. Something like this:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Request.QueryString["Master"] == "Simple")
MasterPageFile = "~/Masterpages/Simple.Master";
}
EDIT: the cause of your error message might be covered by this question.
I Left the ContentPlaceholder to add on it.. Actually , I tried to assign master page without using ContentPlaceHolder.. Now, I realised that, atleast one ContentPlaceholder should be there temporarily, even though we will change the master page dynamically...
Currently I have a site that is set up using a masterpage and a nested master page. The master page setups up the header and footer info. The nested masterpage is used once logged into the site.
The issue I have is that I want to programmatically load a different masterpage to define different header and foot info.
I don't think I can use the OnPreInit() in each content class to set a different masterpage. I don't think I can do this because each Content page uses the Nested Masterpage.
What I would like to do is programmatically set which masterpage is called in the NestedMaster.
Any ideas?
I saw this blog posted on another MasterPage question. Before trying this route I wanted to see if anyone else has experienced this.
Thanks
EDIT:
In the CS page:
public class AdminBasePage : BasePage
{
protected override void SetMasterPageFile()
{
Page.Master.MasterPageFile = "~/PathToMaster/Site.Master";
}
}
Look here:
Nested Master Pages
Page.Master.MasterPageFile = "~/PathToMaster/Site.Master";
(Thanks for the answer Ken and Brad, however I read most of that long tutorial before spotting the simple one line of code had been added to your question, so am making it more obvious by adding it as an answer here and will edit your post. Cheers)