473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Programmatically adding a user control and setting values

I'm having trouble programmatically adding a user control and then
setting some of it's server controls.

I add the user control to the code behind and add it to a placeholder:

protected void Page_Load(object sender, EventArgs e)
{
UserControls_WebUserControl myControl = new
UserControls_WebUserControl();
PlaceHolder1.Controls.Add(web);
}

And I make a reference to it on the webpage:

<%@ Register Src="~/UserControls/WebUserControl.ascx"
TagName="myUserControl" TagPrefix="CustomControl" %>

All good so far?

For the user contorl, to simplify it the the Page_Load of the user
control is trying to set a hyperlink text (as later on a will
hopefully pass a load of values to a constructor of the user control)
like so:

HyperLink1.Text = "Hi I'm a hyperlink";

I'm expecting the obvious (well obvious to me anyways). For one
hyperlink with the text "Hi I'm a hyperlink".

instead when I run this I get the error:

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

When debugging the hyperlink I've placed on the user control doesn't
seem to exist! Could any kind programmer tell this noobie what he's
doing wrong. Thanks in advance.

Alun
Jan 17 '08 #1
6 10090


<al****@gmail.comwrote in message
news:f5**********************************@v17g2000 hsa.googlegroups.com...
I'm having trouble programmatically adding a user control and then
setting some of it's server controls.

I add the user control to the code behind and add it to a placeholder:

protected void Page_Load(object sender, EventArgs e)
{
UserControls_WebUserControl myControl = new
UserControls_WebUserControl();
PlaceHolder1.Controls.Add(web);
}

And I make a reference to it on the webpage:

<%@ Register Src="~/UserControls/WebUserControl.ascx"
TagName="myUserControl" TagPrefix="CustomControl" %>

All good so far?

For the user contorl, to simplify it the the Page_Load of the user
control is trying to set a hyperlink text (as later on a will
hopefully pass a load of values to a constructor of the user control)
like so:

HyperLink1.Text = "Hi I'm a hyperlink";

I'm expecting the obvious (well obvious to me anyways). For one
hyperlink with the text "Hi I'm a hyperlink".

instead when I run this I get the error:

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

When debugging the hyperlink I've placed on the user control doesn't
seem to exist! Could any kind programmer tell this noobie what he's
doing wrong. Thanks in advance.

Alun
Where is HyperLink1 Control ??? In the page or in the UserControl ???

Try with FindControl within the scope of the container of the Hyperlink
(page or control).

HTH
--
Gianluca Gravina
http://blogs.ugidotnet.org/thinkingingrava

Jan 17 '08 #2
The server contorl HyperLink1 is on the user control .ascx page.

Thanks for the suggestion Gianluca. In the user contorl codebehind I
tried your suggestion out:

protected void Page_Load(object sender, EventArgs e)
{
((HyperLink)(FindControl("HyperLink1"))).Text = "I am the
hyperlink";
}

But I still get a System.NullReferenceException for the hyperlink. Any
other ideas?

Jan 17 '08 #3


<al****@gmail.comwrote in message
news:b5**********************************@u10g2000 prn.googlegroups.com...
The server contorl HyperLink1 is on the user control .ascx page.

Thanks for the suggestion Gianluca. In the user contorl codebehind I
tried your suggestion out:

protected void Page_Load(object sender, EventArgs e)
{
((HyperLink)(FindControl("HyperLink1"))).Text = "I am the
hyperlink";
}

But I still get a System.NullReferenceException for the hyperlink. Any
other ideas?

Here is a working sample.

Code in the page Code Behind:

private Control c;

protected void Page_Init(object sender, EventArgs e)
{
c = LoadControl("WebUserControl1.ascx");
plcHolder.Controls.Add(c);
}

protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1 wc = (WebUserControl1) c;
((TextBox) wc.FindControl("controlTextBox")).Text = "Test";
}

In the ascx there's a textbox with id="controlTextBox".

HTH
--
Gianluca Gravina
http://blogs.ugidotnet.org/thinkingingrava

Jan 17 '08 #4
This sort of thing requires a familiarity with the ASP.Net Control Execution
LifeCycle, as the Page (which is a Control) and all the Controls in it, host
other Controls, and there is a cascade of events which occurs when the class
is loaded. See the following:

http://www.digcode.com/default.aspx?...1-584539cbb6bf

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

<al****@gmail.comwrote in message
news:f5**********************************@v17g2000 hsa.googlegroups.com...
I'm having trouble programmatically adding a user control and then
setting some of it's server controls.

I add the user control to the code behind and add it to a placeholder:

