Find Control : Going around in circles

  • Thread starter Thread starter Jennifer Mathews
  • Start date Start date
J

Jennifer Mathews

Is there a method get the value in a hidden field in the following scenario? I have
tried various FindControl with Parent combinations and seem to be going around in
circles with Parent.Parent.etc
Thanks

MasterPage
UserControl : ucPerson
HiddenField : hdnPersonID

WebForm based upon the MasterPage
UserControl : uc1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc1_1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc1_2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2_1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2_2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2_2_1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2_2_2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc3 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc3_1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc3_2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
 
Is there a method get the value in a hidden field in the following
scenario? I have tried various FindControl with Parent combinations
and seem to be going around in circles with Parent.Parent.etc
Thanks

The first thing I would do is create a property for anything you want to
grab from the control, as this makes things easier. you then hook the
property to the actual label you are using for hidden.

Master.FindControl("ucPerson")

Assuming, of course, ucPerson is the control name in the master. Once you
have the control, cast it out as the user control and get the property.

But this assumes your architecture is sound. As you are holding a person id
(the person logged in?), you can just hold that in session, or elsewhere,
rather than continually pull from the master page control collection.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Is there a method get the value in a hidden field in the following scenario?  I have
tried various FindControl with Parent combinations and seem to be going around in
circles with Parent.Parent.etc
Thanks

MasterPage
    UserControl    :    ucPerson
        HiddenField    :    hdnPersonID

WebForm based upon the MasterPage
    UserControl    :    uc1                   ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
        UserControl    :    uc1_1           ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
        UserControl    :    uc1_2           ' requires  access MasterPage's
ucPerson.hdnPersonID in codebehind
    UserControl    :    uc2                   ' requires  access MasterPage's
ucPerson.hdnPersonID in codebehind
        UserControl    :    uc2_1           ' requires  access MasterPage's
ucPerson.hdnPersonID in codebehind
        UserControl    :    uc2_2           ' requires  access MasterPage's
ucPerson.hdnPersonID in codebehind
            UserControl    :    uc2_2_1   ' requires  access MasterPage's
ucPerson.hdnPersonID in codebehind
            UserControl    :    uc2_2_2   ' requires  access MasterPage's
ucPerson.hdnPersonID in codebehind
   UserControl    :    uc3                    ' requires  access MasterPage's
ucPerson.hdnPersonID in codebehind
        UserControl    :    uc3_1           ' requires  access MasterPage's
ucPerson.hdnPersonID in codebehind
        UserControl    :    uc3_2           ' requires  access MasterPage's
ucPerson.hdnPersonID in codebehind

Where do you want to get it? In the master page you can access it
based on ContentPlaceHolder

e.g.

Response.Write(DirectCast(Me.FindControl
("ContentPlaceHolder1").FindControl("HiddenField1"),
HiddenField).Value)

You can try to enable trace to see controls hierarchy. Add <%@ Page
Trace="True" ... %> to the content page and you will see where your
control is.

After postback you can also get it using Form Collection...
 
hold that in session
I used that to begin with and unfortunately under some circumstances it doesn't work.
The circumstances have nothing to do with Session("xxx") not working but for another
reason.
I am in the codebehind in UserControl "uc1" but there is no
Me.Master.FindControl("whatever") .
Basically, Me.Master.FindControl isn't a function. Is there another function I can use?


MasterPage
UserControl : ucPerson
HiddenField : hdnPersonID

WebForm based upon the MasterPage
UserControl : uc1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
 
Where do you want to get it? In the master page you can access itI do not need it in the MasterPage but in UserControls that are imbedded in other UC's
that are inbedded in WebForms that use the MasterPage.
I can't use the ContentPlaceHolder because it can change.
I need it both before and after Postback.


Is there a method get the value in a hidden field in the following scenario? I have
tried various FindControl with Parent combinations and seem to be going around in
circles with Parent.Parent.etc
Thanks

MasterPage
UserControl : ucPerson
HiddenField : hdnPersonID

WebForm based upon the MasterPage
UserControl : uc1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc1_1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc1_2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2_1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2_2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2_2_1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc2_2_2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc3 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc3_1 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind
UserControl : uc3_2 ' requires access MasterPage's
ucPerson.hdnPersonID in codebehind

Where do you want to get it? In the master page you can access it
based on ContentPlaceHolder

e.g.

Response.Write(DirectCast(Me.FindControl
("ContentPlaceHolder1").FindControl("HiddenField1"),
HiddenField).Value)

