Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2010, 08:41 AM
Registered User
 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Nested controls in UserControl return null (FindControl)

Hi everyone,
I have a very weird issue. I have a UserControl that has some controls inside. I want to refer those controls after, in another postback. But when I try to get them the ".Controls" property of my controls returns null.
I'm working on vs2008
Here is the sample code:
Code:
	public partial class MyUserControl : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			foreach (Control control in this.Controls)
			{
				Response.Write(control.ClientID);
			}
		}

		private void MyTable()
		{
			Table table = new Table();

			TableRow row = new TableRow();
			TableCell cell = new TableCell();

			CheckBox check = new CheckBox();
			check.ID = "theId";
			check.Text = "My Check";
			check.AutoPostBack = true;
			cell.Controls.Add(check);
			row.Cells.Add(cell);

			check = new CheckBox();
			check.ID = "theOther";
			check.AutoPostBack = true;
			check.Text = "My Other Check";

			cell = new TableCell();
			cell.Controls.Add(check);
			row.Cells.Add(cell);

			table.Rows.Add(row);
			this.Controls.Add(table);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			MyTable();
			base.Render(writer);
		}
	}
and in the Default.aspx page is something like:
HTML Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.cs" Inherits="Tester.Default" %>
<%@ Register TagPrefix="uc1" TagName="MyControl" Src="~/MyUserControl.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Unbenannte Seite</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<uc1:MyControl ID="MyControlInstance" runat="server" />
    </div>
    </form>
</body>
</html>
Idk if I'm lost in somepart of the asp.net lifecycle. But this situation is making me crazy. Any help would be very grateful.
 
Old September 6th, 2010, 06:46 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Render fires after Load, so your table has not been created yet when you loop over the controls.
Could that be the issue?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old September 6th, 2010, 06:56 AM
Registered User
 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Yes, It was the asp lifecycle

Yes, you're right:
Here I post some links that helped me to solve the issue.
http://forums.asp.net/p/1597300/4054554.aspx
http://stackoverflow.com/questions/3...l-returns-null

Thanks for your response.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored Procedure return value is NULL kshort ASP.NET 2.0 Basics 16 March 14th, 2017 12:45 PM
FindControl in nested repeater chrisk_47 .NET Framework 2.0 0 February 6th, 2007 10:45 PM
Null Return value ITSPRO SQL Server ASP 1 July 27th, 2006 02:53 PM
Reporting Services - Return Null using '%' gschmidt SQL Server 2000 2 May 26th, 2006 11:05 AM
e.Item.FindControl("DropdownId") Returning Null anup_daware .NET Framework 1.x 1 March 27th, 2006 03:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.