FindControl not working

Discussion in 'ASP .Net' started by Keith G Hicks, May 29, 2008.

  1. I'm not sure what I'm doing wrong. Here's my code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load

    Dim hyp As HyperLink = CType(Me.FindControl("hypFilt" &
    Request.QueryString("Filt")), HyperLink)
    If Not hyp Is Nothing Then
    hyp.Font.Bold = True
    EndIf

    End If

    I have several hyperlink controls on a page. Some are ID = hypFiltA,
    hypFiltB, hypFiltC, etc. They are used to filter some data by first letter
    of the alphabet. The code runs, nothing crashes but but the code above never
    finds them. hyp always = nothing.

    "hypFilt" & Request.QueryString("Filt") does evaluate out to the correct
    value.

    Thanks,

    Keith
     
    Keith G Hicks, May 29, 2008
    #1
    1. Advertising

  2. I should mention that the hyperlink controls are not embeded inside another
    control. They are in a content section but they are recognized by
    intellisense when I type "me.hyp..." into the vb code page.

    "Keith G Hicks" <> wrote in message
    news:...
    > I'm not sure what I'm doing wrong. Here's my code:
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As

    System.EventArgs)
    > Handles Me.Load
    >
    > Dim hyp As HyperLink = CType(Me.FindControl("hypFilt" &
    > Request.QueryString("Filt")), HyperLink)
    > If Not hyp Is Nothing Then
    > hyp.Font.Bold = True
    > EndIf
    >
    > End If
    >
    > I have several hyperlink controls on a page. Some are ID = hypFiltA,
    > hypFiltB, hypFiltC, etc. They are used to filter some data by first letter
    > of the alphabet. The code runs, nothing crashes but but the code above

    never
    > finds them. hyp always = nothing.
    >
    > "hypFilt" & Request.QueryString("Filt") does evaluate out to the correct
    > value.
    >
    > Thanks,
    >
    > Keith
    >
    >
     
    Keith G Hicks, May 29, 2008
    #2
    1. Advertising

  3. Set a breakpoint and see if the id is formed correctly, run Me.FindControl
    in the watch window, see what is inside the Me.Controls collection. This
    should help you to figure out what is going on.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Keith G Hicks" <> wrote in message
    news:...
    > I'm not sure what I'm doing wrong. Here's my code:
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs)
    > Handles Me.Load
    >
    > Dim hyp As HyperLink = CType(Me.FindControl("hypFilt" &
    > Request.QueryString("Filt")), HyperLink)
    > If Not hyp Is Nothing Then
    > hyp.Font.Bold = True
    > EndIf
    >
    > End If
    >
    > I have several hyperlink controls on a page. Some are ID = hypFiltA,
    > hypFiltB, hypFiltC, etc. They are used to filter some data by first letter
    > of the alphabet. The code runs, nothing crashes but but the code above
    > never
    > finds them. hyp always = nothing.
    >
    > "hypFilt" & Request.QueryString("Filt") does evaluate out to the correct
    > value.
    >
    > Thanks,
    >
    > Keith
    >
    >
     
    Eliyahu Goldin, May 29, 2008
    #3
  4. It seems that the ID's are fine. When I put me.hypFiltA, or me.hypFiltB, etc
    in the watch, they give me a value.
    Request.QueryString("Filt") in the watch window returns what I expect: "A"
    or "B" or "C" etc.

    When I put me.Controls in the watch window, I get a count of 1.

    When I put Me.FindControl("hypFilt" & Request.QueryString("Filt")) in the
    watch window, it comes up as Nothing.

    If I hard code me.hypFiltA in the same event handler, it works fine. The
    control exists and I'm able to set its properties. But I don't want to code
    all 26 letters of the alphabet into the event handler.

    I tried this in other page events as well (preload, init, initcomplete,
    loadcomplete, prerender, prerendercomplete... all with same results.


    "Eliyahu Goldin" <> wrote in
    message news:...
    > Set a breakpoint and see if the id is formed correctly, run Me.FindControl
    > in the watch window, see what is inside the Me.Controls collection. This
    > should help you to figure out what is going on.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "Keith G Hicks" <> wrote in message
    > news:...
    > > I'm not sure what I'm doing wrong. Here's my code:
    > >
    > > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > > System.EventArgs)
    > > Handles Me.Load
    > >
    > > Dim hyp As HyperLink = CType(Me.FindControl("hypFilt" &
    > > Request.QueryString("Filt")), HyperLink)
    > > If Not hyp Is Nothing Then
    > > hyp.Font.Bold = True
    > > EndIf
    > >
    > > End If
    > >
    > > I have several hyperlink controls on a page. Some are ID = hypFiltA,
    > > hypFiltB, hypFiltC, etc. They are used to filter some data by first

    letter
    > > of the alphabet. The code runs, nothing crashes but but the code above
    > > never
    > > finds them. hyp always = nothing.
    > >
    > > "hypFilt" & Request.QueryString("Filt") does evaluate out to the correct
    > > value.
    > >
    > > Thanks,
    > >
    > > Keith
    > >
    > >

    >
    >
     
    Keith G Hicks, May 29, 2008
    #4
  5. "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Keith G Hicks" <> wrote in message
    > news:...
    >
    > > When I put Me.FindControl("hypFilt" & Request.QueryString("Filt")) in

    the
    > > watch window, it comes up as Nothing.


    > If you put Request.QueryString("Filt") in the watch window, what do you

    see?

    returns "A"

    >
    > If you put "hypFilt" & Request.QueryString("Filt") in the watch window,

    what
    > do you see?


    returns "hypFiltA"

    > Does Me.FindControl("hypFilt" & Request.QueryString("Filt").ToString())
    > work?


    returns Nothing

    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
     
    Keith G Hicks, May 29, 2008
    #5
  6. No. Like I said in my 2nd post, it's all in a content section. It's a master
    page setup (asp.net 2.0 if that helps). This is one of the content pages.
    Here's the markup (below the @Page and the @Register Assembly lines):

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2"
    runat="Server">
    <asp:Image ID="Image1" runat="server"
    ImageUrl="~/Images/PageTitles_YearbookPics.jpg" /><br />
    <br />
    Click an image for a larger view.<br />
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    runat="Server">
    <br />
    <span style="font-size: 11pt">Filters (based on last name at
    graduation):</span>
    <asp:HyperLink ID="hypFiltAll" runat="server"
    NavigateUrl="~/ClassmateYearbookPics.aspx?Filt=ALL" Font-Size="11pt">Show
    All</asp:HyperLink>
    <asp:HyperLink ID="hypFiltA" runat="server"
    NavigateUrl="~/ClassmateYearbookPics.aspx?Filt=A"
    Font-Size="11pt">A</asp:HyperLink>
    <asp:HyperLink ID="hypFiltB" runat="server"
    NavigateUrl="~/ClassmateYearbookPics.aspx?Filt=B"
    Font-Size="11pt">B</asp:HyperLink>

    etc.....


    "Mark Rae [MVP]" <> wrote in message
    news:#...
    > "Keith G Hicks" <> wrote in message
    > news:...
    >
    > >> If you put Request.QueryString("Filt") in the watch window, what do you
    > >> see?

    > >
    > > returns "A"
    > >
    > >> If you put "hypFilt" & Request.QueryString("Filt") in the watch window,
    > >> what do you see?

    > >
    > > returns "hypFiltA"
    > >
    > >> Does Me.FindControl("hypFilt" & Request.QueryString("Filt").ToString())
    > >> work?

    > >
    > > returns Nothing

    >
    > In which case, it sounds very much like the hypFiltA hyperlink isn't
    > actually in the Page control container. Might it be contained within

    another
    > container within the Page container e.g. an <asp:panel> ...?
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
     
    Keith G Hicks, May 29, 2008
    #6
  7. "Mark Rae [MVP]" <> wrote in message
    news:e8J$...
    > "Keith G Hicks" <> wrote in message
    > news:...
    >
    > [top-posting corrected]
    >
    > >> In which case, it sounds very much like the hypFiltA hyperlink isn't
    > >> actually in the Page control container. Might it be contained within

    > > another
    > >> container within the Page container e.g. an <asp:panel> ...?

    > >
    > > No. Like I said in my 2nd post, it's all in a content section. It's a
    > > master
    > > page setup (asp.net 2.0 if that helps). This is one of the content

    pages.
    > > Here's the markup (below the @Page and the @Register Assembly lines):

    >
    > OK, just for the sake of clarity, the hyperlinks are all within a content
    > page, not the MasterPage.


    That's correct.


    > And presumably the code which is trying to refer to those hyperlinks is
    > similarly behind the content page, not behind the MasterPage...?


    That's correct also.

    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
     
    Keith G Hicks, May 29, 2008
    #7
  8. I am sorry to jump in, but do your hyperlinks have runat=server atribute??
    Find control will only work with .NET object.

    George.

    "Keith G Hicks" <> wrote in message
    news:...
    > "Mark Rae [MVP]" <> wrote in message
    > news:e8J$...
    >> "Keith G Hicks" <> wrote in message
    >> news:...
    >>
    >> [top-posting corrected]
    >>
    >> >> In which case, it sounds very much like the hypFiltA hyperlink isn't
    >> >> actually in the Page control container. Might it be contained within
    >> > another
    >> >> container within the Page container e.g. an <asp:panel> ...?
    >> >
    >> > No. Like I said in my 2nd post, it's all in a content section. It's a
    >> > master
    >> > page setup (asp.net 2.0 if that helps). This is one of the content

    > pages.
    >> > Here's the markup (below the @Page and the @Register Assembly lines):

    >>
    >> OK, just for the sake of clarity, the hyperlinks are all within a content
    >> page, not the MasterPage.

    >
    > That's correct.
    >
    >
    >> And presumably the code which is trying to refer to those hyperlinks is
    >> similarly behind the content page, not behind the MasterPage...?

    >
    > That's correct also.
    >
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVP
    >> http://www.markrae.net
    >>

    >
    >
     
    George Ter-Saakov, May 29, 2008
    #8
  9. Yes. It's in the code I posted a couple of posts up.

    "George Ter-Saakov" <> wrote in message
    news:...
    > I am sorry to jump in, but do your hyperlinks have runat=server atribute??
    > Find control will only work with .NET object.
    >
    > George.
    >
     
    Keith G Hicks, May 29, 2008
    #9
  10. Sorry did not see your message...
    I've never had a problem with FindControl not working as it suppose to...
    So here is my take...

    you have a following code which appears to be fine.

    Dim hyp As HyperLink = CType(Me.FindControl("hypFilt" &
    Request.QueryString("Filt")), HyperLink)
    If Not hyp Is Nothing Then
    hyp.Font.Bold = True
    EndIf

    Following options.

    0. All you do is set Bold to font. Not sure how Hyperlink control works with
    it. The style for <A> tag might take over and overwrite the BOLD thing.
    Cause it might product folowing html code <B><A>B</A></B> then style for A
    tag will take over the <B> tag.
    So do something else.

    1. Code never executes.... Easy to test in debugger or just add
    Response.Write("AAA") right before you do FindCotrol

    My bet would be on #0 then #1.

    George.


    "Keith G Hicks" <> wrote in message
    news:...
    > Yes. It's in the code I posted a couple of posts up.
    >
    > "George Ter-Saakov" <> wrote in message
    > news:...
    >> I am sorry to jump in, but do your hyperlinks have runat=server
    >> atribute??
    >> Find control will only work with .NET object.
    >>
    >> George.
    >>

    >
    >
     
    George Ter-Saakov, May 29, 2008
    #10
  11. There are things I plan to do other than bold. That was my first step. When
    I couldn't get that to work I stopped to solve the other problem. Anyway, I
    solved it. See my post under Mark's

    "George Ter-Saakov" <> wrote in message
    news:uQ#...
    > Sorry did not see your message...
    > I've never had a problem with FindControl not working as it suppose to...
    > So here is my take...
    >
    > you have a following code which appears to be fine.
    >
    > Dim hyp As HyperLink = CType(Me.FindControl("hypFilt" &
    > Request.QueryString("Filt")), HyperLink)
    > If Not hyp Is Nothing Then
    > hyp.Font.Bold = True
    > EndIf
    >
    > Following options.
    >
    > 0. All you do is set Bold to font. Not sure how Hyperlink control works

    with
    > it. The style for <A> tag might take over and overwrite the BOLD thing.
    > Cause it might product folowing html code <B><A>B</A></B> then style for A
    > tag will take over the <B> tag.
    > So do something else.
    >
    > 1. Code never executes.... Easy to test in debugger or just add
    > Response.Write("AAA") right before you do FindCotrol
    >
    > My bet would be on #0 then #1.
    >
    > George.
    >
    >
    > "Keith G Hicks" <> wrote in message
    > news:...
    > > Yes. It's in the code I posted a couple of posts up.
    > >
    > > "George Ter-Saakov" <> wrote in message
    > > news:...
    > >> I am sorry to jump in, but do your hyperlinks have runat=server
    > >> atribute??
    > >> Find control will only work with .NET object.
    > >>
    > >> George.
    > >>

    > >
    > >

    >
    >
     
    Keith G Hicks, May 29, 2008
    #11
  12. There is only one control. Me.Controls.Count = 1. Me.Control(0) =
    {ASP.masterpage_master}. Interesting.

    So here's the answer. Ready?

    Dim hyp As HyperLink =
    CType(Me.Master.FindControl("ContentPlaceHolder1").FindControl("hypFilt" &
    Request.QueryString("Filt")), HyperLink)

    This page helped a lot:

    http://www.west-wind.com/WebLog/posts/5127.aspx

    Apparently master pages really screw things up in this regard.

    Thanks everyone for all your help.

    Keith
     
    Keith G Hicks, May 29, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ivan Demkovitch
    Replies:
    1
    Views:
    824
    Ivan Demkovitch
    Oct 7, 2003
  2. Michael Murschell

    Page.FindControl not working on ASCX

    Michael Murschell, Feb 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    7,825
  3. tshad

    Findcontrol not working in VB

    tshad, Oct 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    691
    tshad
    Oct 20, 2004
  4. K B
    Replies:
    2
    Views:
    5,075
    otkoth
    Sep 17, 2007
  5. =?Utf-8?B?QnJhZCBSb2JlcnRz?=

    FindControl not working in v2 after conversion from 1.1

    =?Utf-8?B?QnJhZCBSb2JlcnRz?=, Nov 9, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    332
    =?Utf-8?B?QnJhZCBSb2JlcnRz?=
    Nov 13, 2006
Loading...

Share This Page