I am creating a website in ASP.Net using MasterPage . I need Help for using different page title on each page .
eg: My Company : Home
and My Compant : offerings on my second page how to do using a MasterPage
Master
...
<title>
<asp:ContentPlaceHolder ID="TitleContentPlaceHolder" runat="server"></asp:ContentPlaceHolder>
</title>
...
Content
...
<asp:Content ContentPlaceHolderID="TitleContentPlaceHolder" runat="server" ID="TitleContent">
<asp:Literal runat="server" ID="TitleLabel"></asp:Literal>
</asp:Content>
...
Content codeBehind
protected void Page_Load(object sender, EventArgs e)
{
...
TitleLabel.Text = "Some title";
...
}
there is a section on your child page as follows
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
You can put anything in-between which you write in the head section.
<%# Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
here you can set the title in the page directive
or on the code behind
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "Master Page ";
}
You can use codebehind to give title to your content pages..
protected void Page_Load(object sender, EventArgs e)
{
Page.Title="your title";
}
This is the easiest way which you can follow..
Related
I have a property x in my code-behind file in a web application project. It gives me compile time error while it does not give any error in website project
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="test.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= x2 %>
<%# x2 %>
</div>
</form>
</body>
</html>
Codebehind file :
using System;
namespace test
{public partial class WebForm1 : System.Web.UI.Page
{
public int x2 = 2;
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
It is working fine when I use the same technique in my website project
Kindly help
Thanks
Check if there are any other errors in your code behind WebForm1.aspx.cs.
like missing reference or any other compile error.
This works for me. Are you sure there's a value in ViewState? On form load add a value to test.
ViewState["FormMode"] = "ViewApplicant";
lblMessage.DataBind();
Also make sure the label has some text to view. It could be showing but is empty.
<asp:Label ID="lblMessage" runat="server"
Text="some text"
Visible='<%# FormMode == "View" || FormMode == "ViewApplicant" %>'>
</asp:Label>
Your code is perfectly okay. While accessing the variable/property initillay, the red underline may appear under the expression indicating the compile error, but when you build the solution, there should be no error.
Your original post has changed so here's a new answer.
Try removing the namespace from the code behind and change 'inherits' on the page to Inherits="WebForm1".
page directive:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebForm1" %>
code behind:
using System;
public partial class WebForm1 : System.Web.UI.Page
{
public int x2 = 2;
protected void Page_Load(object sender, EventArgs e)
{
}
}
'Inherits' must match the partial class name.
I have two User Controls. And one aspx page which is loading the controls. Code is given Below
Home.aspx:
<%# Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="MYApp.Home" %>
<asp:Panel ID="pnlTabs" runat="server"></asp:Panel>
Home.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
base.LoadUserControl(UC_Tabs, pnlTabs);
}
Tabs.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UC_Tabs.ascx.cs" Inherits="MyApp.UserControls.UC_Tabs" %> <asp:Button ID="btnPhotoTab" runat="server" Text="Photos" CssClass="tab_style" style="border-right:none;" onclick="btnPhotoTab_Click" />
<asp:Panel ID="pnlPhotos" runat="server"></asp:Panel>
Tabs.ascx.cs:
protected void btnActivityFeed_Click(object sender, EventArgs e)
{
Home HomePage = (Home)this.Page;
LoadUserControl("UC_PhotoSearch.ascx", pnlPhotos);
}
public Control LoadUserControl(string ControlName, Control Container)
{
Control UControl = null;
if (ControlName != null)
{
UControl = this.LoadControl("~/UserControls/" + ControlName);
UControl.ID = UControl.GetType().Name;
Container.Controls.Add(UControl);
}
return UControl;
}
UC_PhotoSearch.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UC_PhotoSearch.ascx.cs" Inherits="MyApp.UserControls.UC_PhotoSearch" %>
<input id="txtSearch" name="txtSearch" type="text" runat="server" />
<asp:Button ID="btnSearch" runat="server" Text="Search" onclick="btnSearch_Click" />
<span id="PhotoSearchedData" runat="server"></span>
UC_PhotoSearch.ascx.cs:
protected void btnSearch_Click(object sender, EventArgs e)
{
//Photo Search Code here
}
The above is all my code.
Now I am loading the Tabs control on Home page and
Photo search Control on Tabs control
Now I click on Search Button But the
btnSearch_Click in not firing and even the page load event is also not firing.
How can I able to Access the btnSearch_Click event from home page.
If I used the following on Tabs control then the button click is working fine.
<%# Register Src=”UC_PhotoSearch.ascx” TagName=”PhotoSearch” TagPrefix=”uc1″ %>
<uc1:PhotoSearch ID=”PhotoSearch″ runat=”server” />
So what is wrong with dynamic loading of controls.
Please Help me
Not sure if I understand you quite, but I think if you load your controls in the Page's PreInit event and not Page_load your events will fire.
My content page looks like this:
<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Basket.aspx.cs" Inherits="Basket" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
Now, I would like to add some controls dynamically to the content when page loads, so I am trying the following code:
protected void Page_Load(object sender, EventArgs e)
{
Content2. // I want to add controls to it dynamically
}
The problem is that the Content2 control is not visible by the compiler and I am getting error about missing directive or assembly reference.
Any solution?
The reason you can't get a reference to that asp:Content control is because it does not stay around when the page is combined with the masterpage. Basically ASP takes all the controls from inside these asp:Content sections and makes them children of the ContentPlaceholder controls inside the masterpage.
As MSDN says: A Content control is not added to the control hierarchy at runtime. Instead, the contents within the Content control are directly merged into the corresponding ContentPlaceHolder control.
That means that if you want to add more controls to that section, you will have to get a reference to the ContentPlaceholder control in the masterpage and add them to it. Something like:
ContentPlaceHolder myContent = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
myContent.Controls.Add(??);
Notice you are using the ContentPlaceHolderID value, NOT the ID of the asp:Content section.
I will recommend that you put a placeholder control in content and use it to add controls. For example,
<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Basket.aspx.cs" Inherits="Basket" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<asp:Placeholder runat="server" ID="Content1Controls" />
</asp:Content>
..
And
protected void Page_Load(object sender, EventArgs e)
{
Content1Controls.Controls.Add(...
}
in my web application i am uploading a video file. While uploading i want to give message to user wait video uploading can i use UpdateProgress can u give me a simple example. Thank you
Hi there here is a small example how you display a updateprogress on a background job:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
UpdateProgress control
Loading...
protected void UpdateButton_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
}
I am feeling that I have terribly wrong somewhere. I was working on a small asp.net app. I have some dynamic themes in the \theme folder and have implemented a page base class to load the master page on the fly. The master is having the ContentPlaceHolder like:
<asp:ContentPlaceHolder ID="cphBody" runat="server" />
Now I am adding pages that are derived from my base class and added the form elements. I know, Visual Studio has problem showing the page in the design mode. I have a dropdown box and wish to add the event of onselectedindexchange. But it is not working. the page is like this:
<%# Page Language="C#" AutoEventWireup="true" Inherits="trigon.web.Pages.MIS.JobStatus" Title="Job Status" AspCompat="true" CodeBehind="JobStatus.aspx.cs" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" runat="Server">
<div id="divError" runat="server" />
<asp:DropDownList runat="server" id="jobType" onselectedindexchange="On_jobTypeSelection_Change"></asp:DropDownList>
</asp:Content>
I have also tried adding the event on the code behind like:
protected void Page_Load(object sender, EventArgs e)
{
jobType.SelectedIndexChanged += new System.EventHandler(this.On_jobTypeSelection_Change);
if (!IsPostBack)
{
JobStatus_DA da = new JobStatus_DA();
jobType.DataSource = da.getJobTypes();
jobType.DataBind();
}
}
protected void On_jobTypeSelection_Change(Object sender, EventArgs e)
{
//do something here
}
Can anybody help?
Regards,
set AutoPostBack="true" on your dropdown