Accessing User Controls

wtbonnell

Freshman
Joined
Oct 6, 2003
Messages
32
I have a user control that has the following content:

Code:
<td id="td_Name" runat="server" bgcolor="#000000">some info here </td>

I place this user control on many .aspx pages and I can view it fine, however, I want to be able to dynamically change the bgcolor of the cell on certain .aspx pages within the code behind. Any ideas?


Thanks for your help...
 
Make your /td/ public available (in user control):
public System.Web.UI.HtmlControls.HtmlTableCell td_Name;

than in page make your user control available for manipulation (in webform):
protected WebUserControl1 WebUserControl11;

and than...
WebUserControl11.td_Name.BgColor = "#FF0000";

Greets
Adam


wtbonnell said:
I have a user control that has the following content:

Code:
<td id="td_Name" runat="server" bgcolor="#000000">some info here </td>

I place this user control on many .aspx pages and I can view it fine, however, I want to be able to dynamically change the bgcolor of the cell on certain .aspx pages within the code behind. Any ideas?


Thanks for your help...
 
Thanks for your help. I tried exactly as you said, but when I ran it I got the following error:

Object reference not set to an instance of an object.

And the line of code that it was highlighting was
WebUserControl11.td_Name.BgColor = "#FF0000";

All the objects were appearing in the Inteli-Sense as I was typing so it appeared as if it was going to work but then I got this null reference exception when I ran it.

Any ideas?

Thanks again...




hrabia said:
Make your /td/ public available (in user control):
public System.Web.UI.HtmlControls.HtmlTableCell td_Name;

than in page make your user control available for manipulation (in webform):
protected WebUserControl1 WebUserControl11;

and than...
WebUserControl11.td_Name.BgColor = "#FF0000";

Greets
Adam
 
The actual user control is named: "adminTabs.ascx". The code behind within the User Control looks like:

Code:
public class adminTabs : System.Web.UI.UserControl
	{
		public System.Web.UI.HtmlControls.HtmlTableCell td_Services;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

	}


The actual admin.aspx.cs page code behind looks like:

Code:
public class admin : Page
	{
		protected adminTabs adminTabs1;

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				try
				{
				      adminTabs1.td_Services.BgColor = "#FF0000";
                                }
				catch(Exception excep)
				{
					throw new Exception("Exception in Page_Load.",excep);
				}
			}
		}

	}

Does this help to find my error?

Thanks...


hrabia said:
What's the id of your user control in page (in html)? Your control has probably other id than "WebUserControl11".
 
Show your html code. Codebehind is ok.

Are you sure that your controls have this ids: td_Services and adminTabs1?

In html something like that:
Code:
<uc1:WebUserControl1 [b]id="WebUserControl11"[/b]runat="server"></uc1:WebUserControl1>
 
Back
Top