find control question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

    #1

    find control question

    i have a master page and aseries of controls labeled fn1, fn2, fn3 , ...

    i want ot loop and use find control but i'm finding them.
    i'm using the following w/o luck

    ContentPlaceHol der cph =
    (ContentPlaceHo lder)Page.Maste r.FindControl(" ContentPlaceHol der1");

    for (int i = 0; i < 7; i++)
    {
    TextBox tbF = (TextBox)cph.Fi ndControl("tbFN " + i.ToString());
    TextBox tbL = (TextBox)cph.Fi ndControl("tbLN " + i.ToString());

    .........
    }

    they come up null
    where are the controls located??
    --
    (i''ll be asking a lot of these, but I find C# totally way cooler than vb
    and there''s no go''n back!!!)
    thanks (as always)

    kes
  • Mark Rae [MVP]

    #2
    Re: find control question

    "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
    news:A671F33D-9E47-4AC9-9020-5E8C09CC7226@mi crosoft.com...
    They come up null
    Where are the controls located??
    Are they located within another container, maybe an <asp:Panel /...?


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

      #3
      Re: find control question


      Appreciate the response!
      no just an HTML table.
      i have to be doing something odd for this not to be straight forward

      if i wanted to use findcontrol even though i don't need to us shouldn't it
      just work?

      the object Page.Master.Nam ingContainer gives me a list of the controls in
      debug
      i'd think a simple findControl should work?
      --
      (i''ll be asking a lot of these, but I find C# totally way cooler than vb
      and there''s no go''n back!!!)
      thanks (as always)

      kes


      "Mark Rae [MVP]" wrote:
      "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
      news:A671F33D-9E47-4AC9-9020-5E8C09CC7226@mi crosoft.com...
      >
      They come up null
      Where are the controls located??
      >
      Are they located within another container, maybe an <asp:Panel /...?
      >
      >
      --
      Mark Rae
      ASP.NET MVP

      >
      >

      Comment

      • Mark Rae [MVP]

        #4
        Re: find control question

        "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
        news:9B114814-9400-4092-971F-221E67E4CBDF@mi crosoft.com...

        [top-posting corrected]
        >>>They come up null
        Where are the controls located??
        >>
        >Are they located within another container, maybe an <asp:Panel /...?
        >
        No just an HTML table.
        As I thought... The answer is, therefore, "Yes, they are located within an
        HTML table"... ;-)
        I have to be doing something odd for this not to be straight forward
        Nope - what you are seeing is standard behaviour.
        If I wanted to use FindControl even though I don't need to us shouldn't it
        just work?
        It is working precisely as it was designed, not the way you think it
        should... :-)

        FindControl is not recursive - if you tell it to find a control in a page,
        it will find only those controls which are directly contained within the
        Page container, not those which are contained in child containers.

        There are loads of workarounds for this:



        --
        Mark Rae
        ASP.NET MVP


        Comment

        • =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?=

          #5
          RE: find control question

          total bummer here with this i, know! My solution is total over kill here, but
          it works

          ok, use generics to return a list of controls.
          i use this in a name space, but you could just use it for a simple
          /// <summary>
          /// returns a list of the given controls
          /// </summary>
          /// <param name="root"></param>
          /// <param name="typ"></param>
          /// <param name="ControlLi st"></param>
          /// <returns></returns>
          public static List<ControlGet AListOfControls (Control root, Type
          typ, List<ControlCon trolList)
          {
          foreach (Control t in root.Controls)
          {
          if (t.GetType() == typ)
          ControlList.Add (t);
          if (t.Controls.Cou nt 0)
          {
          ControlList = GetAListOfContr ols(t, typ, ControlList);
          }
          }
          return ControlList;
          }

          then call it thus for the texh boxes:

          List<Controlctr ls = new List<Control>() ;
          ctrls = AdminPageHelper .GetAListOfCont rols(cph, typeof(TextBox) , ctls);

          from there you got the list loop or use as you see fit.

          --



          "WebBuilder 451" wrote:
          i have a master page and aseries of controls labeled fn1, fn2, fn3 , ...
          >
          i want ot loop and use find control but i'm finding them.
          i'm using the following w/o luck
          >
          ContentPlaceHol der cph =
          (ContentPlaceHo lder)Page.Maste r.FindControl(" ContentPlaceHol der1");
          >
          for (int i = 0; i < 7; i++)
          {
          TextBox tbF = (TextBox)cph.Fi ndControl("tbFN " + i.ToString());
          TextBox tbL = (TextBox)cph.Fi ndControl("tbLN " + i.ToString());
          >
          ........
          }
          >
          they come up null
          where are the controls located??
          --
          (i''ll be asking a lot of these, but I find C# totally way cooler than vb
          and there''s no go''n back!!!)
          thanks (as always)
          >
          kes

          Comment

          • =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

            #6
            RE: find control question

            a lot of work for a simple task. But i'll try it!
            --
            (i''ll be asking a lot of these, but I find C# totally way cooler than vb
            and there''s no go''n back!!!)
            thanks (as always)

            kes


            "Yankee Imperialist Dog" wrote:
            total bummer here with this i, know! My solution is total over kill here, but
            it works
            >
            ok, use generics to return a list of controls.
            i use this in a name space, but you could just use it for a simple
            /// <summary>
            /// returns a list of the given controls
            /// </summary>
            /// <param name="root"></param>
            /// <param name="typ"></param>
            /// <param name="ControlLi st"></param>
            /// <returns></returns>
            public static List<ControlGet AListOfControls (Control root, Type
            typ, List<ControlCon trolList)
            {
            foreach (Control t in root.Controls)
            {
            if (t.GetType() == typ)
            ControlList.Add (t);
            if (t.Controls.Cou nt 0)
            {
            ControlList = GetAListOfContr ols(t, typ, ControlList);
            }
            }
            return ControlList;
            }
            >
            then call it thus for the texh boxes:
            >
            List<Controlctr ls = new List<Control>() ;
            ctrls = AdminPageHelper .GetAListOfCont rols(cph, typeof(TextBox) , ctls);
            >
            from there you got the list loop or use as you see fit.
            >
            --
            >
            >
            >
            "WebBuilder 451" wrote:
            >
            i have a master page and aseries of controls labeled fn1, fn2, fn3 , ...

            i want ot loop and use find control but i'm finding them.
            i'm using the following w/o luck

            ContentPlaceHol der cph =
            (ContentPlaceHo lder)Page.Maste r.FindControl(" ContentPlaceHol der1");

            for (int i = 0; i < 7; i++)
            {
            TextBox tbF = (TextBox)cph.Fi ndControl("tbFN " + i.ToString());
            TextBox tbL = (TextBox)cph.Fi ndControl("tbLN " + i.ToString());

            ........
            }

            they come up null
            where are the controls located??
            --
            (i''ll be asking a lot of these, but I find C# totally way cooler than vb
            and there''s no go''n back!!!)
            thanks (as always)

            kes

            Comment

            • =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

              #7
              Re: find control question

              ok, thanks i'll check them out

              --
              (i''ll be asking a lot of these, but I find C# totally way cooler than vb
              and there''s no go''n back!!!)
              thanks (as always)

              kes


              "Mark Rae [MVP]" wrote:
              "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
              news:9B114814-9400-4092-971F-221E67E4CBDF@mi crosoft.com...
              >
              [top-posting corrected]
              >
              >>They come up null
              Where are the controls located??
              >
              Are they located within another container, maybe an <asp:Panel /...?
              No just an HTML table.
              >
              As I thought... The answer is, therefore, "Yes, they are located within an
              HTML table"... ;-)
              >
              I have to be doing something odd for this not to be straight forward
              >
              Nope - what you are seeing is standard behaviour.
              >
              If I wanted to use FindControl even though I don't need to us shouldn't it
              just work?
              >
              It is working precisely as it was designed, not the way you think it
              should... :-)
              >
              FindControl is not recursive - if you tell it to find a control in a page,
              it will find only those controls which are directly contained within the
              Page container, not those which are contained in child containers.
              >
              There are loads of workarounds for this:

              >
              >
              --
              Mark Rae
              ASP.NET MVP

              >
              >

              Comment

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

                #8
                RE: find control question

                then try this routine:

                var list = ControlWalker(t his, ctl =>
                ctl.ID.StartsWi th("tbFN") ||
                ctl.ID.StartsWi th("tbLN"));

                ....

                public List<ControlCon trolWalker(
                Control ctl,
                Predicate<Contr olmatcher)
                {
                var list = new List<Control>() ;
                if (matcher(ctl)) list.Add(ctl);
                for (int i=0; i < ctl.Controls.Co unt; ++i)
                {
                var childList = ControlWalker(
                ctl.Controls[i],matcher);
                if (childList.Coun t 0)
                list.AddRange(c hildList);
                }
                return (list);
                }



                -- bruce (sqlwork.com)


                "WebBuilder 451" wrote:
                a lot of work for a simple task. But i'll try it!
                --
                (i''ll be asking a lot of these, but I find C# totally way cooler than vb
                and there''s no go''n back!!!)
                thanks (as always)
                >
                kes
                >
                >
                "Yankee Imperialist Dog" wrote:
                >
                total bummer here with this i, know! My solution is total over kill here, but
                it works

                ok, use generics to return a list of controls.
                i use this in a name space, but you could just use it for a simple
                /// <summary>
                /// returns a list of the given controls
                /// </summary>
                /// <param name="root"></param>
                /// <param name="typ"></param>
                /// <param name="ControlLi st"></param>
                /// <returns></returns>
                public static List<ControlGet AListOfControls (Control root, Type
                typ, List<ControlCon trolList)
                {
                foreach (Control t in root.Controls)
                {
                if (t.GetType() == typ)
                ControlList.Add (t);
                if (t.Controls.Cou nt 0)
                {
                ControlList = GetAListOfContr ols(t, typ, ControlList);
                }
                }
                return ControlList;
                }

                then call it thus for the texh boxes:

                List<Controlctr ls = new List<Control>() ;
                ctrls = AdminPageHelper .GetAListOfCont rols(cph, typeof(TextBox) , ctls);

                from there you got the list loop or use as you see fit.

                --



                "WebBuilder 451" wrote:
                i have a master page and aseries of controls labeled fn1, fn2, fn3 , ...
                >
                i want ot loop and use find control but i'm finding them.
                i'm using the following w/o luck
                >
                ContentPlaceHol der cph =
                (ContentPlaceHo lder)Page.Maste r.FindControl(" ContentPlaceHol der1");
                >
                for (int i = 0; i < 7; i++)
                {
                TextBox tbF = (TextBox)cph.Fi ndControl("tbFN " + i.ToString());
                TextBox tbL = (TextBox)cph.Fi ndControl("tbLN " + i.ToString());
                >
                ........
                }
                >
                they come up null
                where are the controls located??
                --
                (i''ll be asking a lot of these, but I find C# totally way cooler than vb
                and there''s no go''n back!!!)
                thanks (as always)
                >
                kes

                Comment

                Working...