 |
|
 |
Hi, GREAT article btw I really am glad you helped me do this, great job. So I have dynamic master pages with dynamic content being loaded from DB and same with regular pages too, but I am having strange problems seeing as some of my ajax'd controls are relying on things happening in the normal page life cycle order. It seems that some of my CreateChildControls functions are happening at strange times and I narrowed it down to my InstantiateIn in the Master page's Init function. The problem being that sometimes the Page reference is null and some of my other controls have problems too. Any help would be really appreciated. Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks for this article,
Perhaps you can help me. I Have a ContentPlaceHolder in a Template of my customcontrol. It works fine if i load a static page in it but not if i have a page with controls on it. Any hints are welcome.
<cc1:MyControl > .... <ItemHeaderTemplate> <asp:ContentPlaceHolder ID="MMContentPlaceHolder" runat="server" /> </ItemHeaderTemplate> .... </cc1:MyControl >
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I've rewritten your code (which is brilliant, by the way), added it to an existing project, and everything works great. I recently added a static ContentPlaceHolder to the Master page in addition to the PlaceHolder which your code uses (PlaceHolder1 is your name for it). The code (in VB):
Dim itemp = MyBase.ContentTemplates("ContentPlaceHolder" + i.ToString())
returns nothing as the ContentPlaceHolder cannot be found in the list. I've stepped through the code and the ContentTemplates list displays both the static ContentPlaceHolder (defined in the markup) and the dynamic ContentPlaceHolders created with your code ("ContentPlaceHolder1"). It is clearly in there but they cannot be retrieved and I am stumped.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
The only things I can think of is that you tried with either an empty ContentPlaceHolder or with no corresponding Content in the Page, because it works just fine for me. If this doesn't help, maybe you should post or send me some code to see if I can help you (I'll try to be a bit faster this time). Also thank you for your kind words.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thank you for the reply. I went back over the code and I was missing the CompiledTemplateBuilder line of code for the ContentPlaceHolder that wasn't working.
That is what happens when you stare at your code for too long... you miss the obvious!
-E
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I am creating a master page and then adding a content page to it. when i place controls(tags) like update panel or table or textbox or label in the content page(within the content tags) i get a red line under all the tags in that page. when i move my mouse over it, it says "Element UpdatePanel is not a known element This can occur if there is a compilation error in the website" . I have tried all possible alterations but in vain. please help me out. many thanks
balaji
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Your problem has to do with ASP.NET AJAX and not master pages. You probably need to add some sections to your web.config file. This is best achieved by dragging and dropping the UpdatePanel from the toolbar instead of editing the page by hand, so your web.config gets updated. Also this way the Register directive is added, if needed.
|
| Sign In·View Thread·PermaLink | 3.00/5 |
|
|
|
 |
|
 |
I did try that option too. Still the same problemo. All my friends are getting the same result. I am working with AJAX-Enabled website in VS 2005. Would it be a problem coz of this. Any solution to this ???
Thank u
balaji
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hey i have kind of solved the problem. i edited my web.config file. changed the to and replaced all the ajax controls in my page which had <asp:..> to and it worked out. hurray. thank u very much for the help.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
I've read other messages stated here, but I have been lost a bit There is a ContentPlaceHolder which resides in a Master page. I want to fill that ContentPlaceHolder from a page that inherits the master page. Actually, I want to fill it by html code. I mean, initially, it is empty, but from the inherited page, it will contain a table, a button or etc, which are invoked with an html code dynamically from the page.
When I try to do with Response.Write() it puts the html code at the top of the page, not in the contentPlaceHolder.
Can you help me?
Regards...
Gorkem
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Use the LiteralControl class. In the method you specify in for the Content (in my example the Build method), add it to the Controls collection to the Control that gets passed. Because of the simplicity of the task, you might want to consider using an anonymous delegate, like this:
string htmlString = ""; base.AddContentTemplate("ContentPlaceHolder1", new CompiledTemplateBuilder(new BuildTemplateMethod(delegate(Control c){ c.Controls.Add(new LiteralControl(htmlString)); } )));
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |
|
 |
