Access child user control's property in parent user control - asp.net

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.

Related

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;}
}

How to register custom server control on ASP.NET page

I have a project and I am trying to register a custom server control (there is no .ascx file) on the page. I am currently using
Class Declaration
namespace MyApp.Controls{
public class CustomControl: WebControl{
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
}
}
On my page,
<%# Register TagPrefix="myControls" Namespace="MyApp.Controls" %>
<myControls:CustomControl runat="server" Text="What up!" />
I receive a Parser Error, with the message "Unknown server tag 'myControls:CustomControl'."
What am I doing wrong?
Well, if this control is in another class library, or even if it's in the same one, it wouldn't be a bad idea to specify control's assembly in #Register:
<%# Register TagPrefix="myControls" Namespace="MyApp.Controls" Assembly="MyApp" %>
<myControls:CustomControl runat="server" Text="What's up!" />
Clean and rebuild your solution too in order to verify everything is compiled rightly!
If your control will be reused on several pages, you may want to register it in web.config, as one of system.web/pages/controls subelements instead of copy-pasting the same <#Register tag in all affected pages.
web.config:
<system.web>
<pages ...>
<controls>
...
<add tagPrefix="myCompany" namespace="MyCompany.Whatever.Controls" assembly="Whatever"/>
</controls>
thepage.aspx:
<myCompany:ControlClassName ID="TheStuff" runat="server" ... />
You should put your control either under the App_Code folder (in the case if the control not in assembly) or add a reference to assembly where this control is:
<%# Register TagPrefix="myControls" Namespace="MyApp.Controls"
Assembly="SomeAssembly" %>
But guessing, your control not under the App_Code folder.
Add an assembly attribute to your register tag

InstantiateIn and user controls

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.

All controls are still null after calling EnsureChildControls()?

The way this page is laid out, all of the data is loaded at Page_Init. Well, I have a custom control that is having problems with this though.
I have it on the page like so:
<cc:SomeControl... />
And then I set the value at Page_Init using
MyControl.Value="blah";
Simple stuff..
The Value is an accessor and has something similar to this:
public string Value{
get...
set{
EnsureChildControls();
MyHiddenField.Value=value;
}
}
and it is here that I have a problem. It says that MyHiddenField is null. Is Page_Init just too early for this? Or is there some other function I need to call?
The fix for this was changing from using a namespace to reference the CustomControl to using a src with a filename
changing this:
<%# Register Assembly="MyProduct" Namespace="MyProduct.CustomControls" TagPrefix="cc" %>
to this:
<%# Register src="/CustomControls/MyControl.ascx" tagname="MyControl" tagprefix="uc2" %>

Asp.net: Can we use MasterPage's viewstate in ContentPage?

Asp.net: Can we use MasterPage's viewstate in ContentPage ?
From a Content Page you can refer to a MasterPage through the Master Property. Create a Property on the Master Page that uses its getter and setter to store its value in ViewState, like so:
string MyProperty
{
get { return ViewState["MyProperty"] as string; }
set { ViewState["MyProperty"] = value; }
}
Obviously you could make that code safer by testing for nulls and what-not...
Here's the important bit: Viewstate elements are only accessible from the controls that added them so you need to refer back up the tree.
You can also strongly type the Master property on Page by using the <%# MasterType %> directive in your ASPX file, thusly:
<%# MasterType VirtualPath="~/masters/SourcePage.master"" %>
HTH.

Resources