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
> 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

Signature
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
A watched clock never boils.
>I think...
>
[quoted text clipped - 63 lines]
>> >
>> > -Rob
bill - 25 Oct 2005 20:24 GMT
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.
> > In the codebehind for the ASCX, the following code was placed there
> > automatically (I assume)
[quoted text clipped - 78 lines]
> >> >
> >> > -Rob
Gummy - 25 Oct 2005 21:02 GMT
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.
> > In the codebehind for the ASCX, the following code was placed there
> > automatically (I assume)
[quoted text clipped - 78 lines]
> >> >
> >> > -Rob
Kevin Spencer - 25 Oct 2005 22:11 GMT
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

Signature
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
A watched clock never boils.
> Kevin,
>
[quoted text clipped - 97 lines]
>> >> >
>> >> > -Rob
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.

Signature
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com
> I think...
>
[quoted text clipped - 61 lines]
> > >
> > > -Rob
Gummy - 26 Oct 2005 06:35 GMT
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.
> Hmmm... I did the following:
> control.ascx
[quoted text clipped - 95 lines]
>> > >
>> > > -Rob