You can try to enable trace to see controls hierarchy. Add <%@ Page
Trace="True" ... %> to the content page and you will see where your
control is.

After postback you can also get it using Form Collection...
 
I view sourced the web page and the hdnPersonID is defined
with the ID = "ctl00_ctl00_cph_MainContent_ucLogin_Ctl_hdnPersonID"

so I tried :
Me.FindControl("ctl00_ctl00_cph_MainContent_ucLogin_Ctl_hdnPersonID")

That didn't work either.

Any other ideas?
 
I do not need it in the MasterPage but in UserControls that are imbedded in other UC's
that are inbedded in WebForms that use the MasterPage.


I can't use the ContentPlaceHolder because it can change.


I need it both before and after Postback.

If HiddenField Control is in UserControl then why can't you get it as
hdnPersonID.Value? I don't get it...

Anyway, there is a Controls collection you can use. For example, when
you have

MasterPage1
....ContentPage1
........ucPerson (UserControl)
.............hdnPersonID (HiddenField)

Then to access hdnPersonID from ucPerson you can use Me.Parent.Controls
(X).Controls(X). You need to enumerate through all controls and check
type...
 
Anyway, there is a Controls collection you can use. For example, when
you have

MasterPage1
...ContentPage1
.......ucPerson (UserControl)
............hdnPersonID (HiddenField)

Then to access hdnPersonID from ucPerson you can use Me.Parent.Controls
(X).Controls(X). You need to enumerate through all controls and check
type...

Here's an example

For Each c As Control In Me.Parent.Controls
If TypeOf c Is ucPerson Then
Response.Write("hdnPersonID=" & DirectCast
(c.FindControl("hdnPersonID"), HiddenField).Value)
Exit For
End If
Next
 
I view sourced the web page and the hdnPersonID is defined
with the ID   = "ctl00_ctl00_cph_MainContent_ucLogin_Ctl_hdnPersonID"

so I tried  :
Me.FindControl("ctl00_ctl00_cph_MainContent_ucLogin_Ctl_hdnPersonID")

I suppose that MainContent is a name of parent page and you call
Me.FindControl from within the ucLogin or Ctl where id of MainContent
is not accessible. Me.FindControl
("ctl00_ctl00_cph_MainContent_ucLogin_Ctl_hdnPersonID") will work only
from Master- or Content Page.
 
I tried something with :

For Each c As Control In Me.Parent.Controls

but the trouble was there were containers in containers because of the
MasterPage\Webforms base upon MP\UserControls\UserControls in UserControls\etc.. Trying
to get to the correct hdnField within all these containers didn't seem to work because
apparently I enumerating the containers properly. (I think I spent 1 to 2 days trying
to create a function for that.)

I tried searching on the web for examples that enumerated the entire html forms
collection but nothing seemed to get me to the hidden field I need.

That is why I thought to ask here. There has got to be an easier way to grab a field
value in a UserControl that the current UC knows nothing about.



Anyway, there is a Controls collection you can use. For example, when
you have

MasterPage1
...ContentPage1
.......ucPerson (UserControl)
............hdnPersonID (HiddenField)

Then to access hdnPersonID from ucPerson you can use Me.Parent.Controls
(X).Controls(X). You need to enumerate through all controls and check
type...

Here's an example

For Each c As Control In Me.Parent.Controls
If TypeOf c Is ucPerson Then
Response.Write("hdnPersonID=" & DirectCast
(c.FindControl("hdnPersonID"), HiddenField).Value)
Exit For
End If
Next
 
you are going about this wrong.

if you want to put the ucPerson user control anywhere then it should
implement an interface (say IPersonID), that other controls can look
for. then starting at Page.Control, you can recurse thru the control
collection to find it:

public interface IPersonID
{
string Value {get;set;}
}
public class HiddenPersonID : HtmlInputHidden, IPersonID
{
public static IPersonID FindMe(Control ctl)
{
if (ctl is IPersonID)
return (IPersonID)ctl;
foreach (Control child in ctl.Controls)
{
var findCtl = FindMe(child);
if (findCtl != null)
return findCtl;
}
return null;
}
}

place on page or master:

<my:HiddenPersonID ID="personid" runat="server" />

to get its value:

var value = MyControls.HiddenPersonID.FindMe(Page).Value;

you could skip the interface and just look for a HiddenPersonID, but the
interface is a better practice.

-- bruce (sqlwork.com)
 
Back
Top