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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problems using findcontrol

Problems using findcontrol

Scheduled Pinned Locked Moved ASP.NET
helpcsharphtmlasp-netsysadmin
3 Posts 2 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.
  • J Offline
    J Offline
    jclaeson
    wrote on last edited by
    #1

    I'm having a problem finding my controls! In the asp.net page I've created a table using: <asp:Table id="tbl" runat="server"></asp:Table> that is filled dynamically based on user input. So, I create a number of rows and cells depending on how many the user requests. Here's how I'm dynamically creating the table: TableRow row3 = new TableRow(); cell1 = new TableCell(); cell2 = new TableCell(); cell1.Text = "![](\"images/up_select_image.gif\")"; TextBox title = new TextBox(); title.ID = "title"+i.ToString(); cell2.Controls.Add(title); row3.Cells.Add(cell1); row3.Cells.Add(cell2); tblMulti.Rows.Add(row3); I do this from 1 to some number n. Once the user submits the form, I need to find the controls and access the data in them. I've tried that doing this: for(int i = 1; i <= numImages; i++) { TextBox title = (TextBox)tblMulti.FindControl("title"+i.ToString()); status.Text = title.Text //status is a label used to test } The problem occurs at the line status.Text = ... The error I get is the standard Object Reference not set to an instance of an object. I've tried iterating through each control in the Table to print out the name of each id but when I do that I get nothing. I've also tried counting the rows and displaying them but again, I get nothing. The HTML is formed correctly if I view the source but it's like there are no rows in the table. Please help!! It's driving me nuts!!!!

    H 1 Reply Last reply
    0
    • J jclaeson

      I'm having a problem finding my controls! In the asp.net page I've created a table using: <asp:Table id="tbl" runat="server"></asp:Table> that is filled dynamically based on user input. So, I create a number of rows and cells depending on how many the user requests. Here's how I'm dynamically creating the table: TableRow row3 = new TableRow(); cell1 = new TableCell(); cell2 = new TableCell(); cell1.Text = "![](\"images/up_select_image.gif\")"; TextBox title = new TextBox(); title.ID = "title"+i.ToString(); cell2.Controls.Add(title); row3.Cells.Add(cell1); row3.Cells.Add(cell2); tblMulti.Rows.Add(row3); I do this from 1 to some number n. Once the user submits the form, I need to find the controls and access the data in them. I've tried that doing this: for(int i = 1; i <= numImages; i++) { TextBox title = (TextBox)tblMulti.FindControl("title"+i.ToString()); status.Text = title.Text //status is a label used to test } The problem occurs at the line status.Text = ... The error I get is the standard Object Reference not set to an instance of an object. I've tried iterating through each control in the Table to print out the name of each id but when I do that I get nothing. I've also tried counting the rows and displaying them but again, I get nothing. The HTML is formed correctly if I view the source but it's like there are no rows in the table. Please help!! It's driving me nuts!!!!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      For one, always check for null when you can't be sure if you're getting a value back. Also, you are aware that indices in .NET languages range from 0 to Count - 1? With the way you're doing it now, you'll skip the first row and cause an IndexOutOfRangeException on the last row. Where are you putting this code? Because HTTP - and hence ASP.NET - is not stateful, if you dynamically generate the table in the handler for a click event but don't enable the ViewState, the table will most likely not reflect those changes. Instead, enable the ViewState for the table and make sure you clear existing rows if necessary. If you don't, then when you re-submit the page form, those rows will no longer exist unless they are created again before you try accessing them. The other thing you must consider is the INamingContainer. While your example indicates that you're doing it correctly, keep in mind that the FindControl method searches the current naming container for the ID you're passing to FindControl. If you do this from a container higher in the control hierarchy, you must take the mangled ID ("control1_control2..._controlN") into account. The Control.ClientID reflects this name, just FYI.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      J 1 Reply Last reply
      0
      • H Heath Stewart

        For one, always check for null when you can't be sure if you're getting a value back. Also, you are aware that indices in .NET languages range from 0 to Count - 1? With the way you're doing it now, you'll skip the first row and cause an IndexOutOfRangeException on the last row. Where are you putting this code? Because HTTP - and hence ASP.NET - is not stateful, if you dynamically generate the table in the handler for a click event but don't enable the ViewState, the table will most likely not reflect those changes. Instead, enable the ViewState for the table and make sure you clear existing rows if necessary. If you don't, then when you re-submit the page form, those rows will no longer exist unless they are created again before you try accessing them. The other thing you must consider is the INamingContainer. While your example indicates that you're doing it correctly, keep in mind that the FindControl method searches the current naming container for the ID you're passing to FindControl. If you do this from a container higher in the control hierarchy, you must take the mangled ID ("control1_control2..._controlN") into account. The Control.ClientID reflects this name, just FYI.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        J Offline
        J Offline
        jclaeson
        wrote on last edited by
        #3

        I'll try enabling the viewstate this evening when I get home. I know I'm missing the first row, that's because there's a static header there so I don't care about that part. I wasn't thinking about the state... I just thought if I created the table dynamically on one postback, it'd be there on the next. Of course now I realize how this might not work. PS - thanks for the data caching article on Devhood!

        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