Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. problem creating textbox on the fly

problem creating textbox on the fly

Scheduled Pinned Locked Moved ASP.NET
help
17 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E eyeseetee

    I am trying to create a function which creates a certain amount of textboxes depending on how many are in the datatable. I have the following code so far: foreach (DataRow row in dataSetselectactivities.Tables[0].Rows) { TextBox tb = new TextBox(); tb.Visible = true; } but how would I add a new value to the textbox for each row, would it be something like tb.text += dttablevalue any help would be great thanks

    W Offline
    W Offline
    wEb GuRu
    wrote on last edited by
    #2

    You need to create Array of Controls instead, to get/set attributes of all textboxes.

    All the way from Sialkot ...

    E 1 Reply Last reply
    0
    • W wEb GuRu

      You need to create Array of Controls instead, to get/set attributes of all textboxes.

      All the way from Sialkot ...

      E Offline
      E Offline
      eyeseetee
      wrote on last edited by
      #3

      ok, ive got this now public void CreateTextBoxes(Object sender, EventArgs e) { foreach (DataRow row in dataSetselectactivities.Tables[0].Rows) { TextBoxesHere.Controls.Add(new TextBox()); } } I am not showing the datatable in the code above because it would take up too much room but it does exist. Anyway Ive managed to create as many textboxes as there are rows dynamically. However now I need to fill each one with data from the datatable. How do I go about that? thanks!

      I M 2 Replies Last reply
      0
      • E eyeseetee

        ok, ive got this now public void CreateTextBoxes(Object sender, EventArgs e) { foreach (DataRow row in dataSetselectactivities.Tables[0].Rows) { TextBoxesHere.Controls.Add(new TextBox()); } } I am not showing the datatable in the code above because it would take up too much room but it does exist. Anyway Ive managed to create as many textboxes as there are rows dynamically. However now I need to fill each one with data from the datatable. How do I go about that? thanks!

        I Offline
        I Offline
        Imran Khan Pathan
        wrote on last edited by
        #4

        public void createTextBoxes(DataTable dt)
        {
        TextBox txt;
        int Count = 1;
        foreach (DataRow dr in dt.Rows)
        {
        txt = new TextBox();
        txt.ID = "txt_" + Count;
        txt.Text = dr["FieldName"].ToString();
        //Add this control in form
        Count+=1;
        }
        }

        regard kHan

        please don't forget to vote on the post that helped you.

        modified on Tuesday, July 8, 2008 8:05 AM

        1 Reply Last reply
        0
        • E eyeseetee

          ok, ive got this now public void CreateTextBoxes(Object sender, EventArgs e) { foreach (DataRow row in dataSetselectactivities.Tables[0].Rows) { TextBoxesHere.Controls.Add(new TextBox()); } } I am not showing the datatable in the code above because it would take up too much room but it does exist. Anyway Ive managed to create as many textboxes as there are rows dynamically. However now I need to fill each one with data from the datatable. How do I go about that? thanks!

          M Offline
          M Offline
          Michael Bookatz
          wrote on last edited by
          #5

          public void CreateTextBoxes(Object sender, EventArgs e)
          {
          TextBox tb;
          foreach (DataRow row in dataSetselectactivities.Tables[0].Rows)
          {
          tb = new TextBox();
          tb.Text = row["colName"].ToString();

          TextBoxesHere.Controls.Add(tb);

          }
          }

          E 1 Reply Last reply
          0
          • M Michael Bookatz

            public void CreateTextBoxes(Object sender, EventArgs e)
            {
            TextBox tb;
            foreach (DataRow row in dataSetselectactivities.Tables[0].Rows)
            {
            tb = new TextBox();
            tb.Text = row["colName"].ToString();

            TextBoxesHere.Controls.Add(tb);

            }
            }

            E Offline
            E Offline
            eyeseetee
            wrote on last edited by
            #6

            thanks!! OK final question how do I add a break after each textbox i.e
            where abouts would I put it, I know the syntax shold be something like + "
            " but not sure which line to put it. also how can I call the code on page_load, at the moment it is called through a button thanks!

            N 1 Reply Last reply
            0
            • E eyeseetee

              thanks!! OK final question how do I add a break after each textbox i.e
              where abouts would I put it, I know the syntax shold be something like + "
              " but not sure which line to put it. also how can I call the code on page_load, at the moment it is called through a button thanks!

              N Offline
              N Offline
              Nirandas
              wrote on last edited by
              #7

              Cann't you use Repeater or DataGrid for this? That will be more simpler. Any way, you will have to create as many Literal Controls as there are textboxs and put it between textboxs to insert some text/html markup between them. You can take out the code inside the button's click and put it in a method and call it from both the page_load and the button_click.

              Nirandas, a developer from India. http://www.nirandas.com

              E 1 Reply Last reply
              0
              • N Nirandas

                Cann't you use Repeater or DataGrid for this? That will be more simpler. Any way, you will have to create as many Literal Controls as there are textboxs and put it between textboxs to insert some text/html markup between them. You can take out the code inside the button's click and put it in a method and call it from both the page_load and the button_click.

                Nirandas, a developer from India. http://www.nirandas.com

                E Offline
                E Offline
                eyeseetee
                wrote on last edited by
                #8

                ive tried putting
                after the tb.Text = row["fieldname"].ToString() + "
                "; but this doesnt work.

                modified on Tuesday, July 8, 2008 9:04 AM

                N 1 Reply Last reply
                0
                • E eyeseetee

                  ive tried putting
                  after the tb.Text = row["fieldname"].ToString() + "
                  "; but this doesnt work.

                  modified on Tuesday, July 8, 2008 9:04 AM

                  N Offline
                  N Offline
                  Nirandas
                  wrote on last edited by
                  #9

                  That would not work as your appending text to the textbox, and browser will not parse it while rendering. As I said, creating literal controls and adding them between textboxs is the way to go if you don't want to use repeater/Datagrid. Can you paste the code which you are using to add these textboxs to the page? Then I may be able to suggest the code to you.

                  Nirandas, a developer from India. http://www.nirandas.com

                  E 1 Reply Last reply
                  0
                  • N Nirandas

                    That would not work as your appending text to the textbox, and browser will not parse it while rendering. As I said, creating literal controls and adding them between textboxs is the way to go if you don't want to use repeater/Datagrid. Can you paste the code which you are using to add these textboxs to the page? Then I may be able to suggest the code to you.

                    Nirandas, a developer from India. http://www.nirandas.com

                    E Offline
                    E Offline
                    eyeseetee
                    wrote on last edited by
                    #10

                    foreach (DataRow row in dataSetselect.Tables[0].Rows) { lb = new Label(); tb = new TextBox(); lb.Text = row["field"].ToString(); tb.Text = row["field"].ToString(); TextBoxesHere.Controls.Add(lb); TextBoxesHere.Controls.Add(tb); } thanks it would need to go somewhere in there

                    N 1 Reply Last reply
                    0
                    • E eyeseetee

                      foreach (DataRow row in dataSetselect.Tables[0].Rows) { lb = new Label(); tb = new TextBox(); lb.Text = row["field"].ToString(); tb.Text = row["field"].ToString(); TextBoxesHere.Controls.Add(lb); TextBoxesHere.Controls.Add(tb); } thanks it would need to go somewhere in there

                      N Offline
                      N Offline
                      Nirandas
                      wrote on last edited by
                      #11

                      Change the line

                      lb.Text = row["field"].ToString();

                      to

                      lb.Text = "
                      " + row["field"].ToString();

                      This will add a line break before every label. HTH

                      Nirandas, a developer from India. http://www.nirandas.com

                      E 1 Reply Last reply
                      0
                      • N Nirandas

                        Change the line

                        lb.Text = row["field"].ToString();

                        to

                        lb.Text = "
                        " + row["field"].ToString();

                        This will add a line break before every label. HTH

                        Nirandas, a developer from India. http://www.nirandas.com

                        E Offline
                        E Offline
                        eyeseetee
                        wrote on last edited by
                        #12

                        it throws up an error: should it be like: lb.Text = " " + row["field"].ToString(); or lb.Text = "" + row["field"].ToString();

                        N 1 Reply Last reply
                        0
                        • E eyeseetee

                          it throws up an error: should it be like: lb.Text = " " + row["field"].ToString(); or lb.Text = "" + row["field"].ToString();

                          N Offline
                          N Offline
                          Nirandas
                          wrote on last edited by
                          #13

                          The complete statement should be in a single line. And the quotes should not be empty, it should contain <br /> The line will be:

                          lb.Text = "<br />" + row["field"].ToString();

                          I didn't formatted the code correctly last time, sorry. You can insert any html markup there, since you want a line break, use <br /> Goodluck.

                          Nirandas, a developer from India. http://www.nirandas.com

                          E 1 Reply Last reply
                          0
                          • N Nirandas

                            The complete statement should be in a single line. And the quotes should not be empty, it should contain <br /> The line will be:

                            lb.Text = "<br />" + row["field"].ToString();

                            I didn't formatted the code correctly last time, sorry. You can insert any html markup there, since you want a line break, use <br /> Goodluck.

                            Nirandas, a developer from India. http://www.nirandas.com

                            E Offline
                            E Offline
                            eyeseetee
                            wrote on last edited by
                            #14

                            No that doesn work either, all that does is put the
                            in the actual textbox as text. I need to add it afterwards but not sure of the syntax Thanks anyway

                            S 1 Reply Last reply
                            0
                            • E eyeseetee

                              I am trying to create a function which creates a certain amount of textboxes depending on how many are in the datatable. I have the following code so far: foreach (DataRow row in dataSetselectactivities.Tables[0].Rows) { TextBox tb = new TextBox(); tb.Visible = true; } but how would I add a new value to the textbox for each row, would it be something like tb.text += dttablevalue any help would be great thanks

                              N Offline
                              N Offline
                              Nirandas
                              wrote on last edited by
                              #15

                              I am pasting the complete code:

                              foreach (DataRow row in dataSetselect.Tables[0].Rows)
                              {
                              lb = new Label();
                              tb = new TextBox();
                              lb.Text = "<br />" + row["field"].ToString();
                              tb.Text = row["field"].ToString();
                              TextBoxesHere.Controls.Add(lb);
                              TextBoxesHere.Controls.Add(tb);
                              }

                              Is it the same code as what you are using?

                              Nirandas, a developer from India. http://www.nirandas.com

                              E 1 Reply Last reply
                              0
                              • E eyeseetee

                                No that doesn work either, all that does is put the
                                in the actual textbox as text. I need to add it afterwards but not sure of the syntax Thanks anyway

                                S Offline
                                S Offline
                                Sherin Iranimose
                                wrote on last edited by
                                #16

                                Instead of label you can use a LiteralControl LiteralControl lit=new LiteralControl(); lit.Text="<br />" Textboxhere.controls.add(lit);

                                EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.

                                modified on Tuesday, July 8, 2008 11:04 AM

                                1 Reply Last reply
                                0
                                • N Nirandas

                                  I am pasting the complete code:

                                  foreach (DataRow row in dataSetselect.Tables[0].Rows)
                                  {
                                  lb = new Label();
                                  tb = new TextBox();
                                  lb.Text = "<br />" + row["field"].ToString();
                                  tb.Text = row["field"].ToString();
                                  TextBoxesHere.Controls.Add(lb);
                                  TextBoxesHere.Controls.Add(tb);
                                  }

                                  Is it the same code as what you are using?

                                  Nirandas, a developer from India. http://www.nirandas.com

                                  E Offline
                                  E Offline
                                  eyeseetee
                                  wrote on last edited by
                                  #17

                                  OK I got it now, I was putting the break in the textbox not the label!! :doh: Thanks for your help in this

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups