Newbie: Adding a User Control Problems

G

Gummy

Hello,

I am trying to create a user control that has a label and two radio buttons
for a survey. The idea is to populate a repeater (or datagrid, haven't
figured out the best way to do this) with the user control.

What will happen is it will read the database with a list of questions, each
question will populate the label and the user will click on either the Yes
or No radio buttons.

I am having problems getting the ASPX page and the ASCX to talk to each
other. At this point I am just trying to keep it simple as with the
following example:

I placed the YesNo.ascx on the ASPX page and added the declaration of:
Protected yno as New YesNo
at the top of the ASPX.

Then in the Page_Load I just have:
yno.SetName() = "a"

In the ASCX I added the following property:

Public Property SetName() as String

Get

Return lblName.Text

End Get

Set(ByVal Value as String)

lblName.Text = Value

End Set

End Property



When I run the page I get the follwing error: "Object reference not set to
an istance of an object" on the line:

lblName.Text = Value



What am I doing wrong? I assume it is a simple mistake.





Thanks for the help.



-Rob
 
G

Gummy

I think...

On the ASCX, I have a label with the name of lblName. But nowhere, in either
the ASCX or the ASPX did I acutally declare it. Am I supposed to declare it?
And if so, how?

In the codebehind for the ASCX, the following code was placed there
automatically (I assume)
Protected WithEvents lblName as System.Web.UI.WebControls.Label
 
K

Kevin Spencer

In the codebehind for the ASCX, the following code was placed there
automatically (I assume)
Protected WithEvents lblName as System.Web.UI.WebControls.Label

That IS a declaration. Not the access modifier: Protected. This means that
it is not accessible from outside the UserControl class. However, that is
not a problem. You simply create a public property that works with it.
Example:

Public ReadOnly Property NameLabel As System.Web.UI.WebControls.Label
Get
Return lblName
End Get
End Property

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
B

bill

My code is identical in structure to that of Gummy, and I have the exact
same problem.

This occurs even when the aspx instantiates an object variable and calls a
public property or procedure in the ascx, as Kevin Spencer suggested. The
property or procedure attempts to set the Text property of a label on the
ascx, and the error occurs.

I read one article on a newsgroup suggesting that when the object variable
for the ascx is declared in the aspx ("Protected yno as New YesNo" in
Gummy's code), the object variable creates a new instance of the ascx, which
is not the same as the instance of the ascx which is loaded.
 
G

Gummy

Kevin,

Thank you for the information.

I was wondering if you could be a little more specific...

What Property to you have to create in the ASCX to Set and Get the label
text, and how, in the ASPX would you Set and Get that text?

Thank you for the help.
 
G

Guest

Hmmm... I did the following:
control.ascx
--html
<asp:TextBox id=txtTest runat=server></asp:TextBox>
--codebehind
Protected WithEvents txtTest As System.Web.UI.WebControls.TextBox
Public Property GetTextValue() As String
Get
Return txtTest.Text
End Get
Set(ByVal Value As String)
txtTest.Text = Value
End Set
End Property


page.aspx
--html
<%@ Register TagPrefix="uc1" TagName="mycontrol" Src="mycontrol.ascx" %>
<uc1:mycontrol id=mycontrol1 runat="server"></uc1:mycontrol>
--codebehind
Protected mycontrol1 As New mycontrol
--in page_load
mycontrol1.GetTextValue = "testing 1234"


This worked perfectly.

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com
 
K

Kevin Spencer

Hi Gummy,

I gave you an example. In the example, the property name is NameLabel.

Assuming that your ASCX is named "UserControl1" in the page (for example),
you would set the Text property thusly:

UserControl1.NameLabel.Text="Some text."

To get the Text property:

Dim s As String = UserControl1.NameLabel.Text

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
G

Gummy

Kevin & Curt,



Thanks so much for the great information and all the help. I am getting
really close, but I'm not there yet.



Here's what I've done:



1.. Created a whole new project.
2.. Created YesNo.ascx
3.. Add a textbox, txtYesNo, to YesNo.ascx
4.. Add the following property to the YesNo.ascx
Public Property GetTextValue() As String

Get

Return txtYesNo.Text

End Get

Set(ByVal Value As String)

txtYesNo.Text = Value

End Set

End Property



5.. Created WebForm1.aspx
6.. Drag the YesNo.ascx onto WebForm1.aspx
7.. Add the following declaration on the WebForm1.aspx
Protected yno As New YesNo
8.. In the Page_Load on WebForm1.aspx added this:
yno.GetTextValue = "Enter Text"
9.. When I run the page I get a "Object reference not set to an instance
of an object." error on this line:
txtYesNo.Text = Value



For fun, I removed the code from the WebForm1.aspx and in the code-behind
for the YesNo.ascx on its Page_Load I added:

txtYesNo.Text = "Enter Text Here"

And that works just fine.



My naïve guess is that I am not instantiating the YesNo.ascx somehow. I don't
know if I even need to, but I can't figure out why there is no Object
reference.



Thanks again for all your help. I hope to be able to return the favor
someday.
 
B

bill

I have exactly duplicated Curt's code (instantiating an object variable for
the user control), word for word , and also used Kevin's approach (calling
the user control directly) and I get the same error as Gummy.
 
J

john_teague

Gummy, your list of actions included:
5.. Created WebForm1.aspx
6.. Drag the YesNo.ascx onto WebForm1.aspx
7.. Add the following declaration on the WebForm1.aspx
Protected yno As New YesNo

If you dragged the user control onto the page, you do not need to
instantiate a NEW YesNo on your code behind. If you look at the
control declarations that vs.net creates on the code-behind, it looks
like: (in c#)

protected Label myLabel;
protected TextBox myTextbox;

it does not say:
protected Label myLabel = new Label()

this is because they are contructed by the JIT.

Try:
protected YesNo yno

Sorry I don't know vb well enough to correctly translate the
declaration. I decided it would be better to put it correctly in c#
and let someone who knows vb to do the proper translation.
 
B

bill

Gummy-
I figured out why mine didn't work, and maybe why yours doesn't work also

When you add the YesNo.ascx user control to the aspx, it is named YesNo1
(probably). This is the ID.

When you declare the object variable, you are naming it "yno". ( "Protected
yno as New YesNo")

Instead, try naming the object variable the same as the ID of the control
when added to the aspx page:
"Protected YesNo1 as New YesNo" or "Protected YesNo1 as YesNo" (both
work for me).

I don't know why it works this way, but it does.
 
G

Gummy

Bill,

THANK YOU!!!

Obviously, it now works for me as well. I would like to know more why this
works this way, but that you got it to work is just fantastic!

I really appreciate you posting the answer. This has solved three days of my
banging my head against the wall.

Now onto getting it into a datalist, or datagrid or repeater...

Thanks again!
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top