Dynamically creating array of controls

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

    #1

    Dynamically creating array of controls

    Anybody quickly replies it, would be a great help!

    I try to create dynamic array of textboxes control in ASP.NET(C#). I got the
    following error while I tried to assign value to ID property of dynamically
    created textbox. (look at my code please )

    Error: “ Object reference is not set to an instance of Object”

    totalRows – int variable which varies with number of records in SQL table

    Textbox[]txbox = new Textbox [totalRows];

    For (int i=0; i < totalRows; i++)
    {

    txbox[i].ID = “myTextbox” ; //Getting error here

    }

    Thanks,
  • [Alan Flores]

    #2
    RE: Dynamically creating array of controls

    an id is a unique identifier, it seems you are passing the same value to all
    of the controls.. try this..[color=blue]
    > For (int i=0; i < totalRows; i++)
    > {
    >
    > txbox[i].ID = “myTextbox” + i;
    >
    > }[/color]


    "Bhavin" wrote:
    [color=blue]
    > Anybody quickly replies it, would be a great help!
    >
    > I try to create dynamic array of textboxes control in ASP.NET(C#). I got the
    > following error while I tried to assign value to ID property of dynamically
    > created textbox. (look at my code please )
    >
    > Error: “ Object reference is not set to an instance of Object”
    >
    > totalRows – int variable which varies with number of records in SQL table
    >
    > Textbox[]txbox = new Textbox [totalRows];
    >
    > For (int i=0; i < totalRows; i++)
    > {
    >
    > txbox[i].ID = “myTextbox” ; //Getting error here
    >
    > }
    >
    > Thanks,[/color]

    Comment

    • Bhavin

      #3
      RE: Dynamically creating array of controls

      Do you know how can I capture values from dynamically created controls?

      "[Alan Flores]" wrote:
      [color=blue]
      > an id is a unique identifier, it seems you are passing the same value to all
      > of the controls.. try this..[color=green]
      > > For (int i=0; i < totalRows; i++)
      > > {
      > >
      > > txbox[i].ID = “myTextbox” + i;
      > >
      > > }[/color]
      >
      >
      > "Bhavin" wrote:
      >[color=green]
      > > Anybody quickly replies it, would be a great help!
      > >
      > > I try to create dynamic array of textboxes control in ASP.NET(C#). I got the
      > > following error while I tried to assign value to ID property of dynamically
      > > created textbox. (look at my code please )
      > >
      > > Error: “ Object reference is not set to an instance of Object”
      > >
      > > totalRows – int variable which varies with number of records in SQL table
      > >
      > > Textbox[]txbox = new Textbox [totalRows];
      > >
      > > For (int i=0; i < totalRows; i++)
      > > {
      > >
      > > txbox[i].ID = “myTextbox” ; //Getting error here
      > >
      > > }
      > >
      > > Thanks,[/color][/color]

      Comment

      • Angelos Karantzalis

        #4
        Re: Dynamically creating array of controls

        When you create an array of objects, the objects inside it are NOT
        initialized ! In other owrd, it's an array of null values mate ... change
        your code to this:

        Textbox[] txbox = new Textbox [totalRows];

        for(int i=0; i < totalRows; i++){
        txbox[i] = new Textbox();
        txbox[i].ID = "myTextbox" +i;
        }

        Angel
        O:]




        "Bhavin" <Bhavin@discuss ions.microsoft. com> wrote in message
        news:15E5AB6B-89B2-421A-BD36-B93AF5FBA317@mi crosoft.com...[color=blue]
        > Anybody quickly replies it, would be a great help!
        >
        > I try to create dynamic array of textboxes control in ASP.NET(C#). I got[/color]
        the[color=blue]
        > following error while I tried to assign value to ID property of[/color]
        dynamically[color=blue]
        > created textbox. (look at my code please )
        >
        > Error: " Object reference is not set to an instance of Object"
        >
        > totalRows - int variable which varies with number of records in SQL table
        >
        > Textbox[]txbox = new Textbox [totalRows];
        >
        > For (int i=0; i < totalRows; i++)
        > {
        >
        > txbox[i].ID = "myTextbox" ; //Getting error here
        >
        > }
        >
        > Thanks,[/color]


        Comment

        • Bhavin

          #5
          Re: Dynamically creating array of controls


          Thanks a lot Angel. It worked(But next problem)

          Textbox[] txbox = new Textbox [totalRows];

          for(int i=0; i < totalRows; i++){
          txbox[i] = new Textbox();
          txbox[i].ID = "myTextbox" +i;
          }

          I used above code in my page_load event. It worked. But once all dynamic
          empty textboxes got created I entered values in them. Now I want to capture
          those values using dynamic IDs of textboxes but those IDs no longer available
          after page_load event. I would like to capture all values from textboxes to
          INSERT data in SQL table.



          "Angelos Karantzalis" wrote:
          [color=blue]
          > When you create an array of objects, the objects inside it are NOT
          > initialized ! In other owrd, it's an array of null values mate ... change
          > your code to this:
          >
          > Textbox[] txbox = new Textbox [totalRows];
          >
          > for(int i=0; i < totalRows; i++){
          > txbox[i] = new Textbox();
          > txbox[i].ID = "myTextbox" +i;
          > }
          >
          > Angel
          > O:]
          >
          >
          >
          >
          > "Bhavin" <Bhavin@discuss ions.microsoft. com> wrote in message
          > news:15E5AB6B-89B2-421A-BD36-B93AF5FBA317@mi crosoft.com...[color=green]
          > > Anybody quickly replies it, would be a great help!
          > >
          > > I try to create dynamic array of textboxes control in ASP.NET(C#). I got[/color]
          > the[color=green]
          > > following error while I tried to assign value to ID property of[/color]
          > dynamically[color=green]
          > > created textbox. (look at my code please )
          > >
          > > Error: " Object reference is not set to an instance of Object"
          > >
          > > totalRows - int variable which varies with number of records in SQL table
          > >
          > > Textbox[]txbox = new Textbox [totalRows];
          > >
          > > For (int i=0; i < totalRows; i++)
          > > {
          > >
          > > txbox[i].ID = "myTextbox" ; //Getting error here
          > >
          > > }
          > >
          > > Thanks,[/color]
          >
          >
          >[/color]

          Comment

          • Angelos Karantzalis

            #6
            Re: Dynamically creating array of controls

            You could just store those controls in a member variable in your page class,
            and check on them, getting their .Text property.

            .... or, when you create them, you could add an event handler for their
            OnChange event, adn get the values that way

            .... or, you could even loop inside the page's controls collection and find
            them through there !

            ... or I could just keep writing. I think the first or second suggestion
            will work fine :]

            Angel
            O:]


            "Bhavin" <Bhavin@discuss ions.microsoft. com> wrote in message
            news:2265EDA7-2CD1-48B4-93AD-98EDE920E6E1@mi crosoft.com...[color=blue]
            >
            > Thanks a lot Angel. It worked(But next problem)
            >
            > Textbox[] txbox = new Textbox [totalRows];
            >
            > for(int i=0; i < totalRows; i++){
            > txbox[i] = new Textbox();
            > txbox[i].ID = "myTextbox" +i;
            > }
            >
            > I used above code in my page_load event. It worked. But once all dynamic
            > empty textboxes got created I entered values in them. Now I want to[/color]
            capture[color=blue]
            > those values using dynamic IDs of textboxes but those IDs no longer[/color]
            available[color=blue]
            > after page_load event. I would like to capture all values from textboxes[/color]
            to[color=blue]
            > INSERT data in SQL table.
            >
            >
            >
            > "Angelos Karantzalis" wrote:
            >[color=green]
            > > When you create an array of objects, the objects inside it are NOT
            > > initialized ! In other owrd, it's an array of null values mate ...[/color][/color]
            change[color=blue][color=green]
            > > your code to this:
            > >
            > > Textbox[] txbox = new Textbox [totalRows];
            > >
            > > for(int i=0; i < totalRows; i++){
            > > txbox[i] = new Textbox();
            > > txbox[i].ID = "myTextbox" +i;
            > > }
            > >
            > > Angel
            > > O:]
            > >
            > >
            > >
            > >
            > > "Bhavin" <Bhavin@discuss ions.microsoft. com> wrote in message
            > > news:15E5AB6B-89B2-421A-BD36-B93AF5FBA317@mi crosoft.com...[color=darkred]
            > > > Anybody quickly replies it, would be a great help!
            > > >
            > > > I try to create dynamic array of textboxes control in ASP.NET(C#). I[/color][/color][/color]
            got[color=blue][color=green]
            > > the[color=darkred]
            > > > following error while I tried to assign value to ID property of[/color]
            > > dynamically[color=darkred]
            > > > created textbox. (look at my code please )
            > > >
            > > > Error: " Object reference is not set to an instance of Object"
            > > >
            > > > totalRows - int variable which varies with number of records in SQL[/color][/color][/color]
            table[color=blue][color=green][color=darkred]
            > > >
            > > > Textbox[]txbox = new Textbox [totalRows];
            > > >
            > > > For (int i=0; i < totalRows; i++)
            > > > {
            > > >
            > > > txbox[i].ID = "myTextbox" ; //Getting error here
            > > >
            > > > }
            > > >
            > > > Thanks,[/color]
            > >
            > >
            > >[/color][/color]


            Comment

            • Dan

              #7
              Re: Dynamically creating array of controls

              Angelos - Can you translate into vb.net? - Can you direct me to a newsgroup
              that might if not?

              Thanks

              Dan

              "Angelos Karantzalis" wrote:
              [color=blue]
              > When you create an array of objects, the objects inside it are NOT
              > initialized ! In other owrd, it's an array of null values mate ... change
              > your code to this:
              >
              > Textbox[] txbox = new Textbox [totalRows];
              >
              > for(int i=0; i < totalRows; i++){
              > txbox[i] = new Textbox();
              > txbox[i].ID = "myTextbox" +i;
              > }
              >
              > Angel
              > O:]
              >
              >
              >
              >
              > "Bhavin" <Bhavin@discuss ions.microsoft. com> wrote in message
              > news:15E5AB6B-89B2-421A-BD36-B93AF5FBA317@mi crosoft.com...[color=green]
              > > Anybody quickly replies it, would be a great help!
              > >
              > > I try to create dynamic array of textboxes control in ASP.NET(C#). I got[/color]
              > the[color=green]
              > > following error while I tried to assign value to ID property of[/color]
              > dynamically[color=green]
              > > created textbox. (look at my code please )
              > >
              > > Error: " Object reference is not set to an instance of Object"
              > >
              > > totalRows - int variable which varies with number of records in SQL table
              > >
              > > Textbox[]txbox = new Textbox [totalRows];
              > >
              > > For (int i=0; i < totalRows; i++)
              > > {
              > >
              > > txbox[i].ID = "myTextbox" ; //Getting error here
              > >
              > > }
              > >
              > > Thanks,[/color]
              >
              >
              >[/color]

              Comment

              Working...