InstantiateIn and user controls - asp.net

I want to add my user control in InstantiateIn like this:
public void InstantiateIn(Control container)
{
PIMVisualizer pimVisualizer = new PIMVisualizer();
container.Controls.Add(pimVisualizer);
container.Controls.Add(new Button() {Text="asdf"});
}
but the control doeas not appear on the page (the button does). The control is trivial, I am just testing the approach:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="PIMVisualizer.ascx.cs" Inherits="PIMVisualizer" %><asp:Label ID="Label1" runat="server" Text="UC"></asp:Label>
but still, why does not the text "UC" appear onthe page?

You have to load UserControls, do not create them via constructor.

Related

Make control public in designer generated file

It there any way to tell the ASP.NET .designer.cs code generator to create public controls instead of protected?
.master.designer.cs file:
/// <summary>
/// btnLanguageEN control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.LinkButton btnLanguageEN;
.master file:
<asp:LinkButton runat="server" ID="btnLanguageEN" OnClick="btnLanguageEN_Click" Text="ENG" />
Why I need it? I am trying to access to master page controls from child page:
.aspx file:
<%# MasterType TypeName="www.MainMaster" %>
.aspx.cs file:
Master.btnLanguageEN.Text = "123";
Here I receive an error stating that btnLanguageEN is protected.
Rather than modify the designer generated code, reference the control through the Page's Master property using the FindControl method.
You'll need to add the following directive to the top of your child page (if it's not already there) to reference the master page:
<%# MasterType virtualpath="~/Masters/Master1.master" %>
Then, in your child page's code behind, you can do things like the following.
void Page_Load()
{
// Gets a reference to a LinkButton on the master mage.
// a ContentPlaceHolder
LinkButton mpLinkButton = (LinkButton) Master.FindControl("btnLanguageEN");
if(mpLinkButton != null)
{
mpLinkButton.Text = "123 - Labelled by Child Page";
}
}
Reference: http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx
Create a strongly typed reference to your master page by including this on your ASPX page.
<%# MasterType virtualpath="~/Masters/Master1.master" %>
On your Master Page, create a property. This will make the control available from other classes..
public Button btnLanguageENMaster
{
get {return btnLanguageEN;}
private set;
}
Then from your regular page code behind you can do this...
Master.btnLanguageENMaster.Text = "Hello, world!"

Templated RadDock control inside of a RadDockZone?

