Results 1 to 6 of 6

Thread: [Resolved] FindControl() to find a Programmatically Added Control?

  1. #1

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551

    [Resolved] FindControl() to find a Programmatically Added Control?

    Hey guys, I've got a page that dynamically adds textboxes to a page. well, i sort of do it in a loop... i have i, and for whatever i contains, i add controls like this:

    VB Code:
    1. For i = 0 To lstColumns.Items.Count - 1
    2.  
    3.             Dim tRow As New TableRow()
    4.             Dim tCell1 As New TableCell()
    5.            
    6.             Dim newTextBox As New TextBox()
    7.             newTextBox.ID = "txtDG_" & i
    8.             newTextBox.Text = "Blah"
    9.  
    10.             tCell1.Controls.Add(newTextBox)
    11.  
    12.             tRow.Cells.Add(tCell1)
    13.        
    14.             tblEdit.Rows.Add(tRow)
    15. Next

    Now, this works peachy, and my textboxes all appear, and it's just great, however, i do need to later retreive their values... so lets say i do something like this:

    VB Code:
    1. For i = 0 to lstColumns.Items.Count - 1
    2.            Response.Write(CType(FindControl("txtDG_" & i), TextBox).Text)
    3. Next

    This should work, should it not? At least one would think.. neways, here's the error i'm getting:

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 180:
    Line 181: For i = 0 To lstColumns.Items.Count - 1
    Line 182: Response.Write(CType(FindControl("txtDG_" & i), TextBox).Text)
    Line 183: Next
    Line 184:
    Last edited by Redth; Jan 19th, 2003 at 11:21 PM.

  2. #2

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    Well, i think i know what the problem is... It seems as though dynamically added controls don't persist in a page on postback...

    but, i don't see another way around this.. surely there is someway to make these values persist, no?

    appreciate the help!

  3. #3

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    as usual, i fixed my own problem

    All i did was grab the info from the Request.Form() object, and i found the values were passed that way...

    i didn't really need the boxes to persist, i just needed the info, so all worked out well

  4. #4
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    By the way, runtime created controls do not persist.

    I spent probably a whole week trying to get them to persist (I program ASP.NET as my job) and I could not. I eventually added everything to the Web Page myself. (Long, arduous process)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  5. #5
    Lively Member
    Join Date
    Dec 2001
    Location
    vienna, austria
    Posts
    118

    Thumbs up

    redth,

    thanks for this post, i ran into the same problem, did not find a solution, but you helped me out by also posting the solution

    thanks again,
    big nell
    visit me on www.big-nell.com to find out the truth

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    I spent quite a bit of time trying to figure this one out, then a post on a microsoft newsgroup gave me the info to complete a solution for a project i was working on. The basic concept is to recreate the controls before their PostBack Change events fire(which are the events that update the control values if they have changed from postback to postback). The initial post in this thread is actually an easy one cuz of the loop. It can get quite a bit more difficult when the user needs to be able to add and remove more complicated UserControls at runtime, as well as save and load the control data to and from a database.

    Here's the aspx page i used for a test:
    VB Code:
    1. <%@ Page language="c#"
    2.     Codebehind="Dynamic.aspx.cs"
    3.     AutoEventWireup="false"
    4.     Inherits="CSharp.DynamicControls.Dynamic" %>
    5. <html>
    6.     <body>
    7.         <form runat="server">
    8.             <asp:Button
    9.                 Runat="server"
    10.                 ID="btnPrintValues"
    11.                 OnClick="btnPrintValues_Click"
    12.                 Text="Print Values"/>
    13.             <asp:Button
    14.                 Runat="server"
    15.                 ID="btnPostBack"
    16.                 OnClick="btnPostBack_Click"
    17.                 Text="Post Back"/>
    18.             <br/>
    19.             <asp:Table
    20.                 Runat="server"
    21.                 ID="tblEdit"/>
    22.         </form>
    23.     </body>
    24. </html>
    And here's the code behind(really the only important thing to note is that the table is being recreated every time in the OnLoad):
    PHP Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace 
    CSharp.DynamicControls
    {
        public class 
    Dynamic System.Web.UI.Page
        
    {
            public 
    System.Web.UI.WebControls.Button btnPostBack;
            public 
    System.Web.UI.WebControls.Button btnPrintValues;
            public 
    System.Web.UI.WebControls.Table tblEdit;
            
            protected 
    void btnPostBack_Click(object senderEventArgs e)
            {
                
    //do nothing just postback to show the values have persisted.
            
    }
            protected 
    void btnPrintValues_Click(object senderEventArgs e)
            {
                for (
    int index 0;index 10;index++)
                {
                    
    TextBox tb = (TextBox)FindControl("txtDG_" index.ToString());
                    
    Response.Write(tb.Text "<br/>");
                }
            }
            private 
    void loadTable()
            {
                for(
    int index 0;index 10;index++)
                {
                    
    TextBox tb = new TextBox();
                    
    tb.ID "txtDG_" index;
                    
    tb.Text "Blah";
                    
    TableCell td = new TableCell();
                    
    td.Controls.Add(tb);
                    
    TableRow tr = new TableRow();
                    
    tr.Cells.Add(td);
                    
    tblEdit.Rows.Add(tr);
                }
            }
            
    override protected void OnLoad(EventArgs e)
            {
                
    loadTable();
            }
            
    override protected void OnInit(EventArgs e)
            {
                
    base.OnInit(e);
            }        
        }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width