FindControl is null

G

Guest

In the below example could someone please assist in why the findcontrol
method is returning null. Is there are easy way to determine where in the
page heirarchy a control exists - I currently use the debugger and after I am
done drilling down 10 levels deep I still can't easily tell where the control
lives...

====CODE BEHIND====
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 playground
{
public class tabs : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblFolders;

private void Page_Load(object sender, System.EventArgs e)
{

// build html on fly
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i=0;i<5;i++)
{
string id = "v2" + i.ToString().Trim();
sb.Append("<td name=\"" + id + "\" id=\"" + id + "\" bgcolor=\"#00e0\"
width=\"10\" runat=\"server\"><a href=\"tabs.aspx?folder=" + id +
"\">Folder</a></td>");
sb.Append("<td width=\"1\"></td>");
}

lblFolders.Text=sb.ToString();

if(Request.Params["folder"] != null)
{
highLightTab(Request.Params["folder"].ToString().Trim());
}
}

private void highLightTab(string controlID)
{
if (controlID != null)
{
// this does not find the control
HtmlTableCell td = (HtmlTableCell)Page.FindControl(controlID);
try
{
td.BgColor="yellow";
}
catch
{
}
}
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

====HTML .ASPX====
<%@ Page language="c#" Codebehind="tabs.aspx.cs" AutoEventWireup="false"
Inherits="playground.tabs" %>
<HTML>
<HEAD>
<title>tabs</title>
</HEAD>
<body>

<form id="tabs" method="post" runat="server">
<table border="1">
<tr>
<asp:Label ID="lblFolders" Runat="server"></asp:Label>
</tr>
</table>
</form>
</body>
</HTML>
 
G

Guest

The FindControl Method is looking for a page element whose "id" property is
set to the string value given. The value given is apparently not one that is
found. The requested page element must support the "id" property.
 
S

Scott Allen

FindControl will only find server side controls (controls with a
runat="server" attribute).

HTH,
 
G

Guest

Scott,

All my controls are server side....

Scott Allen said:
FindControl will only find server side controls (controls with a
runat="server" attribute).

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/


In the below example could someone please assist in why the findcontrol
method is returning null. Is there are easy way to determine where in the
page heirarchy a control exists - I currently use the debugger and after I am
done drilling down 10 levels deep I still can't easily tell where the control
lives...

====CODE BEHIND====
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 playground
{
public class tabs : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblFolders;

private void Page_Load(object sender, System.EventArgs e)
{

// build html on fly
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i=0;i<5;i++)
{
string id = "v2" + i.ToString().Trim();
sb.Append("<td name=\"" + id + "\" id=\"" + id + "\" bgcolor=\"#00e0\"
width=\"10\" runat=\"server\"><a href=\"tabs.aspx?folder=" + id +
"\">Folder</a></td>");
sb.Append("<td width=\"1\"></td>");
}

lblFolders.Text=sb.ToString();

if(Request.Params["folder"] != null)
{
highLightTab(Request.Params["folder"].ToString().Trim());
}
}

private void highLightTab(string controlID)
{
if (controlID != null)
{
// this does not find the control
HtmlTableCell td = (HtmlTableCell)Page.FindControl(controlID);
try
{
td.BgColor="yellow";
}
catch
{
}
}
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

====HTML .ASPX====
<%@ Page language="c#" Codebehind="tabs.aspx.cs" AutoEventWireup="false"
Inherits="playground.tabs" %>
<HTML>
<HEAD>
<title>tabs</title>
</HEAD>
<body>

<form id="tabs" method="post" runat="server">
<table border="1">
<tr>
<asp:Label ID="lblFolders" Runat="server"></asp:Label>
</tr>
</table>
</form>
</body>
</HTML>
 
M

Martin Dechev

Hi,

Setting a label's Text to some html does not add a control in the control
collection of the page. If you want to add controls - change your label with
placeholder and in the loop do something like this:

for(int i=0;i<5;i++)
{
string id = string.Format("v2{0}", i);
HtmlAnchor a = new HtmlAnchor();
a.InnerText = "Folder";
a.HRef = "tabs.aspx?folder=" + id;
HtmlTableCell td = new HtmlTableCell();
td.ID = id;
td.BgColor = "#00e0";
td.Width = "10"
td.Controls.Add(a)
placeHolder1.Controls.Add(td)
}

Hope this helps
Martin Dechev
ASP.NET MVP
Chris Fink said:
In the below example could someone please assist in why the findcontrol
method is returning null. Is there are easy way to determine where in the
page heirarchy a control exists - I currently use the debugger and after I am
done drilling down 10 levels deep I still can't easily tell where the control
lives...

====CODE BEHIND====
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 playground
{
public class tabs : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblFolders;

private void Page_Load(object sender, System.EventArgs e)
{

// build html on fly
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i=0;i<5;i++)
{
string id = "v2" + i.ToString().Trim();
sb.Append("<td name=\"" + id + "\" id=\"" + id + "\" bgcolor=\"#00e0\"
width=\"10\" runat=\"server\"><a href=\"tabs.aspx?folder=" + id +
"\">Folder</a></td>");
sb.Append("<td width=\"1\"></td>");
}

lblFolders.Text=sb.ToString();

if(Request.Params["folder"] != null)
{
highLightTab(Request.Params["folder"].ToString().Trim());
}
}

private void highLightTab(string controlID)
{
if (controlID != null)
{
// this does not find the control
HtmlTableCell td = (HtmlTableCell)Page.FindControl(controlID);
try
{
td.BgColor="yellow";
}
catch
{
}
}
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

====HTML .ASPX====
<%@ Page language="c#" Codebehind="tabs.aspx.cs" AutoEventWireup="false"
Inherits="playground.tabs" %>
<HTML>
<HEAD>
<title>tabs</title>
</HEAD>
<body>

<form id="tabs" method="post" runat="server">
<table border="1">
<tr>
<asp:Label ID="lblFolders" Runat="server"></asp:Label>
</tr>
</table>
</form>
</body>
</HTML>
 
B

bruce barker

your dynamic html code does not build any server controls (its just text in
a label), so there is no control to find.

-- bruce (sqlwork.com)
 
S

Scott Allen

Hi Chris:

Sorry, I wasn't as clear about this as the others were, but the way
you are building the control doesn't allow it to be instantiated
serverside.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Scott,

All my controls are server side....

Scott Allen said:
FindControl will only find server side controls (controls with a
runat="server" attribute).

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/


In the below example could someone please assist in why the findcontrol
method is returning null. Is there are easy way to determine where in the
page heirarchy a control exists - I currently use the debugger and after I am
done drilling down 10 levels deep I still can't easily tell where the control
lives...

====CODE BEHIND====
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 playground
{
public class tabs : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblFolders;

private void Page_Load(object sender, System.EventArgs e)
{

// build html on fly
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i=0;i<5;i++)
{
string id = "v2" + i.ToString().Trim();
sb.Append("<td name=\"" + id + "\" id=\"" + id + "\" bgcolor=\"#00e0\"
width=\"10\" runat=\"server\"><a href=\"tabs.aspx?folder=" + id +
"\">Folder</a></td>");
sb.Append("<td width=\"1\"></td>");
}

lblFolders.Text=sb.ToString();

if(Request.Params["folder"] != null)
{
highLightTab(Request.Params["folder"].ToString().Trim());
}
}

private void highLightTab(string controlID)
{
if (controlID != null)
{
// this does not find the control
HtmlTableCell td = (HtmlTableCell)Page.FindControl(controlID);
try
{
td.BgColor="yellow";
}
catch
{
}
}
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

====HTML .ASPX====
<%@ Page language="c#" Codebehind="tabs.aspx.cs" AutoEventWireup="false"
Inherits="playground.tabs" %>
<HTML>
<HEAD>
<title>tabs</title>
</HEAD>
<body>

<form id="tabs" method="post" runat="server">
<table border="1">
<tr>
<asp:Label ID="lblFolders" Runat="server"></asp:Label>
</tr>
</table>
</form>
</body>
</HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top