use Findcontrol to find user control
Hi ,
I have registered a control from web.config file, using it on a page.
I have another user controls which are registered in it. I need to find the user control and refer to this registered user control to access literal values.
I am getting object reference error while doing that.
below is my code..below is my code..
In the code "quickScore" is the user control which I drop from my toolbox.
"QuickWizard" is a wizard control in "quickscore" control. I have "screen_103_ctl" as a registered control in "quickscore". I need to access Literal control from the "screen_103_ctl". PLease suggest something....it is urgent.
Thank you.
Dim uc As UserControl = Page.FindControl("quickScore")
Dim wiz As Wizard = uc.FindControl("QuickScoringWizard")
Dim uc1 As UserControl = wiz.FindControl("screen_103_ctl")
Dim lit1 As Literal = uc1.FindControl("Literal1_103")
Dim lit2 As Literal = uc1.FindControl("Literal2_103")
Dim lit3 As Literal = uc1.FindControl("Literal3_103")
Literal1_104.Text = lit1.Text
Literal2_104.Text = lit2.Text
Literal3_104.Text = lit3.Text
Hi Jay,
You can use recursion or create a control container for your control (control that is used as containers for other controls defined through control templates (ITemplate))
If you use Sitefinity 4.0 you can use ControlTraverser - transforms a control hierarchy to a collection by traversing the hierarchy with a specified algorithm
ControlTraverser traverser = new ControlTraverser(this, TraverseMethod.DepthFirst);
Thanks Ivan......can you please provide some relevant code to find the control I drop on the sitefinity page and then find a control inside that....and then find a control inside that control :)...or is there any sample project which I can refer too.
Hi Jay ,
You can get the page node and then loop through its controls collection.
var pm = PageManager.GetManager();
var node = pm.GetPageNodes().Where(n => n.Title ==
"ControlsNode"
).SingleOrDefault();
var g = node.Page.Controls.Where(gt => .....);
internal
protected
virtual
GenericContainer Container
get
if
(
this
.container ==
null
)
this
.container =
this
.CreateContainer(
this
.LayoutTemplate);
return
this
.container;
/// <summary>
/// Creates the container.
/// </summary>
/// <param name="template">The template.</param>
/// <returns></returns>
protected
internal
virtual
GenericContainer CreateContainer(ITemplate template)
GenericContainer container =
new
GenericContainer();
template.InstantiateIn(container);
return
container;
#endregion
protected
virtual
Control Test
get
return
this
.Container.GetControl<Control>(
"Test"
,
false
);
Thanks Ivan,
Do I implement this in my regular .net user control and then upload the modified control into sitefinity?
or do you have some working project where I can refer to?
Thank you very much
Jay Mehta.
Hello Jay,
You should use a custom control - class library. If you inherit from SimpleView then you will be able to use the built-in container that recursively looks inside all controls from your template.
Greetings,
Ivan Dimitrov
the Telerik team
Here is the code snippet I have included in my user control.....let me know if this should work when I upload this control on to sitefinity. or How do I modify this code to make it work with your suggestion.
This code is able to find the contentplaceholder from the master template, but then is not able to find the control which I uploaded to the page..."ucQuickWizard" (This is the name I have mentioned in the web.config in the tools section)...."screen_103_ctl" is a user control sitting in one of my folders on the server.
Dim mpContentPlaceHolder As ContentPlaceHolder
Dim uc As UserControl
Dim lit1 As New Literal
Dim lit2 As New Literal
Dim lit3 As New Literal
mpContentPlaceHolder = CType(Page.Master.FindControl("mainbody"), ContentPlaceHolder)
If Not mpContentPlaceHolder Is Nothing Then
uc = CType(mpContentPlaceHolder.FindControl("ucQuickWizard"), UserControl)
If Not uc Is Nothing Then
Dim wiz As Wizard = uc.FindControl("QuickScoringWizard")
Dim uc1 As UserControl = wiz.FindControl("screen_103_ctl")
lit1 = uc1.FindControl("Literal1_103")
lit2 = uc1.FindControl("Literal2_103")
lit3 = uc1.FindControl("Literal3_103")
Literal1_104.Text = lit1.Text
Literal2_104.Text = lit2.Text
Literal3_104.Text = lit3.Text
End If
End If
Hi Jay Mehta,
As Ivan has noted you have to look through the whole control collection in the content place holder. It might turn out that your control is situated in a child control of the control place holder.
All the best,
Radoslav Georgiev
the Telerik team
Thank you Radoslav.
1 Dim mpContentPlaceHolder As ContentPlaceHolder
2 Dim uc As UserControl
3 Dim lit1 As New Literal
4 Dim lit2 As New Literal
5 Dim lit3 As New Literal
6 mpContentPlaceHolder = CType(Page.Master.FindControl("mainbody"), ContentPlaceHolder)
7 If Not mpContentPlaceHolder Is Nothing Then
8
uc = CType(mpContentPlaceHolder.FindControl("ucQuickWizard"), UserControl)
9
Dim wiz As Wizard = uc.FindControl("QuickScoringWizard")
10
Dim uc1 As UserControl = wiz.FindControl("screen_103_ctl")
11
lit1 = uc1.FindControl("Literal1_103")
12
lit2 = uc1.FindControl("Literal2_103")
13
lit3 = uc1.FindControl("Literal3_103")
14
Literal1_104.Text = lit1.Text
Literal2_104.Text = lit2.Text
Literal3_104.Text = lit3.Text
End If
End If
Hi Ankit,
Could you send the HTML of the control, so we could see whether the issue comes from Sitefinity. It is strange that the controls collection for you is empty and you get null reference exception.
Sincerely yours,
Ivan Dimitrov
the Telerik team