I've created a templated ASP.NET user control based on the RadDock control. However, when adding such a control to a RadDockZone causes a runtime error stating that it can only contain RadDock controls. Is there any way to solve this?
Templated user control markup
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TemplatedDock.ascx.cs" Inherits="TemplatedDock" %>
<telerik:RadDock ID="RadDock1" runat="server" EnableAnimation="True" DockHandle="Grip" Resizable="True">
<ContentTemplate>
<asp:PlaceHolder ID="dockPlaceholder" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</telerik:RadDock>
Templated user control code-behind
public partial class TemplatedDock : System.Web.UI.UserControl
{
private ITemplate _content;
[TemplateContainer(typeof(ContentContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content
{
get
{
return _content;
}
set
{
_content = value;
}
}
void Page_Init()
{
if (_content != null)
{
ContentContainer container = new ContentContainer();
_content.InstantiateIn(container);
dockPlaceholder.Controls.Add(container);
}
}
}
public class ContentContainer : Control, INamingContainer{}
}
Usage in RadDockZone
<telerik:RadDockZone ID="RadDockZone1" runat="server">
<a:TemplatedDock>
<Content>
<telerik:RadGrid ID="someGrid" runat="server"></telerik:RadGrid>
</Content>
</a:TemplatedDock>
</telerik:RadDockZone>
There is no way for this to happen. A RadDockZone must have only RadDock controls as children. Custom controls have a different type and will, therefore throw an exception.
The zone is tightly coupled with the dock to offer easy integration like drag-drop, state saving, etc., and this has its price.

Can not access control from nested master page in content page

I am using nested master pages where i want to use Label control from nested master page and update its text. but it is not accessing. When i removed outer master page then it is working fine. Following is the markup and code.
OUTER MASTER
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Roster.Site" %>
NESTED MASTER
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="RoasterMaster.master.cs" Inherits="Roster.RoasterMaster" MasterPageFile="~/Site.Master" %>
<%# MasterType VirtualPath ="~/Site.Master" %>
CONTENT PAGE
<%# Page Language="C#" AutoEventWireup="true" Inherits="RequestsView" CodeBehind="ViewRequestsByPM.aspx.cs" MasterPageFile ="~/Roaster/RoasterMaster.Master" Title ="Roaster- View Requests by PM" %>
<%# MasterType VirtualPath ="~/Roaster/RoasterMaster.Master" %>
CONTENT PAGE CODE
protected void Page_Load(object sender, EventArgs e)
{
Label lblTitle = new Label();
lblTitle =(Label)Master.FindControl("lblTitle");
lblTitle.Text = "View Roaster Request";
}
What is going wrong with the implementation. Please help. Thanks
You can add the below code snippet in
NESTED MASTER PAGE
public string PageTitle { get; set; } // In page_load
lblTitle.Text = PageTitle;
CONTENT PAGE CODE
this.Master.PageTitle = "YOUR TEXT";
This will work for you...
Assuming that your label is in Roster master page, you can simply add method to set the text in master page code behind. For example,
in RoasterMaster.master.cs
public void SetTitle(string value)
{
this.lblTitle = value;
}
And in content page code
Master.SetTitle("View Roaster Request");
In case, your label is in outer master then you can similarly forward the call to the outer master from roster master code.
EDIT
Your code does not work in nested master case scenarios because Master page contents get added within page control hierarchy with different naming container. FindControl method does not span multiple naming containers which is the case here - because of nesting you have nested naming containers. Page.Master would give you outer naming container but your label might be lying in inner naming container. One of the way, is to write your own find control implementation that will recurs within the control tree but really it does not make sense - I would rather use above code which is more efficient and more importantly better maintainable.

How to put one user control into another user control

suppose i have two user control. usercontrolA and usercontrolB. how to include usercontrolB into usercontrolA. also please tell me how to read controls value of usercontrolB from usercontrolA and also from page. looking for details discussion. thanks
May be below can help you
In Control A
<%# Register Src="~/UserControls/ControlB.ascx" TagName="ControlB" TagPrefix="uc" %>
<uc:ControlB ID="myControlB" runat="server" />
In Control A.ascx.cs
string userName=myControlB.UserNameTextBox.Text;
In User ControlB.ascx.cs have some properties like
public TextBox UserNameTextBox
{
get {return this.txtUserName;}
}

Access child user control's property in parent user control

I have included a user control in another statically following code :
place the folowing directive in the asp code of the parent page or
usercontrol:
<%# Register src="Name_of_your_child_control.ascx"
tagname="Name_of_your_child_control" tagprefix="uc1" %>
use the following tag in the asp-code of the parent page/control:
<uc1:Name_of_your_child_control ID="Name_of_your_child_control1"
runat="server" />
.....
But the issue is...i am not able to access the public properties of user control which got included(child user control) in given user control(parent user control)...
Please help :(
Say your usercontrol was this:
<%# Control Inherits="Project.MyControl" Codebehind="MyControl.ascx.cs" %>
<asp:TextBox ID="TB" runat="server" />
Your control code-behind:
namespace Project
{
public partial class MyControl : UserControl
{
public string MyTextProperty
{
get { return TB.Text; }
set { TB.Text = value; }
}
}
}
In your parent page that included the control, like this:
<%# Register src="~/MyControl.ascx" tagname="MyControl" tagprefix="uc1" %>
<uc1:MyControl ID="MyControlID" runat="server" />
You can use that property in code:
MyControlID.MyTextProperty = "bob";
Using
Name_of_your_child_control1.PublicPropertyName
must work for your parent user control.
Check the path and file names you are using, Anish. You have something wrong. Is Visual Studio telling you it can't find the control? Is it failing at compile time? Runtime?
It's funny but whenever you add a property to a user control.
You need to register it again in the parent. So in your case,
Add a space at the end of this line and remove it again:
$<% Register src="~/MyControl.ascx" tagname="MyControl" tagprefix="uc1" %>
This will re - register the user control and you will be able to access new properties.

Resources