Ok this an odd way of doing this, I know I should have done it on Page_Load in every pages when using Masterpage, but is there a way around this issue? I don't want to rebuild the pages if I can help it,if I can only insert a title on <asp:content></asp:content> it would be easier.
If anyone has run into this or maybe have a suggestion of a good way to do it , I know jquery can do it document.title ='' but I heard it's not SEO friendly.
Thanks!
You can still set the title in each page that's using a MasterPage. In markup: -
<%# Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc
Or in code: -
protected override void OnLoad(EventArgs e)
{
Page.Title = "Your Title";
base.OnLoad(e);
}
you can place a contentplaceholer in your master page as..
<asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder>
After that In content page add its reference as..
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title> Your title goes here. </title>
</asp:Content>
more simple solution in Master page
<%: Page.Title %> - the main title goes here
in content page first line of it
<%# Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc
I know this is an older question but what definitely works for me is to set a Literal control in the title tag of the MasterPage like this:
<title><asp:Literal runat="server" id="pagetitle" Text="MyTitle"></asp:Literal></title>
Then, in the content page, put this in the Page_Load method:
Literal myTitleLiteral = (Literal)Master.FindControl("pagetitle");
if (myTitleLiteral != null)
{
myTitleLiteral.Text = "Test Title";
}
Related
I have done my MasterPage and create a new .aspx page with this MasterPage . but whenever I try to add a RadioButton to the .aspx page it's generate to me this error:
Control 'head_ctl00' of type 'RadioButton' must be placed inside a
form tag with runat=server.
and I have tried to add a form tag and this error came out !
A page can have only one server-side Form tag.
I'm confused how I can solve this problem ! any idea ?
in case you need my .aspx page :
<%# Page Title="" Language="C#" MasterPageFile="~/MySite.Master" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="MyWebsite.Registration" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<form id="frm" runat="server">
<asp:RadioButton runat="server"></asp:RadioButton>
</form>
</asp:Content>
You have inserted your RadioButton inside the head placeholder that has not the form tag in your MasterPage file. You should place RadioButton in another PlaceHolder like MainContent or FeaturedContent. Also you don't need the form tag in your ContentPages, The form tag on the master page will be sufficient.
This question most likely has an easy solution, but I cannot figure it out. I have a .aspx page with the following code:
<%# Page Title="Update ASV Information" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UpdateASV.aspx.cs" Inherits="COAF_Process_to_ASV_Relation_Tool.UpdateASV" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" >
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
<asp:Button runat="server" ID="asv_update" OnClick="asvUpdate_Click" Text="Update ASV" />
</asp:Content>
I want to be able to write text from the C# code behind this page (UpdateASV.aspx.cs). Whenever I try:
Response.Write("some text");
It puts the code behind the content2 placeholder. I want it inside. Is there an easy way to do this?
Response.Write directly modifies the response. You don't want to do that in your web forms application. You should be modifying the contents of a Label, Literal, or PlaceHolder control from the code-behind.
Use Label to put text into the page
Use Literal to put raw html into the page
Use PlaceHolder to add new controls to the page dynamically.
Either way, the placement of the control(Label, Literal, or PlaceHolder) on your page determines where on the page your output will be rendered.
If you are determined to stick to server side controls:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
Literal myText = new Literal()
{
Text = "some text"
};
Content2.Controls.Add(myText);
}
}
Otherwise, just put the code inline
<%=Response.Write("some text") %>
This is my first SO post, so please forgive any faux pas. I feel as though I've tried everything on every post related to this error (disable viewstate, noCache, etc) and I'm at my wits end.
I have a blank project with a single master page, a single page, and a single control.
The page (aspx) loads using the master page. This works fine. There is a button on the page which loads the control (the ascx) onto the aspx in the section called divRightMainView. This also works fine (which is where my problem seems to differ from all others I've found...). There is a button on the ascx which is supposed to call the code-behind of the ascx - this is where I get the "state information" error.
This is the aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Outer.aspx.cs" Inherits="TestProject.Outer" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
function loadUserView(viewToLoad) {
PageMethods.RenderControl(viewToLoad, onSuccess, onError);
}
function onSuccess(results) {
alert(results);
var command = results.split('##')[0];
if (command == 'loadView') {
var htmlToLoad = results.split('##')[1];
$get('divRightMainContentView').innerHTML = htmlToLoad;
}
}
function onError(error) {
alert('error ' + error);
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div id="leftSideContent">
<input type='button' id="clickToLoadASCX" value="Click Me" onclick="loadUserView('/inner.ascx');"/>
This is the "Outer.aspx" page which will hold the ascx after the button is clicked
</div>
<div id="divRightMainContentView">
</div>
</asp:Content>
Aspx code behind (which renders the ascx)
[WebMethod]
public static string RenderControl(String controlName)
{
Page page = new Page();
Control control = page.LoadControl(controlName);
HtmlForm form = new HtmlForm();
form.Controls.Add(control);
page.Controls.Add(form);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
return "loadView##" + writer.ToString();
}
This is the ascx called inner.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="inner.ascx.cs" Inherits="TestProject.inner" %>
<div>
This is the inner text. When I click the button below I should proceed to inner.ascx.cs to the "PageBehindCall_Submit".
<asp:Button ID="innerButton" runat="server" onclick="PageBehindCall_Submit" Text="Submit"/>
</div>
And finally, the ascx code-behind
protected void PageBehindCall_Submit(object sender, EventArgs e)
{
string str = "This call does not work!";
}
I'm hoping to be able to use the "PageBehindCall_Submit" to process data, grab inputs from the ascx, etc. Let me know if there is a way to make this happen, or another possible work-around? Any help would be appreciated!
Why not load the ascx on page load of the ascx but turn the visibility off rather than manually rendering user control html. This is very weird that it does not hook up with the event in ascx as the control belongs to ascx. But I think you should add it in aspx then try to turn on visibility whenever you need it.
To manage the page title on my pages, I have a master page with a ContentPlaceHolder in the header.
<head runat="server">
<asp:ContentPlaceHolder runat="server" ID="headContent">
</asp:ContentPlaceHolder>
</head>
On each of my pages, I add the meta tags and page title as follow:
<asp:content id="Header" contentplaceholderid="headContent" runat="server">
<meta name="keywords" content="keyword1, keyword2" />
<meta name="description" content="page description" />
<%Page.Title = "My page title";%>
</asp:content>
I cannot modify the code on the pages by placing the Page.Title in the OnInit method of the page.
I need access to the page title in the codebehind of the master page, but I always get an empty title when I use Page.Title.
By using <%Page.Title = "My page title";%> you implicitly tell the ASP.NET to execute this embedded code block during the page's render phase.
What does it mean? You won't be able to get this value before the page's render phase. Assuming you're trying to get this value a little bit earlier than during the render. That's why you get the empty string.
The workaround could be in setting your Title property of the <%# Page directive in the beginning of your page e.g.:
<%# Page Title="My Title Goes Here" Language="C#" ... %>
By setting this you'll be able to access the Page.Title property from your master page a little bit earlier than the page's rendering occurs.
Just use
<title>My page title</title>
If i have an ASP.NET MVC 2 Web Application with the following Views:
Main.aspx
Page1.aspx
Page2.aspx
And all Views inherit from a MasterView called Site.master,
I want to be able to have a default Title/H1 for the page, which can be overriden in the deriving Views.
E.g Main.aspx would have "MySite - xxx", Page1.aspx would have "MySite - Page 1", Page2.aspx would have "MySite - Page2".
And if i choose not to set the Title/H1 in a new derived view, the master Title/H1 would be displayed.
With WebForms, i would accomplish this in the following way:
Make the Title/H1 tags on the master runat="server"
Expose protected properties on the master code-behind
On Page_Load of the derived pages code-behind, set the property (Master.Title = "Page 1").
On Page_PreRender of the master code-behind, set the Title/H1 tags to "My Site - " + Title;
How can we accomplish this with ASP.NET MVC?
I could do this in the master:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
And then set it in the view:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MySite - Page 1
</asp:Content>
But i wanted to be able to only specify "Page 1" in the View, and the title gets magically changed to "MySite - Page 1". Know what i mean? The "MySite - " part of the title needs to be the template for the title.
I'm probably missing something obvious here. :)
With a quick search I found this:
http://haacked.com/archive/2009/04/03/tipjar-title-tags-and-master-pages.aspx
explains why
<title>MySite<asp:content..../></title>
doesn't work
This is how I usually do it:
<title>MySite - <%: Page.Title ?? "Default title" %></title>
in your MasterPage.
Then, you can define the Title property on a content page like this:
<%# Page Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage"
Title="Page 1"
%>
Edit:
Well, you might want to see this SO question: ASP.NET MVC - View with master page, how to set title?.
Is much easier as you are describing it.
Just add a content placeholder to your master page
<title>
My Site - <asp:ContentPlaceHolder ID="PageTitle" runat="server" />
</title>
Then in your Content Page use it as
<asp:Content ID="Content3" ContentPlaceHolderID="PageTitle" runat="server">
My Page
</asp:Content>
This way will work but you have to use an HTML HEAD tag, not a server control. So simply remove runat="server" from your HEAD.