Textbox Loop with C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • movieking81
    New Member
    • Feb 2007
    • 52

    Textbox Loop with C#

    Ok, so what I got is 10 textboxes on a .Net page. What I need to do is loop through them in C# and change the ids. The code I have on the .cs page behind the .aspx page is as follows:

    Code:
        
    protected void Page_Load(object sender, EventArgs e)
        {
            LoopTextboxes(Page.Controls);
        }
        private void LoopTextboxes(ControlCollection controlCollection)
        {
            foreach (Control control in controlCollection)
            {
                if (control is TextBox)
                {
                    ((TextBox)control).Text = "Text Here";
                }
                if (control.Controls != null)
                {
                    LoopTextboxes(control.Controls);
                }
            
            }
        }
    Problem is the code can only change certain properties of the textbox, right? Color, Value, etc. Also, the "foreach" conditional statement doesn't loop. Can someone provide a little help so I can loop through and change the ids?

    Thanks
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I do not understand your question.
    Why would you want to change the IDs of TextBoxes?
    If you change the ID of the TextBox then how are you supposed to use it in code later...and know what TextBox you're really accessing?

    Please explain why you are trying to do this so that we can help you find an approach that will work for you.


    -Frinny

    Comment

    • movieking81
      New Member
      • Feb 2007
      • 52

      #3
      The renaming thing was just a idea. What I have are rows/columns of textboxes (30) in a HTML table and each one needs to have it's own identifer so it can updated by the user to a MSSQL database. An example is below. I guess I could do it the long way but that's a lot of typing. I'd rather create a loop and use a variable to create the ID. Is there a better way to do it?

      Code:
      <table>
      <tr>
      <td>
      <asp:Textbox id="tb1" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb2" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb3" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb4" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb5" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb6" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb7" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb8" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb9" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb10" runat="server" />
      </td>
      <td>
      <asp:Textbox id="tb11" runat="server" />
      </td>
      </tr>
      </table>
      Thanks for the help

      Comment

      • semomaniz
        Recognized Expert New Member
        • Oct 2007
        • 210

        #4
        why dont you create the textbox dynamically when you load the page and add that to a panel control. Then use a for each loop to access the controls inside the panel control to access the textboxes itself.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Yes there are a lot of better ways to do this.

          You could use a pre-built control like the GridView control to dynamically create your table and fill it with dynamically created TextBoxes...or you could use a Repeater control to generate your table and TextBoxes.

          The important thing about both of these classes is that they both implement the INamingContaine r Interface. This interface creates a new ID namespace within a Page which means that all of the dynamic controls within the class that implements this interface will be given a unique ID within the control when rendered on the page. This unique ID is combined with the ID of the dynamic control.

          It's pretty cool stuff but you don't have to go down to that level if you don't want to.

          I think, going by your programming style, you'd be most interested in using the Repeater control.

          Or, like semomaniz was suggesting, you could dynamically create these controls if you want to. If you're interested in doing this, please read over this short article on how to use dynamic controls in ASP.NET before you begin or you may end up pulling your hair out in frustration with strange problems that are not apparent at first :)

          -Frinny

          Comment

          • movieking81
            New Member
            • Feb 2007
            • 52

            #6
            The Repeater Control works great, thanks. What I need to do now is include several dropdown list in the repeater, which I've managed to do just fine. However, outside of the repeater I have them filled using XML data, but now since they are in the repeater the names/ids are auto generated. How would I go about filling in the dropdown list with XML data within the repeater? Would this involve the INamingContaine r Interface you mentioned above?

            Thanks again for the help.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              I don't know how I missed your post Movieking81.
              Sorry for the delayed reply.


              You should be able to use the Find(controlID) method to get a reference to the DropDownList that you want to work with.

              -Frinny

              Comment

              Working...