Creating web controls inside a content placeholder programmatically - c#

I've got a master page and a content page. How can I add controls to the master page's content placeholders programmatically?

Try something like following:
this.Master.Controls.FindControl("ContentPlaceHolderID").Controls.Add(yourControl);

this.Page.Master.FindControl
As said above would work fine.
Alternatively, make whatever placeholder in your master page a ContentPlaceHolder, and then your child pages can put stuff there directly without having to go up through the Master page.

Related

How do I use my HTML page inside my ASP.NET page?

I have a .html.
I also have a .aspx that have two panels. I want to display my .html page between the panels, how can I do that?
I would like my .aspx page to end up with following structure. If I just drag the .html file in, it only creates a hyperlink. Should I put the .html code into a asp web forms instead?
...
<asp.Panel ...>
</asp.Panel>
.html
<asp.Panel ...>
</asp.Panel>
...
If you want it in the same page, you could just paste the contents of the html body between your panels.
If your html is going to change in future, and you want it to remain a different page, use an iframe and set it to display your html page.
If you want to combine both on the fly, you can use a asp.net Literal control, read the contents of your html page and set the contents of the html to the Text property of Literal
You can use iframe to display html page.
Use like this:
<iframe width="100%" height="100%" src="a.html" />
Convert your html page as user controls. Then include that user controls in aspx page.
Details:
1. Create a user control. ( .ascs extention file ).
2. Place html code inside user control.
3. Create asp page.
4. Include user control in asp page via following code.
<%# Register src="control.ascx" tagname="controlName" tagprefix="pre" %>
<pre:controlName ID="controlId" runat="server" />

Problem With Custom Control on Page Using a MasterPage

I have a content page which has a related master page.
I register a prefix <%# TagPrefix ..... and can load other custom controls at that namespace.
However, one particualar control at the same namespace, when added to the aspx page, breaks it.
The control in question inherits from asp:Panel, has a parameterless constructor, defines a few public accessors, and creates some standard child controls, and nothing much else.
Are there some fundamental restrictions to creating custom asp controls that I am breaking unknowingly?
Add the control back to the page. Delete the designer file for the page: .aspx.designer.cs
Then right click on the page and select Convert to Web Application. This should give you the actual error the page has when attempting to write the control definition to your designer file.
I suspect there is a compilation error in your custom control.
My control was attempting to access Page.Header which was null as the master page had not marked the tag with runat="server".
I guess that is a fundamental restriction that I was looking for...

Remove statically added controls at runtime

The Scenario: I have an asp.net website where I show a div popup on page load for taking a few user details. When a user inputs the details, or closes the popup, I set up a flag cookie so that the popup is not displayed again for the user. The div is in the MasterPage so that it is displayed no matter on which page a user lands first time. The div contains an UpdatePanel which has all the controls required for taking the details. This whole functionality is working fine.
The Problem: Now this div popup is not showing(by setting display:none) on subsequent postbacks(which I want), but the html markup is still loading with the page unnecessarily adding to the page size. What I would idealy want to do is: Check if flag cookie is set. If no, show the popup, else remove the popup's markup from the page.
Now since the div is not a server control, I cannot possibly remove it and the all the controls inside it. So, I thought of removing the UpdatePanel from the page:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["flag"] != null)
{
if (Page.Controls.Contains(updpnl_contact))
{
Page.Controls.Remove(updpnl_contact);
updpnl_contact.Dispose();
}
}
}
But I guess this tends to work with dynamically added controls only, and since the control is added at Design Time, it is not being removed.
Is there any way I can achieve this?
If you add a runat="server" attribute to your <div> element, it will be available in the code-behind. You'll need an id on it as well. Then you can just toggle the Visible property. If this property is false, the control won't be rendered to the client (i.e. no HTML markup).
What you're trying to do is not at all the usual workflow. I tend to think that it will not work as it would mess up control tree, maybe even corrupt the viewstate and so on.
As a possible solution, you can put it's visibility to hidden in the code behind. This, in the contrary to the usual 'gut feeling', doesn't work like the css propery 'display:none' for example - instead the control will not even be rendered into the page when it's not visible. This may be the workaround for you.
Happy coding.
A more efficient approach would be to create the panel as a UserControl and load it dynamically in codebehind when it's needed, then add it to your page. E.g, in code:
MyPopupControl popup = (MyPopupControl)Page.LoadControl("/path/to/usercontrol.ascx");
PopupPanel.Controls.Add(popup);
Where PopupPanel is an empty <asp:Panel>. Then, not even the markup will need to be loaded/processed except when its needed.
There is no reason that all the code you use to display and process this panel couldn't also be in the usercontrol, isolating it from the master page.
Can you build the panel dynamically, based on the cookie setting?

Can I know what page is in my master page's ContentPlaceHolder?

All the Google finds I ran into tell me how to use FindControl to access a control on the master from the content page itself.
However, what I'm trying to do is the opposite.
From the master page, I want to reference whichever child page is in the ContentPlaceHolder.
Why you ask.
I want the master page to know which tab should be active depending on the content Page currently in the placeholder.
This lets me avoid having each page to reference the master page and allow them to change the active tab; that should be the master page's job (if there's a way it can know whom it's enclosing).
Thanks. No rants please.
If you are looking to get the instance of the executing page class, you can retrieve it from the current HTTP context:
var page = HttpContext.Current.CurrentHandler as Page;
From there, you can navigate the page's control tree, call FindControl(), and so on. Be cautious about page lifecycle, though, as master page events tend to fire before their page event counterparts.

Masterpage + updatepanel on child page

So I have a masterpage with a login that is in an update panel. I have a child page that has a literal control that should update when the login updates. What it doesn't do is reload the method I use to generate the content for that literal when it posts back. I tried to call the method on the child page from the master page once you click log in, but I get an error that the literal control cannot be found (because it exists on the child page not the master page). How would I reference that control in the masterpage to pass it to my method?
The article below shows how the control tree works with MasterPages and how to reference different controls at different levels of the control tree.
ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
So the scenario is that you have an update panel on the child page that when triggered doesn't update/refresh your lets say, label which is in your header on your master page.
What you do is, in the code behind of your master page create a function that changes the value of the label.
Include an update panel on the master page for the label, which is triggered by the textchanged event etc.
Now, in your child page code behind or your let say, button click event, call upon the function that exists in the master page and send the needed value in the parenthesis.
C#:
((MyMaster)this.Page.Master).ShowMessage(text);
VB.NET:
DirectCast(Me.Page.Master, MyMaster).ShowMessage(text)
This should update the label with the correct value whilst triggering the update panel on the master page and thus refreshing your label as well.
I'm about to try this now for myself, wish me luck. :D

Categories