FindControl is terible to use and maintain

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AAaron123

    FindControl is terible to use and maintain

    I'm this far in determining the correct code to find a textbox I need to
    set.

    Me.Master.FindC ontrol("Body1") .FindControl("F orm2").FindCont rol("Table2").F indControl("Tab leRow7").FindCo ntrol("TableCel lR7C2S2").FindC ontrol("RightCP H").FindControl ("div1").FindCo ntrol("div2").F indControl("Log inView1")

    Took me longer than I want to say to produce the above and I'm not there
    yet.



    Isn't there a better way? Seems a routine that recursively searches might
    work.



    So my question is: Is there a better way or do I have to keep working to
    finish the above.



    Thanks



    PS Then if a year form now someone changes almost anything in the .aspx file
    this will break!


  • Mark Rae [MVP]

    #2
    Re: FindControl is terible to use and maintain

    "AAaron123" <aaaron123@road runner.comwrote in message
    news:em4RGAmSJH A.3300@TK2MSFTN GP02.phx.gbl...
    Isn't there a better way? Seems a routine that recursively searches might
    work.
    GIYF:



    --
    Mark Rae
    ASP.NET MVP


    Comment

    • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

      #3
      RE: FindControl is terible to use and maintain

      just a couple lines of code:



      public static List<ControlFin dControls(Contr ol parent,
      Predicate<Contr olmatch)
      {
      var list = new List<Control>() ;
      foreach (Control ctl in parent.Controls )
      {
      if (match(ctl))
      list.Add(ctl);
      list.AddRange(F indControls(ctl , match));
      }
      return list;
      }

      to use:

      var list = FindControls(Pa ge, c =c.ID == "myid");



      -- bruce (sqlwork.com)


      "AAaron123" wrote:
      I'm this far in determining the correct code to find a textbox I need to
      set.
      >
      Me.Master.FindC ontrol("Body1") .FindControl("F orm2").FindCont rol("Table2").F indControl("Tab leRow7").FindCo ntrol("TableCel lR7C2S2").FindC ontrol("RightCP H").FindControl ("div1").FindCo ntrol("div2").F indControl("Log inView1")
      >
      Took me longer than I want to say to produce the above and I'm not there
      yet.
      >
      >
      >
      Isn't there a better way? Seems a routine that recursively searches might
      work.
      >
      >
      >
      So my question is: Is there a better way or do I have to keep working to
      finish the above.
      >
      >
      >
      Thanks
      >
      >
      >
      PS Then if a year form now someone changes almost anything in the .aspx file
      this will break!
      >
      >
      >

      Comment

      • Patrice

        #4
        Re: FindControl is terible to use and maintain

        From the control name you used are you sure this control is not reachable
        directly (ie. a variable is declared automatically for you by the designer
        to access this control) ? Check the designer generated file. You have
        generally to use FindControl when the control is created "dynamicall y" (ie
        inside a repeater template, in a gridview row etc...).

        Then knowing when (and perhaps what) you want to do that could be helpfull.
        You could perhaps define in the markup the function that will handle a
        particular event (such as the init event) so that you can work from there...

        --
        Patrice

        "AAaron123" <aaaron123@road runner.coma écrit dans le message de groupe de
        discussion : em4RGAmSJHA.330 0@TK2MSFTNGP02. phx.gbl...
        I'm this far in determining the correct code to find a textbox I need to
        set.
        >
        Me.Master.FindC ontrol("Body1") .FindControl("F orm2").FindCont rol("Table2").F indControl("Tab leRow7").FindCo ntrol("TableCel lR7C2S2").FindC ontrol("RightCP H").FindControl ("div1").FindCo ntrol("div2").F indControl("Log inView1")
        >
        Took me longer than I want to say to produce the above and I'm not there
        yet.
        >
        >
        >
        Isn't there a better way? Seems a routine that recursively searches might
        work.
        >
        >
        >
        So my question is: Is there a better way or do I have to keep working to
        finish the above.
        >
        >
        >
        Thanks
        >
        >
        >
        PS Then if a year form now someone changes almost anything in the .aspx
        file this will break!
        >
        >

        Comment

        • AAaron123

          #5
          Re: FindControl is terible to use and maintain

          Trying to convert to vb. Got this far but don't know what to do with c >=
          c.ID = "Table2.
          If you know both vb and c# I could use another push

          Dim list As Generic.List(Of Control) = FindControls(Pa ge, c >= c.ID =
          "Table2")

          ....


          Public Shared Function FindControls(By Val parent As Control, ByVal match As
          Predicate(Of Control)) As Generic.List(Of Control)

          Dim list As Generic.List(Of Control) = New Generic.List(Of Control)()

          For Each ctl As Control In parent.Controls

          If match(ctl) Then

          list.Add(ctl)

          End If

          list.AddRange(F indControls(ctl , match)) 'Adds the elements of the collection
          ctl to the end

          Next

          Return list

          End Function

          Thanks

          "bruce barker" <brucebarker@di scussions.micro soft.comwrote in message
          news:75D368FB-3BFF-49A2-A1C5-11119AFE69B1@mi crosoft.com...
          just a couple lines of code:
          >
          >
          >
          public static List<ControlFin dControls(Contr ol parent,
          Predicate<Contr olmatch)
          {
          var list = new List<Control>() ;
          foreach (Control ctl in parent.Controls )
          {
          if (match(ctl))
          list.Add(ctl);
          list.AddRange(F indControls(ctl , match));
          }
          return list;
          }
          >
          to use:
          >
          var list = FindControls(Pa ge, c =c.ID == "myid");
          >
          >
          >
          -- bruce (sqlwork.com)
          >
          >
          "AAaron123" wrote:
          >
          >I'm this far in determining the correct code to find a textbox I need to
          >set.
          >>
          >Me.Master.Find Control("Body1" ).FindControl(" Form2").FindCon trol("Table2"). FindControl("Ta bleRow7").FindC ontrol("TableCe llR7C2S2").Find Control("RightC PH").FindContro l("div1").FindC ontrol("div2"). FindControl("Lo ginView1")
          >>
          >Took me longer than I want to say to produce the above and I'm not there
          >yet.
          >>
          >>
          >>
          >Isn't there a better way? Seems a routine that recursively searches might
          >work.
          >>
          >>
          >>
          >So my question is: Is there a better way or do I have to keep working to
          >finish the above.
          >>
          >>
          >>
          >Thanks
          >>
          >>
          >>
          >PS Then if a year form now someone changes almost anything in the .aspx
          >file
          >this will break!
          >>
          >>
          >>

          Comment

          • AAaron123

            #6
            Re: FindControl is terible to use and maintain


            "Patrice" <http://www.chez.com/scribe/wrote in message
            news:98283130-C7D1-4A3A-9E63-EA85F8090DDB@mi crosoft.com...
            From the control name you used are you sure this control is not reachable
            directly (ie. a variable is declared automatically for you by the designer
            to access this control) ? Check the designer generated file. You have
            generally to use FindControl when the control is created "dynamicall y" (ie
            inside a repeater template, in a gridview row etc...).
            >
            Then knowing when (and perhaps what) you want to do that could be
            helpfull. You could perhaps define in the markup the function that will
            handle a particular event (such as the init event) so that you can work
            from there...
            >
            --
            Patrice
            thanks for you interest.

            I'm playing with asp:ChangePassw ord

            It appears I can change anyone's password if I know the old password.

            However the username textbox opens with my username (I logged in) in it.

            No big deal to remove it but I set the ChangePassword username property to
            "" and my username still appears.

            Bugged me that I could not do that and I tried to find the control the hard
            way.



            ChangePasswordT emplate Property say there should be a "Username" control id



            So, how does one find find such a control?



            Thanks


            Comment

            • AAaron123

              #7
              Re: FindControl is terible to use and maintain

              Can't win. If I spend an hour looking before I ask it's not there. But if I
              ask without looking it's all over the Internet.

              I am glad I asked though because Bruce Barker's response included the
              Predicate delegate which I looking into now.

              Thanks for all the help


              "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
              news:OBuYEHmSJH A.4576@TK2MSFTN GP03.phx.gbl...
              "AAaron123" <aaaron123@road runner.comwrote in message
              news:em4RGAmSJH A.3300@TK2MSFTN GP02.phx.gbl...
              >
              >Isn't there a better way? Seems a routine that recursively searches might
              >work.
              >
              GIYF:

              >
              >
              --
              Mark Rae
              ASP.NET MVP
              http://www.markrae.net

              Comment

              • Patrice

                #8
                Re: FindControl is terible to use and maintain

                I'm not sure to understand at which step you would like to do that as I
                don't know about this particular control but I was thinking about something
                such as having :

                <asp:ChangePass word runat="server" OnPasswordChang ing="MyMethod" />

                in your ASPX markup file (or for any other event that best fit your needs,
                perhaps OnPreRender).

                Then you don't even have to use FindControl, you take control just inside
                whatever handler you want with the sender argument allowing to get the
                control without having to search anything.

                The same technique can apply in other cases...

                --
                Patrice

                "AAaron123" <aaaron123@road runner.coma écrit dans le message de groupe de
                discussion : #rEz0cnSJHA.457 6@TK2MSFTNGP03. phx.gbl...
                >
                "Patrice" <http://www.chez.com/scribe/wrote in message
                news:98283130-C7D1-4A3A-9E63-EA85F8090DDB@mi crosoft.com...
                >From the control name you used are you sure this control is not reachable
                >directly (ie. a variable is declared automatically for you by the
                >designer to access this control) ? Check the designer generated file. You
                >have generally to use FindControl when the control is created
                >"dynamically " (ie inside a repeater template, in a gridview row etc...).
                >>
                >Then knowing when (and perhaps what) you want to do that could be
                >helpfull. You could perhaps define in the markup the function that will
                >handle a particular event (such as the init event) so that you can work
                >from there...
                >>
                >--
                >Patrice
                >
                thanks for you interest.
                >
                I'm playing with asp:ChangePassw ord
                >
                It appears I can change anyone's password if I know the old password.
                >
                However the username textbox opens with my username (I logged in) in it.
                >
                No big deal to remove it but I set the ChangePassword username property to
                "" and my username still appears.
                >
                Bugged me that I could not do that and I tried to find the control the
                hard way.
                >
                >
                >
                ChangePasswordT emplate Property say there should be a "Username" control
                id
                >
                >
                >
                So, how does one find find such a control?
                >
                >
                >
                Thanks
                >
                >

                Comment

                • AAaron123

                  #9
                  Re: FindControl is terible to use and maintain

                  That sound like it would work

                  thanks

                  "Patrice" <http://www.chez.com/scribe/wrote in message
                  news:19075700-9DF5-47F4-BA91-B24FA97ED135@mi crosoft.com...
                  I'm not sure to understand at which step you would like to do that as I
                  don't know about this particular control but I was thinking about
                  something such as having :
                  >
                  <asp:ChangePass word runat="server" OnPasswordChang ing="MyMethod" />
                  >
                  in your ASPX markup file (or for any other event that best fit your needs,
                  perhaps OnPreRender).
                  >
                  Then you don't even have to use FindControl, you take control just inside
                  whatever handler you want with the sender argument allowing to get the
                  control without having to search anything.
                  >
                  The same technique can apply in other cases...
                  >
                  --
                  Patrice
                  >
                  "AAaron123" <aaaron123@road runner.coma écrit dans le message de groupe
                  de discussion : #rEz0cnSJHA.457 6@TK2MSFTNGP03. phx.gbl...
                  >>
                  >"Patrice" <http://www.chez.com/scribe/wrote in message
                  >news:9828313 0-C7D1-4A3A-9E63-EA85F8090DDB@mi crosoft.com...
                  >>From the control name you used are you sure this control is not
                  >>reachable directly (ie. a variable is declared automatically for you by
                  >>the designer to access this control) ? Check the designer generated
                  >>file. You have generally to use FindControl when the control is created
                  >>"dynamicall y" (ie inside a repeater template, in a gridview row etc...).
                  >>>
                  >>Then knowing when (and perhaps what) you want to do that could be
                  >>helpfull. You could perhaps define in the markup the function that will
                  >>handle a particular event (such as the init event) so that you can work
                  >>from there...
                  >>>
                  >>--
                  >>Patrice
                  >>
                  >thanks for you interest.
                  >>
                  >I'm playing with asp:ChangePassw ord
                  >>
                  >It appears I can change anyone's password if I know the old password.
                  >>
                  >However the username textbox opens with my username (I logged in) in it.
                  >>
                  >No big deal to remove it but I set the ChangePassword username property
                  >to "" and my username still appears.
                  >>
                  >Bugged me that I could not do that and I tried to find the control the
                  >hard way.
                  >>
                  >>
                  >>
                  >ChangePassword Template Property say there should be a "Username" control
                  >id
                  >>
                  >>
                  >>
                  >So, how does one find find such a control?
                  >>
                  >>
                  >>
                  >Thanks
                  >>
                  >>
                  >

                  Comment

                  • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

                    #10
                    Re: FindControl is terible to use and maintain

                    its a lambda expression being used as a delgate. i don't know the vb syntax,
                    use an anonymous delegate or a lookup the lambda syntax.

                    -- bruce (sqlwork.com)


                    "AAaron123" wrote:
                    Trying to convert to vb. Got this far but don't know what to do with c >=
                    c.ID = "Table2.
                    If you know both vb and c# I could use another push
                    >
                    Dim list As Generic.List(Of Control) = FindControls(Pa ge, c >= c.ID =
                    "Table2")
                    >
                    ....
                    >
                    >
                    Public Shared Function FindControls(By Val parent As Control, ByVal match As
                    Predicate(Of Control)) As Generic.List(Of Control)
                    >
                    Dim list As Generic.List(Of Control) = New Generic.List(Of Control)()
                    >
                    For Each ctl As Control In parent.Controls
                    >
                    If match(ctl) Then
                    >
                    list.Add(ctl)
                    >
                    End If
                    >
                    list.AddRange(F indControls(ctl , match)) 'Adds the elements of the collection
                    ctl to the end
                    >
                    Next
                    >
                    Return list
                    >
                    End Function
                    >
                    Thanks
                    >
                    "bruce barker" <brucebarker@di scussions.micro soft.comwrote in message
                    news:75D368FB-3BFF-49A2-A1C5-11119AFE69B1@mi crosoft.com...
                    just a couple lines of code:



                    public static List<ControlFin dControls(Contr ol parent,
                    Predicate<Contr olmatch)
                    {
                    var list = new List<Control>() ;
                    foreach (Control ctl in parent.Controls )
                    {
                    if (match(ctl))
                    list.Add(ctl);
                    list.AddRange(F indControls(ctl , match));
                    }
                    return list;
                    }

                    to use:

                    var list = FindControls(Pa ge, c =c.ID == "myid");



                    -- bruce (sqlwork.com)


                    "AAaron123" wrote:
                    I'm this far in determining the correct code to find a textbox I need to
                    set.
                    >
                    Me.Master.FindC ontrol("Body1") .FindControl("F orm2").FindCont rol("Table2").F indControl("Tab leRow7").FindCo ntrol("TableCel lR7C2S2").FindC ontrol("RightCP H").FindControl ("div1").FindCo ntrol("div2").F indControl("Log inView1")
                    >
                    Took me longer than I want to say to produce the above and I'm not there
                    yet.
                    >
                    >
                    >
                    Isn't there a better way? Seems a routine that recursively searches might
                    work.
                    >
                    >
                    >
                    So my question is: Is there a better way or do I have to keep working to
                    finish the above.
                    >
                    >
                    >
                    Thanks
                    >
                    >
                    >
                    PS Then if a year form now someone changes almost anything in the .aspx
                    file
                    this will break!
                    >
                    >
                    >
                    >
                    >
                    >

                    Comment

                    Working...