Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2005

Tip: Looking for answers? Try searching our database.

Newbie: Adding a User Control Problems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gummy - 25 Oct 2005 18:31 GMT
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
Curt_C [MVP] - 25 Oct 2005 19:00 GMT
is lblName declared in the codebehind?

Signature

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

> Hello,
>
[quoted text clipped - 45 lines]
>
> -Rob
Gummy - 25 Oct 2005 19:23 GMT
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

> is lblName declared in the codebehind?
>
[quoted text clipped - 47 lines]
> >
> > -Rob
Kevin Spencer - 25 Oct 2005 19:39 GMT
> 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
Curt_C [MVP] - 25 Oct 2005 21:24 GMT
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
bill - 26 Oct 2005 15:23 GMT
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.

> Hello,
>
[quoted text clipped - 45 lines]
>
> -Rob
john_teague - 26 Oct 2005 16:01 GMT
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.
bill - 26 Oct 2005 19:32 GMT
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.

> Hello,
>
[quoted text clipped - 45 lines]
>
> -Rob
Gummy - 26 Oct 2005 21:18 GMT
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!

> Gummy-
> I figured out why mine didn't work, and maybe why yours doesn't work also
[quoted text clipped - 63 lines]
> >
> > -Rob

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.