Hi, I'm a decent programmer but am completely new to the Web Development side of things, and am kind of winging it to build a website. I'm using Visual Studios 2008/ASP.net/C#.

So basically, I'm keeping things simple at the moment. I have a master file that contains a 'header', 'content', and 'footer' section. In the header section I have a row of buttons, which when clicked should change the contents of the 'content' section. I have a corresponding .aspx file for each of these, with generic contents to start:

<%@ Page Language="C#" MasterPageFile="~/Master.master" 
    AutoEventWireup="true" CodeFile="Content1.aspx.cs" Inherits="Content1" %>

<asp:Content ID="content1" ContentPlaceHolderID="content_section" runat="server">
    <h1>CONTENT 1</h1>
</asp:Content>

Repeat from content1 to content5 to match 5 buttons.

Now I need to fill in this section (I think):

protected void btnContent1_Click(object sender, EventArgs e)
    {
        
    }

but am not sure how it's done. Any help would be appreciated, thanks.

I don't know asp but in javascript it looks something like this.
document.placeholder.src = "hello.gif"

I don't know asp but in javascript it looks something like this.
document.placeholder.src = "hello.gif"

Thanks for the suggestion, but yeah it isn't quite the same. I can access my placeholder within the code, typing "content_section." brings up a list of functions, but there's no .src or .source, and ones like .page or .id return a value rather than set it.

Browsing around some more any examples I've found online focus on using LinkButton objects combined with a Sitemap within the .aspx file to change the place holder. I haven't been able to translate this into using a regular Button's event handling.

Hi

could you perhaps give more detail on what you want to achieve?

if you are trying to show different content at different times by the click of a button. You could use a <div>..
Example:

<div id="myContent" runat="server"></div>

Once creating the <div> and setting it's "runat" property to "server" you can call the div from within the code behind, you can set the visible property to true or false as well, and you could dynamically create controls within it

Hi

could you perhaps give more detail on what you want to achieve?

Sorry, got a little off track, but I'm getting back to this problem now. Part of the problem is I want to do it how architact suggested, something like:

on button# click
placeholder.src = "page#.aspx"

But the ASP placeholder object doesn't work like this, rather it seems to be backwards, the content placeholder is assigned to "page#.aspx" instead? This is my default.aspx file, separate from my master file:

<%@ Page Language="C#" MasterPageFile="~/EgocentricMaster.master" AutoEventWireup="true"  
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="default_content" ContentPlaceHolderID="main_placeholder" runat="Server">
       <h1>DEFAULT</h1>
</asp:Content>

I have several .aspx files set up like this at the moment. Which I want to swap in and out of the main content section. Which looks like this in my master file at the moment:

<div id="master_maincontent" runat="server">
            <asp:ContentPlaceHolder id="main_placeholder" runat="server">
            </asp:ContentPlaceHolder>
    </div>

Using daniweb.com for an example, what I'm trying to accomplish is the "HOME|FORUMS|TUTORIALS|CODE SNIPPETS|BLOGS|LINK DIRECTORY" part of the header. The header always stays the same but when you click on one of the options the content below changes. Except instead of using an ASP:LinkButton type which they seem to use I want to do this with the standard ASP:Button, programmatically.

Maybe this isn't a good idea and I should just stick with LinkButton's?

if u have five contentplaceholders in u r master page,u can use this to find control of each contentplaceholder
   ContentPlaceHolder mcon = new ContentPlaceHolder();
        mcon = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
        u can make visible false for other content placeholders on each button click

u can use tab container control in ajaxcontroltoolkit ,if u are using ajax

if u have five contentplaceholders in u r master page,u can use this to find control of each contentplaceholder
   ContentPlaceHolder mcon = new ContentPlaceHolder();
        mcon = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
        u can make visible false for other content placeholders on each button click

That's just about it. I only have one "ContentPlaceHolder" actually, and five "Content" objects each in their own .aspx file. These are what I want to swap in and out of the placeholder on a button click. Using your code sample, I came up with:

protected void button1_Click(object sender, EventArgs e)
{
        Content con = new Content();
 
        con = (Content)Master.FindControl("~/Page1.aspx").FindControl("content1");
        masterplaceholder.ID = con.ContentPlaceHolderID;
}

I think this should work, but I'm getting a Null Object reference on "content1". This object is listed in the code example of my first post. If I keep working at it I can probably get it myself, but I'm heading out for the day so I'll leave the question up here.

I'm also wondering if I shouldn't be referencing the content in Page1.aspx directly rather than creating an extra variable for it?

what u want u want to change the content of the page page.aspx .whats the meaning of content(),can u post u r code here.

what u want u want to change the content of the page page.aspx .whats the meaning of content(),can u post u r code here.

