I have a problem with dynamically loading control's into master page's ContentPlaceHolder.
Name of the ContentPlaceHolder and path of the UserControl is loaded from db and send to this little code:
Control c = this.Page.LoadControl(uc-path-from-db);
this.Page.Master.FindControl(cph-name-from-db).Controls.Add(c);
When I run it, I get this error:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
What am I doing wrong?
Its not totally clear without your markup, but it is telling you that you can't Add the control in there because some of your markup contains a <% ... %> block which tells ASP.NET to execute whatever is in there to create your markup. Try removing your <% ... %> block and it should let you Add the control. Or post your markup for a more complete answer.
Depending on the type of Web Project you're working with, you have to be sure to <%# Register%> your UserControl on both the MasterPage you're loading it to, and the Page you're loading it from. If you're working with a web site and not a web application it compiles each page into it's on little assembly and won't reference the usercontrol's assembly unless it sees that Reference tag in the markup.
I'm not 100% on this, but it's worth a look.
Related
I have a user control, say control 1, that looks for control2, which is placed on the root master page : Site.Master
And this is how I am getting Control2 from Control 1 now,
MasterPage showMaster = this.Page.Master.Master;
MasterPage siteMaster = showMaster.Master;
Control2= siteMaster.FindControl("Control2");
The above code works fine. But because our application uses nested master pages, I am running into a bit of situation here.
How do I find control 2 dynamically regardless of where I put Control1 in which template? Becasue right now, depends on where I put Control1, and how nested is that template in relation to the Site.Master, I have to change how far up in the chain I get Site.Master in the Control 1 code.
Any good ideas on how I can avoid doing that?
Please advise.
i found a work around with recursively loading the control
I'm trying to construct a custom control for ASP.NET
I started by creating a Web Application in VS2010 and creating a new .ascx page. The page is called "TestBox" and it's just a single TextBox control with "This is a test" as the text.
I built the project and then included the DLL in another website in order to make sure I would be able to move controls. Based on a tutorial I found here I added the following line of code to the top of the page:
<%# Register TagPrefix="TestControl" Namespace="TestControl" Assembly="TestControl" %>
Then I added this to the page itself:
<TestControl:TestBox ID="TestBox1" runat="server" />
The code compiles and the page loads without throwing up any errors, but when it loads it's completely blank. By introducing a deliberate runtime error, I determined that the TextBox is definitely being loaded, but the control itself still isn't showing up.
Am I missing something?
Code for the TestControl:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestBox.ascx.cs" Inherits="TestControl.TestBox" %>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged">This is a test</asp:TextBox>
I haven't touched the Designer code or the .cs code in any way.
EDIT: Figured it out. I had declared a namespace for the .CS file but not the .ASPX file itself.
The answer was that I had to add a namespace to the ASPX file itself and not just the underlying code file. I forgot to add Class="TestControl.TestBox" to the page declaration.
I have been building a custom control for some time now and overcome a number of hurdles. One challenge I have yet to resolve is the ability to use a custom control more than once on the same page.
I have a custom control that functions well on its own, but when two of the same controls are placed on the page the second control is able to control the first one. My guess is that the first one (control) is the first object and the second one is the same object. How can I make sure in the code that if I use the same control more than once on a page it will behave as two separate controls. Are there any specific things I should look at to make sure it allows it to be on a page more than once.
Thanks in advance.
When you add multiple instances of a control, be sure to give them different IDs. Then when writing any code that will interact with them, reference them by that ID.
<%# Register Src="controls/myControl.ascx" TagName="myControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainPlaceHolder" runat="server">
<uc1:myControl ID="ctlFirst" runat="server">
<uc1:myControl ID="ctlSecond" runat="server">
</asp:Content>
Then in the code behind:
ctlFirst.SomeProperty = true;
ctlSecond.SomeProperty = false;
Place a couple of instances of your custom control onto an ASPX page then view the HTML source and have a look at all the element IDs generated on each of the control instances. ASP.NET will automatically mangle the IDs of your control's children, prefixing them with the ID of the parent control. If you're outputting raw HTML, this might not happen. If there are any duplicate IDs, then that may be the cause of your problem, particularly if you're using client-side logic to manipulate the controls on the page.
Also, make sure that you're not using any session or application variables in your controls.
I am writing a webpart and was trying to update the browser title... so, I went into mywebpart.ascx added the following:
<asp:Content ID="contentPageTitle" ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
<%= SPContext.Current.Site.OpenWeb().Title %>
</asp:Content>
I then got this error:
Content controls have to be top-level controls in a content page or a nested master page that references a master page.
So, I am trying to do it programatically in mywebpart.cs by doing:
Content content = new Content();
content.ContentPlaceHolderID = "PlaceHolderPageTitle";
I now need to input this piece: SPContext.Current.Site.OpenWeb().Title
What property in the Content control allows me to do that? If there is a better way to do this, I am open as well. Thanks.
Unfortunately, you cannot place a content control within a user control. As the error message indicates, content controls must be top level controls in a page or master page, and cannot belong in any other kind of control.
An alternate approach might be to customize your page layout or master page to contain the logic you want to provide.
If you have some assurance of the ID of the content control (not the ContentPlaceholderID, mind you), then you can interact with the content control like so:
var content = Page.FindControl("contentPageTitle");
content.Controls.Add(new LiteralControl("Hello, World!"));
--
On an aside, do ensure that any SPWeb opened with OpenWeb() gets disposed properly, or you may face memory management problems down the road.
From what I've already read this appears to be impossible, but I wanted to see if anyone out there has a secret trick up their sleeve or at least a definitive "no".
Supposedly a master page is really just a control for a content page to use, not actually the "master" of a content page. If I wanted to go from one content page, to another content page with the same master page, I would just say
Response.Redirect("PageB.aspx");
But this would immediately cause a postback, flickering the page, which is the crappy pre-ajax way of doing things.
In this current project, I'm trying to see if I could figure out how to change the current content page of a ContentPlaceHolder in the master page asynchronously, when a button is clicked on the master page.
Is this possible, if so how?
I don't know if you can between pages (.aspx) but it can definitely be done using UserControls.
ASP.Net pages each have their own URL so what you're trying to do is to go from one URL to another without any postback, that's just not how it's supposed to work.
Using user controls (.ascx):
Create a page that uses the MasterPage and use something like this in the content
<ajax:UpdatePanel ...>
<ContentTemplate>
<asp:PlaceHolder ...>
</ContentTemplate>
</ajax:UpdatePanel>
Search for UpdatePanel and tweak its settings to do what you want, then learn how to swap user controls in a placeholder.
No, you cannot because a master page is actually a control rendered on a particular aspx page, rather than actually containing the aspx page as it deceptively appears to be programmatically and in design view.
More Info:
You could however use a variety of other controls to simulate this effect. The asp:MultiView control is one example, each "page" could be made in a single view and placed in an update panel, thus allowing it to be switched asynchronously. Alternatively you could define each page in a separate user control and put those in an update panel, asynchronously switching the visible property on those controls as needed.
There are really a lot of different ways to achieve an effect similar to changing the master page's content placeholder.