FindControl not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith G Hicks

    FindControl not working

    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.EventArg s)
    Handles Me.Load

    Dim hyp As HyperLink = CType(Me.FindCo ntrol("hypFilt" &
    Request.QuerySt ring("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.QuerySt ring("Filt") does evaluate out to the correct
    value.

    Thanks,

    Keith


  • Keith G Hicks

    #2
    Re: FindControl not working

    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" <krh@comcast.ne twrote in message
    news:uFenXRSwIH A.1504@TK2MSFTN GP05.phx.gbl...
    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.EventArg s)
    Handles Me.Load
    >
    Dim hyp As HyperLink = CType(Me.FindCo ntrol("hypFilt" &
    Request.QuerySt ring("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.QuerySt ring("Filt") does evaluate out to the correct
    value.
    >
    Thanks,
    >
    Keith
    >
    >

    Comment

    • Eliyahu Goldin

      #3
      Re: FindControl not working

      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]




      "Keith G Hicks" <krh@comcast.ne twrote in message
      news:uFenXRSwIH A.1504@TK2MSFTN GP05.phx.gbl...
      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.EventArg s)
      Handles Me.Load
      >
      Dim hyp As HyperLink = CType(Me.FindCo ntrol("hypFilt" &
      Request.QuerySt ring("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.QuerySt ring("Filt") does evaluate out to the correct
      value.
      >
      Thanks,
      >
      Keith
      >
      >

      Comment

      • Keith G Hicks

        #4
        Re: FindControl not working

        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.QuerySt ring("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.QuerySt ring("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, prerendercomple te... all with same results.


        "Eliyahu Goldin" <REMOVEALLCAPIT ALSeEgGoldDinN@ mMvVpPsS.orgwro te in
        message news:eXlvunWwIH A.5472@TK2MSFTN GP06.phx.gbl...
        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]


        >
        >
        "Keith G Hicks" <krh@comcast.ne twrote in message
        news:uFenXRSwIH A.1504@TK2MSFTN GP05.phx.gbl...
        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.EventArg s)
        Handles Me.Load

        Dim hyp As HyperLink = CType(Me.FindCo ntrol("hypFilt" &
        Request.QuerySt ring("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.QuerySt ring("Filt") does evaluate out to the correct
        value.

        Thanks,

        Keith
        >
        >

        Comment

        • Mark Rae [MVP]

          #5
          Re: FindControl not working

          "Keith G Hicks" <krh@comcast.ne twrote in message
          news:u4HbKcYwIH A.4560@TK2MSFTN GP03.phx.gbl...
          When I put Me.FindControl( "hypFilt" & Request.QuerySt ring("Filt")) in the
          watch window, it comes up as Nothing.
          If you put Request.QuerySt ring("Filt") in the watch window, what do you see?

          If you put "hypFilt" & Request.QuerySt ring("Filt") in the watch window, what
          do you see?

          Does Me.FindControl( "hypFilt" & Request.QuerySt ring("Filt").To String())
          work?


          --
          Mark Rae
          ASP.NET MVP


          Comment

          • Keith G Hicks

            #6
            Re: FindControl not working


            "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
            news:Og0NbkYwIH A.2028@TK2MSFTN GP05.phx.gbl...
            "Keith G Hicks" <krh@comcast.ne twrote in message
            news:u4HbKcYwIH A.4560@TK2MSFTN GP03.phx.gbl...
            >
            When I put Me.FindControl( "hypFilt" & Request.QuerySt ring("Filt")) in
            the
            watch window, it comes up as Nothing.
            If you put Request.QuerySt ring("Filt") in the watch window, what do you
            see?

            returns "A"
            >
            If you put "hypFilt" & Request.QuerySt ring("Filt") in the watch window,
            what
            do you see?
            returns "hypFiltA"
            Does Me.FindControl( "hypFilt" & Request.QuerySt ring("Filt").To String())
            work?
            returns Nothing

            Comment

            • Mark Rae [MVP]

              #7
              Re: FindControl not working

              "Keith G Hicks" <krh@comcast.ne twrote in message
              news:OlZNHsYwIH A.4916@TK2MSFTN GP03.phx.gbl...
              >If you put Request.QuerySt ring("Filt") in the watch window, what do you
              >see?
              >
              returns "A"
              >
              >If you put "hypFilt" & Request.QuerySt ring("Filt") in the watch window,
              >what do you see?
              >
              returns "hypFiltA"
              >
              >Does Me.FindControl( "hypFilt" & Request.QuerySt ring("Filt").To String())
              >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


              Comment

              • Keith G Hicks

                #8
                Re: FindControl not working

                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" ContentPlaceHol derID="ContentP laceHolder2"
                runat="Server">
                <asp:Image ID="Image1" runat="server"
                ImageUrl="~/Images/PageTitles_Year bookPics.jpg" /><br />
                <br />
                Click an image for a larger view.<br />
                </asp:Content>
                <asp:Content ID="Content2" ContentPlaceHol derID="ContentP laceHolder1"
                runat="Server">
                <br />
                <span style="font-size: 11pt">Filters (based on last name at
                graduation):</span>
                <asp:HyperLin k ID="hypFiltAll " runat="server"
                NavigateUrl="~/ClassmateYearbo okPics.aspx?Fil t=ALL" Font-Size="11pt">Sho w
                All</asp:HyperLink>
                <asp:HyperLin k ID="hypFiltA" runat="server"
                NavigateUrl="~/ClassmateYearbo okPics.aspx?Fil t=A"
                Font-Size="11pt">A</asp:HyperLink>
                <asp:HyperLin k ID="hypFiltB" runat="server"
                NavigateUrl="~/ClassmateYearbo okPics.aspx?Fil t=B"
                Font-Size="11pt">B</asp:HyperLink>

                etc.....


                "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                news:#EQ06wYwIH A.576@TK2MSFTNG P05.phx.gbl...
                "Keith G Hicks" <krh@comcast.ne twrote in message
                news:OlZNHsYwIH A.4916@TK2MSFTN GP03.phx.gbl...
                >
                If you put Request.QuerySt ring("Filt") in the watch window, what do you
                see?
                returns "A"
                If you put "hypFilt" & Request.QuerySt ring("Filt") in the watch window,
                what do you see?
                returns "hypFiltA"
                Does Me.FindControl( "hypFilt" & Request.QuerySt ring("Filt").To String())
                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

                >

                Comment

                • Mark Rae [MVP]

                  #9
                  Re: FindControl not working

                  "Keith G Hicks" <krh@comcast.ne twrote in message
                  news:uQvEA9YwIH A.4376@TK2MSFTN GP06.phx.gbl...

                  [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.

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


                  --
                  Mark Rae
                  ASP.NET MVP


                  Comment

                  • Keith G Hicks

                    #10
                    Re: FindControl not working

                    "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                    news:e8J$xEZwIH A.3860@TK2MSFTN GP06.phx.gbl...
                    "Keith G Hicks" <krh@comcast.ne twrote in message
                    news:uQvEA9YwIH A.4376@TK2MSFTN GP06.phx.gbl...
                    >
                    [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.

                    Comment

                    • George Ter-Saakov

                      #11
                      Re: FindControl not working

                      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" <krh@comcast.ne twrote in message
                      news:OzXlf8awIH A.1236@TK2MSFTN GP02.phx.gbl...
                      "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                      news:e8J$xEZwIH A.3860@TK2MSFTN GP06.phx.gbl...
                      >"Keith G Hicks" <krh@comcast.ne twrote in message
                      >news:uQvEA9YwI HA.4376@TK2MSFT NGP06.phx.gbl.. .
                      >>
                      >[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
                      >>
                      >
                      >

                      Comment

                      • Keith G Hicks

                        #12
                        Re: FindControl not working

                        Yes. It's in the code I posted a couple of posts up.

                        "George Ter-Saakov" <gt-nsp@cardone.com wrote in message
                        news:uw2XYJbwIH A.4876@TK2MSFTN GP02.phx.gbl...
                        I am sorry to jump in, but do your hyperlinks have runat=server atribute??
                        Find control will only work with .NET object.
                        >
                        George.
                        >

                        Comment

                        • George Ter-Saakov

                          #13
                          Re: FindControl not working

                          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.FindCo ntrol("hypFilt" &
                          Request.QuerySt ring("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 <Atag might take over and overwrite the BOLD thing.
                          Cause it might product folowing html code <B><A>B</A></Bthen style for A
                          tag will take over the <Btag.
                          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" <krh@comcast.ne twrote in message
                          news:uVklaabwIH A.4376@TK2MSFTN GP06.phx.gbl...
                          Yes. It's in the code I posted a couple of posts up.
                          >
                          "George Ter-Saakov" <gt-nsp@cardone.com wrote in message
                          news:uw2XYJbwIH A.4876@TK2MSFTN GP02.phx.gbl...
                          >I am sorry to jump in, but do your hyperlinks have runat=server
                          >atribute??
                          >Find control will only work with .NET object.
                          >>
                          >George.
                          >>
                          >
                          >

                          Comment

                          • Mark Rae [MVP]

                            #14
                            Re: FindControl not working

                            "Keith G Hicks" <krh@comcast.ne twrote in message
                            news:OzXlf8awIH A.1236@TK2MSFTN GP02.phx.gbl...
                            >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.
                            Hmm - OK. I'm starting to run out of options now...

                            If the code which is trying to reference the hyperlink is definitely behind
                            the same page where the hyperlink is defined, and the hyperlink is not
                            contained within a separate container, then FindControl should definitely
                            find it.

                            So, can you please try the following:

                            In the watch window, evaluate Me.Controls.Cou nt and then inspect each of
                            them e.g.

                            Me.Controls[0]
                            Me.Controls[1]

                            etc

                            Is hypFiltA one of them...?


                            --
                            Mark Rae
                            ASP.NET MVP


                            Comment

                            • Keith G Hicks

                              #15
                              Re: FindControl not working

                              There is only one control. Me.Controls.Cou nt = 1. Me.Control(0) =
                              {ASP.masterpage _master}. Interesting.

                              So here's the answer. Ready?

                              Dim hyp As HyperLink =
                              CType(Me.Master .FindControl("C ontentPlaceHold er1").FindContr ol("hypFilt" &
                              Request.QuerySt ring("Filt")), HyperLink)

                              This page helped a lot:

                              Using MasterPages in your layout changes the control hierarchy of your page considerably which can break some existing code that relies on dynamically locating controls on the page with FindControl().


                              Apparently master pages really screw things up in this regard.

                              Thanks everyone for all your help.

                              Keith




                              Comment

                              Working...