|
-
Jan 19th, 2003, 10:08 PM
#1
Thread Starter
Fanatic Member
[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:
For i = 0 To lstColumns.Items.Count - 1
Dim tRow As New TableRow()
Dim tCell1 As New TableCell()
Dim newTextBox As New TextBox()
newTextBox.ID = "txtDG_" & i
newTextBox.Text = "Blah"
tCell1.Controls.Add(newTextBox)
tRow.Cells.Add(tCell1)
tblEdit.Rows.Add(tRow)
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:
For i = 0 to lstColumns.Items.Count - 1
Response.Write(CType(FindControl("txtDG_" & i), TextBox).Text)
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.
-
Jan 19th, 2003, 10:31 PM
#2
Thread Starter
Fanatic Member
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!
-
Jan 19th, 2003, 11:21 PM
#3
Thread Starter
Fanatic Member
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
-
Jan 22nd, 2003, 06:49 PM
#4
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)
-
Apr 9th, 2003, 12:29 PM
#5
Lively Member
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 
-
Apr 9th, 2003, 11:03 PM
#6
Hyperactive Member
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:
<%@ Page language="c#"
Codebehind="Dynamic.aspx.cs"
AutoEventWireup="false"
Inherits="CSharp.DynamicControls.Dynamic" %>
<html>
<body>
<form runat="server">
<asp:Button
Runat="server"
ID="btnPrintValues"
OnClick="btnPrintValues_Click"
Text="Print Values"/>
<asp:Button
Runat="server"
ID="btnPostBack"
OnClick="btnPostBack_Click"
Text="Post Back"/>
<br/>
<asp:Table
Runat="server"
ID="tblEdit"/>
</form>
</body>
</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 sender, EventArgs e)
{
//do nothing just postback to show the values have persisted.
}
protected void btnPrintValues_Click(object sender, EventArgs 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|