In the child page, I want to find master page's Content placeholder and add text to it. How do I make that possible?

StackOverflow https://stackoverflow.com/questions/4499146

Question

I tried this much:-

protected void Page_PreInit(object sender, EventArgs e)
  {
        class1 obj = new class1();
        DataTable dt = new DataTable();
        dt = obj.get_text();
        ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1"); 
        ContentPlaceHolder1. ????


 }
Was it helpful?

Solution

Given you have a valid reference to ContentPlaceHolder1...

In this line:

ContentPlaceHolder1. ????

Do this:

// Add text to the place holder.
ContentPlaceHolder1.Controls.Add(new LiteralControl("my text to insert"));

OTHER TIPS

You cannot add text to a content placeholder. Since it's in a masterpage, you have to add the text to a control on the page you're loading. If you want the text to appear on the master page, you want to add a label or literal outside the content placeholder on the master page and access it the same way you access the content placeholder in your example.

However, given your example, you don't need to do that. You can just set the text on the page you're viewing.

contentPlaceholder1.Controls.Add();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top