asp:Content still visible when using Visible="False" - c#

I have a master page with couple ContentPlaceHolder inside it and added some content page of this master page.
I would like to set Visible="False" on one asp:Content in some page but it's not working as I'm still able to view data of both asp:Content controls.
Why?
Master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterBase.Master.cs" Inherits="MasterBase" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<!-- HEADER -->
<asp:ContentPlaceHolder ID="head" runat="server" />
<!-- CONTENT -->
<asp:ContentPlaceHolder ID="bodyContent" runat="server" />
<!-- FOOTER -->
...
</form>
</body>
</html>
Content Page
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server" Visible="False">
<!-- Some Data -->
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="bodyContent" runat="server">
<!-- Some Data -->
</asp:Content>

Try this
mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl("Content1");
mpContentPlaceHolder.Visible=False;

Related

GridView in Content tags

I have a masterpage in ASP.NET Web forms. I created a ContentPlaceHolder between the form tags on the master page. Then I added a web form (named Page.aspx) to the project with the aforementioned masterpage. I added a Content control to the Page.aspx. Then I wanted to add a GridView in Content tags on Page.aspx. But it gives me the following warning: " ASP.NET runtime error: Only Content controls are allowed directly in a content page that contains Content controls.". How can I fix problem? Sorry for my English.
You probably need to wrap them in these tags:
<ContentTemplate></ContentTemplate>
Master Page
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Master.master.cs"
Inherits="Master_Page" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<form id="form1" runat="server">
</form>
</html>
Page.aspx
<%# Page Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true"
CodeFile="Page.aspx.cs" Inherits="Faculty_Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:Panel ID="Panel2" runat="server" Height="449px">
<asp:GridView ID="GridView1" runat="server" >
</asp:GridView>
</asp:Panel>
</asp:Content>
Just make a try out of this,It may help

Can't add button to aspx page

I tried the following code to the Webform.aspx page:
<asp:Button ID="btnShowAssignLecturer" runat="server"
onclick="btnShowAssignLecturer_Click" Text="Assign Lecturer To Room" />
It says: System.Web.HttpException: Control 'ctl00_head_btnShowAssignLecturer' of type 'Button' must be placed inside a form tag with runat=server.
When I do that, I get another error. Not quite sure what to do.
Master page code:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="AllsWellHospital.Front_End.MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<h1>All Wells Hospital</h1>
<p>
<asp:Label ID="DateDisplay" runat="server"></asp:Label>
</p>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
<link id="Link1" href="/Styles/StyleSheet2.css" runat="server" rel="stylesheet" type="text/css"/>
<style>
body
{
background-color:#d0e4fe;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="topContent">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="WebForm1.aspx" Text="Web form.aspx" Value="Upload SP10"></asp:MenuItem>
</Items>
</asp:Menu>
</div>
</form>
</body>
</html>
Webform.aspx code:
<%# Page Title="" Language="C#" MasterPageFile="~/Front_End/MasterPage.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AllsWellHospital.Front_End.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium"
Text="Course Report"></asp:Label>
<asp:Button ID="btnShowAssignLecturer" runat="server"
onclick="btnShowAssignLecturer_Click" Text="Assign Lecturer To Room" />
</asp:Content>
Your button needs to be within a form tag and currently it isn't. Do you really want to place the button within the head portion of your form? Why not just add another content area for your main content. Try doing something like this:
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="AllsWellHospital.Front_End.MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<h1>All Wells Hospital</h1>
<p>
<asp:Label ID="DateDisplay" runat="server"></asp:Label>
</p>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
<link id="Link1" href="/Styles/StyleSheet2.css" runat="server" rel="stylesheet" type="text/css"/>
<style>
body
{
background-color:#d0e4fe;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="topContent">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="WebForm1.aspx" Text="Web form.aspx" Value="Upload SP10"></asp:MenuItem>
</Items>
</asp:Menu>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</form>
Your aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Front_End/MasterPage.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AllsWellHospital.Front_End.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium"
Text="Course Report"></asp:Label>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server"
onclick="btnShowAssignLecturer_Click" Text="Assign Lecturer To Room" />
</asp:Content>
A button must be placed inside of a server managed <form> tag in order for the post to actually do anything in the ASP pipeline.
However, your content place holder Content1 is inside the <head> tag of the master page, where <form> is invalid. You need to rethink your master page so that this content does not live inside of the <head> element, and include a <form runat='server'> surrounding your form.
You may have meant to use the HTML 5 <header> element, which is distintly different than the normal <head> element.

Master Page data didn't show in other pages in asp.net

I'm new in asp.net, I am currently in the basic of master.pages.
I am testing how to put an image header to my master page.
Here's my master.master:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="master.master.cs" Inherits="master" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<asp:Image ID="headerImage" runat="server" ImageUrl="~/images/cross-header.gif" />
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
And here's the Default.aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/master.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
When I run this, The imageHeader that I put in the master.master, didn't show up.
Any assistance from you guys will be greatly appreciated.
Thanks for all your help!
If you are trying to see the image in all your pages, move out image from ContentPlaceHolder in your MasterPage
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<asp:Image ID="headerImage" runat="server" ImageUrl="~/images/cross-header.gif" />
</asp:ContentPlaceHolder>
TO :
<asp:Image ID="headerImage" runat="server" ImageUrl="~/images/cross-header.gif" />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>

Content controls have to be top-level controls in a content page or a nested master page that references a master page

I want to use nested master page so i create the following master page :
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="test.master.cs" Inherits="DocumentFlowUI.test" MasterPageFile="~/MasterPage2.master" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
and i create the following page to use that master page :
<%# Page Title="" Language="C#" MasterPageFile="~/test.Master" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="DocumentFlowUI.WebForm4" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Content>
I get the following error :
Content controls have to be top-level controls in a content page or a
nested master page that references a master page
The HTML-code in your nested masterpage must be wrapped with an asp:content-tag with a contentplaceholderid from the "master" masterpage.
Just to demonstrate Erik's point:
Parent masterpage:
<asp:ContentPlaceHolder ID="head" runat="server" />
Child masterpage:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:ContentPlaceHolder ID="head" runat="server" />
</asp:Content>
Page:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<!-- content -->
</asp:Content>

How to set javascript on child page?

I have two pages, a master page and child page. How can I set javascript on child page?
I want implement textchange function on child page using javascript.
Master Page :
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Site" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="ContentPlaceHolderHead" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolderMain" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Child page :
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>
<asp:Content ID="ContentHead" ContentPlaceHolderID="**ContentPlaceHolderHead**" runat="server" >
<script language="javascript" type="text/javascript">
// You can write a javascript code HERE...
</script>
</asp:Content>
<asp:Content ID="ContentMain" ContentPlaceHolderID="ContentPlaceHolderMain" runat="server" >
<!-- Content -->
</asp:Content>
In other way, you can add a javascript file in the code-behind file of child page.
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl somejs = new HtmlGenericControl("script");
somejs.Attributes.Add("type", "text/javascript");
somejs.Attributes.Add("src", ResolveClientUrl("~/Content/js/something.js"));
this.Page.Header.Controls.Add(somejs);
}
Just Add scrip inside a contentplaceholder and make sure you set the master page file of your child page eg: MasterPageFile="~/Mymaster.Master".
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript" language="javascript">
//Your Script HERE
</script>
//YOUR HTML TAG HERE
</Content>
Regards
You could user ClientScriptManager.RegisterStartupScript in your child page.

Categories

Resources