Avatar of VBBRett
VBBRett asked on

How do you programmatically lput things into the Content Place holder from C# in an ASP.net page?

Hello.  I am trying to make things load in the "Slave" contentplaceholder section in an asp.net page that has a Master page.  The content is being loaded dynamically and that is why I am doing it the way I am doing it.  Below is the code that I have in the C# section and I have not touched the html side of the page.  Please let me know how I can program things into the contentplaceholder area of the web page and not above this and above the master page.  It appears as if the pictures that I have programmed to be loaded onto the page appear above the whole master/slave page.  Please tell me how I can get things to load inside the contentplaceholder.  thanks!

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
public partial class Internationalhotbedhome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DAL.DAL intlhotbedimage = new DAL.DAL();
            DataTable dt;
            //dt = example
            dt = intlhotbedimage.IntlHotBedImageById(1);
 
            
            {                
 
                
                foreach (DataRow dr in dt.Rows)
                {
                    
                        
                        string x = (string)dr[2].ToString();
                        Response.Write("<img src=");
                        Response.Write(Server.HtmlEncode("'"));
                        Response.Write("images/hotbedintl/");
                        Response.Write(x);
                        Response.Write(Server.HtmlEncode("'"));
                        Response.Write(Server.HtmlEncode("/>"));
 
                                               
 
                        Response.Write("<br />");
                        string y = (string)dr[1].ToString();
                        Response.Write("<img src=");
                        Response.Write(Server.HtmlEncode("'"));
                        Response.Write("images/hotbedintl/");
                        Response.Write(y);
                        Response.Write(Server.HtmlEncode("'"));
                        Response.Write(Server.HtmlEncode("/>"));
                        Response.Write("<br />");                    
 
                }
                                            
     
            }
 
                                  
 
            
        }
 
    }

Open in new window

C#

Avatar of undefined
Last Comment
burakiewicz

8/22/2022 - Mon
burakiewicz

you can make the items image controls then you can add them to the contentplaceholder at runtime
ex


foreach (DataRow dr in dt.Rows)
                {
                    HtmlImage hi = new HtmlImage();
                     hi.Src = "images/hotbedintl/"+x;
                     HtmlImage hi2 = new HtmlImage();
                     hi2.Src = "images/hotbedintl/"+y;
                     contentplaceholder.Controls.Add(hi);
                     contentplaceholder.Controls.Add(hi);
 
                        
                        
                                           
 
                }
 
 
HtmlImage hi = new HtmlImage();
hi.Src = "images/image006.gif";

Open in new window

ASKER
VBBRett

Can you adapt my code to be like your code please?
burakiewicz

ok, give me a minute, and try this for both of your questions
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
VBBRett

Thank you burakiewicz!
burakiewicz

see if this displays the correct number of images, i will put the formatting in after that
protected void Page_Load(object sender, EventArgs e)
        {
           DataTable dt;
           DAL.DAL intlhotbedimage = new DAL.DAL();
 
           dt = intlhotbedimage.IntlHotBedImageById(1);
           foreach (DataRow dr in dt.Rows)
           {
               string x = (string)dr[2].ToString();
               string y = (string)dr[1].ToString();
               contentplaceholder.Controls.Add(DrawImage(x));
               contentplaceholder.Controls.Add(DrawImage(y));
           }
        }
        private HtmlImage DrawImage(string imagePath)
        {
            HtmlImage img = new HtmlImage();
            img.Src = "images/hotbedintl/" + imagePath;
            return img;
        }

Open in new window

ASKER
VBBRett

The name 'contentplaceholder' does not exist in the current context
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
VBBRett

Do I declare a variable for contentplaceholder or something?  Thjere is an error that say the following:

The name 'contentplaceholder' does not exist in the current context
burakiewicz

that is the name of the content placeholder ex... cphMain or cphSlave or contenetplaceholder1
ASKER
VBBRett

How do you name it?  Here is what is listed on my web page....

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

<asp:Content ID="Content1" ContentPlaceHolderID="PageCenterContent" Runat="Server">
<div id="ContentSelector"></div>
</asp:Content>

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
burakiewicz

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question