Ok, I was trying to do this without simply posting everything, but here it is. As I'm just starting out in web development (with a hobbyist mentality) I'm keeping everything very basic right now, just trying to lay down the framework to start.

My masterfile contains 3 main DIV sections, a header, the main content, and a footer. Things are being organized with a stylesheet which I don't think needs to be posted.

MasterPage.master

<%@ Master  Language="C#" 
            AutoEventWireup="true" 
            CodeFile="MasterPage.master.cs" 
            Inherits="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="MasterHead" runat="server">
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <form id="MasterPageForm" runat="server">
    
    <div id="master_header">
        <div id="master_headertop">
            <asp:Image runat="server" ID="HeaderImage" 
                ImageUrl="~/Images/logo.GIF" 
        </div>
        <div id="master_headerbottom">
            <asp:Button ID="btnHome" runat="server" Text="Home" Width="150" 
                onclick="btnHome_Click" ></asp:Button>

            <asp:Button ID="btnPage1" runat="server" Text="Page1" Width="150" 
		onclick="btnPage1_Click"></asp:Button>

            <asp:Button ID="btnPage2" runat="server" Text="Page2" Width="150" 
                onclick="btnPage2k"></asp:Button>

            <asp:Button ID="btnPage3" runat="server" Text="Page3" Width="150" 
                onclick="btnPage3"></asp:Button>

            <asp:Button ID="btnPage4" runat="server" Text="Page4" Width="150" 
                onclick="btnPage4"></asp:Button>

            <asp:Button ID="btnPage5" runat="server" Text="Page5" Width="150" 
                onclick="btnPage5"></asp:Button>        
        </div>
    </div>

    <div id="master_content" runat="server">
        <div id="master_contentplaceholder">
            <asp:ContentPlaceHolder id="mainContent" runat="server">
		<H1>HOME</H1>
            </asp:ContentPlaceHolder>
        </div>
    </div>
    <div id="master_footer">
	/*...Footer stuff*/
    </div>    
    </form>
</body>
</html>

Here's the current C# code for the master page, where I'm trying to implement the button functionality. The button_click function is the default setup by Visual Studios.

MasterPage.Master.CS

public partial class MasterPage : System.Web.UI.MasterPage
{


    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnHome_Click(object sender, EventArgs e)
    {        
    }

    protected void btnPage1_Click(object sender, EventArgs e)
    {
	//Isn't working
        Content con = new Content();
 
	con = (Content)Master.FindControl("~/Page1.aspx").FindControl("content1");
	masterplaceholder.ID = con.ContentPlaceHolderID;
    }

    protected void btnPage2_Click(object sender, EventArgs e)
    {
    }

    protected void btnPage3_Click(object sender, EventArgs e)
    {        
    }

    protected void btnPage4_Click(object sender, EventArgs e)
    {
    }

    protected void btnPage5_Click(object sender, EventArgs e)
    {
    }

}

And here's the corresponding .aspx files. I only need to show one, as there's nothing in them at the moment except some text to display which page is loading. The .aspx files are named "Page1.aspx" to "Page5.aspx", and the content ID "content1" to "content5".

page1-5.aspx

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" 
    AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1" %>

<asp:Content ID="content1" ContentPlaceHolderID="mainContent" runat="server">
    <h1>PAGE 1</h1>
</asp:Content>

So what I want to do is fairly simple. I want the content of Page#.aspx loaded into the MainContent placeholder section of the Master Page when one of the buttons in the Master Header is clicked.

is the master file different for all the pages or is the same as have given in u r question

is the master file different for all the pages or is the same as have given in u r question

It's the same, I only have one master file right now. One master file to many pages, I thought that's how it worked?

What you are doing should be referenced by each page. When you click a button in your header, which is located on your master page, you want the main content to change? This is done by just going to the next page, that references the master. This is still a server call, so it's exactly as going to the next page.

So, with your master page created, now create your content pages that just reference the contentplaceholder from the master, which is the control asp:Content.

It is it's own separate page, so you just go from page to page, where your master will stay the same. You do this by setting your master in the head line of the document. It will do the rest itself.

However, with your header as a contentplaceholder, if you have buttons in them, you will have to either copy the code to each page you use with that header, or just make the header part of the master page.

Does this make sense?

The master page will be standard among all pages that reference it, or among all pages if hard-coded into the web.config file.

Then each page that has the content just references the master page and only place the content in the appropriate content holders. Any code not within those holders will throw an error.

hi,
there is no need to load pages in contentplaceholder of master file,u cant debug master page.if u want to achieve the functioanlity ,u can create a usercontrol containing
the linkbuttons u have used in the master page.
just create a placeholder in the master page and load the user control in to it.

and debug page1.aspx.
u can achieve the functionality u want

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.