protected void Page_Load(object sender, EventArgs e)
{
UserControls_WebUserControl myControl = new
UserControls_WebUserControl();
PlaceHolder1.Controls.Add(web);
}

And I make a reference to it on the webpage:

<%@ Register Src="~/UserControls/WebUserControl.ascx"
TagName="myUserControl" TagPrefix="CustomControl" %>

All good so far?

For the user contorl, to simplify it the the Page_Load of the user
control is trying to set a hyperlink text (as later on a will
hopefully pass a load of values to a constructor of the user control)
like so:

HyperLink1.Text = "Hi I'm a hyperlink";

I'm expecting the obvious (well obvious to me anyways). For one
hyperlink with the text "Hi I'm a hyperlink".

instead when I run this I get the error:

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

When debugging the hyperlink I've placed on the user control doesn't
seem to exist! Could any kind programmer tell this noobie what he's
doing wrong. Thanks in advance.

Alun

Jan 17 '08 #5
Thanks for the working code.

From that I was able to figure out what I was doing wrong. I was
trying to instantate the user contorl like a server control like:

UserControls_WebUserControl myControl = new
UserControls_WebUserControl();

where as like you have done, I should have been using the
LoadControl() method.

I stumbled accross this posting that confirmed the different
instanition methods:

http://groups.google.com/group/micro...3f908c1cae7af0

Just as a side note for anyone interested in passing parameters to a
user control constructor, there's a great article here that sorts it
out:

http://blah.winsmarts.com/2006/05/20...aspx?postID=12

Thanks grava for your help. My problems are now solved! At least for
the next hour or so ;)
Jan 17 '08 #6
Howdy,

Exception you get is because UserControl are treated differently (you can't
use constructor to instantiate), use LoadControl method instead (then, all
references to your controls within usercontrol will be properly intantiated):

UserControls_WebUserControl myControl =
(UserControls_WebUserControl)
this.LoadControl("~/UserControls/WebUserControl.ascx")
PlaceHolder1.Controls.Add(myControl);

In addition move the code to Page_Init event because the view state.

Hope this helps
--
Milosz
"al****@gmail.com" wrote:
I'm having trouble programmatically adding a user control and then
setting some of it's server controls.

I add the user control to the code behind and add it to a placeholder:

protected void Page_Load(object sender, EventArgs e)
{
UserControls_WebUserControl myControl = new
UserControls_WebUserControl();
PlaceHolder1.Controls.Add(web);
}

And I make a reference to it on the webpage:

<%@ Register Src="~/UserControls/WebUserControl.ascx"
TagName="myUserControl" TagPrefix="CustomControl" %>

All good so far?

For the user contorl, to simplify it the the Page_Load of the user
control is trying to set a hyperlink text (as later on a will
hopefully pass a load of values to a constructor of the user control)
like so:

HyperLink1.Text = "Hi I'm a hyperlink";

I'm expecting the obvious (well obvious to me anyways). For one
hyperlink with the text "Hi I'm a hyperlink".

instead when I run this I get the error:

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

When debugging the hyperlink I've placed on the user control doesn't
seem to exist! Could any kind programmer tell this noobie what he's
doing wrong. Thanks in advance.

Alun
Jan 18 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have...
18
by: Julia Hu | last post by:
Hi, I have a datagrid, and in different rows I need to programmatically bind different type of controls and load data into these controls. For example,in the first row I need to bind data into a...
8
by: mark.norgate | last post by:
I've asked this question before, but still haven't solved it, so am asking again. I am programmatically adding a user control to the page in response to a button click. The user control consists...
3
by: Ken Fine | last post by:
I'm interested in programmatically manipulating groups of ASP.NET controls by type. Can someone suggest code for the following? Loop through, say, all label controls on a page, and assigning a...
4
by: Bob | last post by:
Hi, I'm working with VWD and i defined programmatically a button (in code-behind) with an ID. So I expect to see beside "Page Events" and "Form1" my button "b1" in order to use the Click event....
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
0
by: RKT | last post by:
I have a DataGridView bound to an MS Access table. This is a single- user application. When the User is adding or editing a row, the User may click on a Control elsewhere. That Control has context...
4
by: alun65 | last post by:
I'm attempting to programmatically build up some HTML in the code behind. Like so: // Create Hyperlink HyperLink link = new HyperLink(); link.NavigateUrl = "nice cat"; link.Text = "Cats...
3
by: Ken Fine | last post by:
I am using ASP.NET's CreateUserWizard control. I want to force the visitor to use a username and e-mail address that I am providing in programming, and I do not want the visitor to be able to edit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.