Is there a VB example of your demo? I tried it but I got different results, and not good results, errors in fact.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
There is now. I updated the article with a working VB.NET demo project. Don't expect too much, for this is my first "project" in this language .
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi there, am really impressed with what you have done in this article and wondered if I could pick your brain.
I am trying to create a system where I can populate the ContentPlaceHolders of a page dynamically from a database, based on a querystring value. So I was thinking that the best way to do this would be to do most of the work in Page_PreInit, to get the content and the names of the placeholders (that already exist in the masterPage).
Do you think this is possible? Any advice or tips would be greatly appreciated.
Thanks dave
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
DaveWarren wrote: ...in Page_PreInit, to get the content and the names of the placeholders (that already exist in the masterPage). ...
If you look at the first post below (entitled "Late bound master pages"), my answer to that question contains the answer to yours as well. The problem is, the Page_PreInit event occurs before the master page is created and therefore no placeholders exist yet (as opposed to what you wrote). So in order to add the content correctly, you have to know the number and IDs of the ContentPlaceHolders in advance. If you set the MasterPageFile to let's say "Master1.master", you should know about it's ContentPlaceHolder controls. In case it's a static master page, just have a look at the markup for the IDs, if it's dynamic, make sure the master page and the page that uses it are synchronized. There's no way to get information about the master page dynamically in the PreInit phase, and after that it's already to late. To sum it up, you either have to fill up the ContentPlaceHolders with their IDs hard-coded in the page code or create them also using database data.
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Thanks for the fast reply!
So far I have managed to get the page to create the placeholders from the data in the database, but I am having trouble working out how to get the build method to display the correct content for those placeholders.
This code appears in Page_PreInit
foreach (DataSet1.DataTable1Row contentRow in content2) { base.AddContentTemplate(contentRow.cph_name, new CompiledTemplateBuilder(new BuildTemplateMethod(this.Build))); }
And I can't see how to pass the content to the build method.
I have tried to pass build a string with the content in but haven't managed to get it to work.
Any more ideas greatly appreciated
Thanks again
Dave
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I'm not quite sure what your content really is, but my anser is based on the assumption that it's some string of html code and that it is also stored in the DataSet you retrieve. In this case you just have to find which row you actually need. Though in this example the same Build method gets invoked, the parameter is different each time, because everytime the corresponding ContentPlaceHolder gets passed. Which has the same ID as the Content created in the PreInit phase. Which is stored in the cph_name column of each row. So by using c.ID you can identify the correct row, retrieve the content part and assign it to let's say a LiteralControl.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Good article. Very informative.
I've got a slightly different problem though. I want to write a master page that will load it's markup from another source, add a contentplaceholder to it and then merge with the content page. Let's say I've got, in my master page, a string containing the HTML for a web page. I parse through it and insert the string for a asp:contentplaceholder control where I want it. Now I want to throw away the HTML that would actually come out of the master page and use my string instead. Is that possible?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Yes it is possible. You only need to override the Render method of the master page. Here you either need to use the HtmlTextWriter's Write method, or the RenderControl method of controls. If you need to render parts of your HTML string inside a control (for example div), just use a LiteralControl. So a minimalistic example would be: protected override void Render(HtmlTextWriter writer) { writer.Write("<html><body>"); ContentPlaceHolder1.RenderControl(writer); writer.Write("</body></html>"); }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
You, my friend, are a genius. I've been staring at this problem all day and I didn't see that.
Cheers
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
First off, I really enjoyed your article. There isn't a great deal of content with regard to working with master pages in a more advanced way. So, I was hoping that your experience might be able to help me with something.
What I am trying to do is set the master page programatically (easy enough), but with a caveat. Say I have 2 master pages:
Master1: mainCPH secondaryCPH
Master2: mainCPH
And I have the following pages...
Page1: mainCPH content
Page2: mainCPH content secondaryCPH content
What I want to do is be able to set the master page for Page2 to Master2, and have the secondaryCPH content area omitted from the rendering. Currently, the page errors out when you try to render it so I am a little stuck. Any ideas on how I could accomplish this?
Aaron
|
| Sign In·View Thread·PermaLink | 1.50/5 |
|
|
|
 |