I have one problem. I want to setup master pages in my application. As I have too many aspx pages, I don't want to go to everypage and add asp:content tag in it. I want some dynamic way in which I can look into my page, copying it's html/other asp controls and adding it to dynamically created content template.
I know how to dynamically create content templates. But don't know whether it is possible copying aspx page's html and adding it to content template.
Let's say, In my aspx page's codebehind, on page_PreInit, I am adding following code.
base.AddContentTemplate("WebHeaderPH", new CompiledTemplateBuilder(new BuildTemplateMethod(this.getControl)));
getControl is a method which contain anything (html, text or may load a user control.)
This way, I am adding a content template dynamically.
Now my aspx page contents following code.
<span id="someid" runat="server">blah blah...</span>
Now what I want is, to get this span tag by it's id, and wrap it under asp:content. or may be in the above getControl method.
but putting this span on page, throw following error,
Content controls have to be top-level controls in a content page or a nested master page that references a master page.
Either I have to put, asp:content template on the page. but If I do this, then I don't need to create contentTemplate dynamically or keep whole page blank as I am creating contentTemplate dynamically. In both cases, I have to edit all my aspx pages.
Is there a way to get page's html and add it into contentTemplate without any need to edit aspx page?
I'm afraid you have to go step by step manually. It is indeed a design mistake, but it won't take you THAT long to change them all.
I've unfortunately made the same mistake as well and I fixed it by manually editing my pages.
Related
I'm working on a .NET WebForm app which has a master page. The .NET can not recognize many elements in the aspx file. For example it say Label is not a valid asp element and then says most likely reason is a malformed web.config file.
I double check my web.config and all looks good. The other webpages do not have same issue. The interesting thing is I do not have this issue inside the MasterPage.
I also tried recreate the page from scratch but still having same issue.
I'm using VS2019. Framework 4.6
You have to provide some sample code and markup. Also, if you have a master page, then that is a huge deal also. As a general rule, asp.net controls you drop on the page are able to be used from code behind. However, the code for the standard page(s) that we use as a "child" of a master pages means that code behind for the master page can easy use controls in master page. And code behind for the page being displayed in that master page ALSO can freely use its own controls. But controls between the master page and the working child page is VERY different matter.
And of course controls dropped into a repeater, or say listview (or even gridview) means that the one label or text box control is automatic repeated over and over. As such, you have to pull/get/use the one row out of that data bound repeating control, and then from that one repeating row grab the control in question.
So, saying I can't start my car, or I can't use or get a control?
We need more information as to the context of what control, where it is (in the master or child), and is the control perhaps nested inside of a data bound repeater, listview, gridview etc.
so, edit your question - add some details as to the markup, where it is (master or the child page), and we can help.
So, as a general rule, code behind in master page is free to use controls in the master page.
And in the web page you created, once again code behind is free to use controls in that page.
It can be more difficult to say have code in master page, and have it reference controls in the child page that is being displayed. But, then again, it is VERY rare that code in master page would need to reference or play with controls in the child page, since a master page will (usually) just be your main navigation bar - and it will be the same for many if not all pages you display - hence you master page really can't know what controls will exist in the current child page being displayed.
I have a Master Page and a Content Page. I have Check In Check Out small form in Master Page and I have a Contact form in Content page. But while running the website, I am getting the error which says:A page can have only one server side form tag. When I remove the form tag from the Content Page. Then,it says the Textbox should be placed within a form tag. But I want both the forms,since both are necessary. So please help me so that I can have both the forms.
There can be only one <form> tag on the whole page, that is just how webforms work. In your case it is in the master page. The pages using the Master do not and should not have <form> tags.
But from your question it seems that you think you are using master pages, but are actually not. See this page as to how the .aspx pages are supposed to look with master and child pages.
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/master-pages/creating-a-site-wide-layout-using-master-pages-cs
It is not possible to add more than one form in a single aspx file. Otherwise try to use iframe in that page.Inside the iframe we can use another aspx page.
I have two web-form pages in ASP, one for viewing a record and and another for editing it. I would like to have a link on each which popups a box to allow editing a certain property on the record. If I only placed it on the Edit page, I would use an UpdatePanel but I would have to copy and paste code if I wanted it on the View page as well. Is there a way to have the code in one area which be can be referenced and used from both pages?
Edit: I know I could make a custom control to handle it although it would include all its HTML on the page when the page first loads, not when the link is clicked.
Edit2: I wanted to avoid having the contents of the popup rendered when the page first loads but now I realize it's just a matter of not binding any data in it until the Postback occurs.
You can use a MasterPage for both the pages, and put the popup with UpdatePanel on the master page, so you can access it from both the pages.
Hope it helps.
Don't forget to upvote it if it solves you problem.
Thanks.. :)
Make the bulk of the panel a user control (ascx). Then just put a thin wrapper around it on both the pages where you need it. The bulk of the code is then in the ascx and ascx.vb and does not need to be written twice.
I have a masterpage and content page. And I'm trying to run a java script that needs to be executed on the page loading.
As I am using a master page do not have access to the field
My doubt is how to run the script within the content page? And where the script has to be? the head of the master page or inside the content page?
You can run C#/VB code on page load at any page or custom user control, so you will have access to whatever on the page.
I think you want to put that code in masterpage to standardize the procedure, one of the solutions (I don't know if you can accomblish this):
you can build your own "CustomPage" class by inheriting from "Page"
so, you can set the onLoadEvent to a preset action.
and inherit from it for all your pages.
You could put a ContentPlaceholder in the Head section of your master page. Thats if you want to put your script in the Head
I have a master page(MyMasterPage.master) with two content place holders. I wanna show another aspx page(MyHeader.aspx) in the 1st content place holder of a content page(MyContentPage.aspx) that uses my master page.
You should be using user controls for reusable components of a pages. Create a Header.ascx file instead of a .aspx. You can then drag that into your ContentPlaceHolder from the solution explorer when in design mode.
http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx
I would use UserControls. MyHeader.ascx.
I echo the prior commenter's suggestion to use a usercontrol if possible.
If you really want to keep your existing aspx page; you might want to take a look at using an IFrame to accomplish this.