Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 7th, 2006, 12:23 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Control won't work!

Hi, I have a webform with a user control on it. The user control is basically a little navigation menu with some buttons on it that redirect users to a new pages. What I'm trying to do from my webform is tell the user control to change the button text color so the user knows what page he's on but I'm getting the following error :(

Object reference not set to an instance of an object

Here's the code I'm trying to do this with...


(My Webform)

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim TopNav1 As New TopNav
        TopNav1.AdminButtonColor = "#FFFFFF"
    End Sub

(My User Control)
Public Property AdminButtonColor() As String
        Get
            Return btnAdmin.Text.ToString()
        End Get
        Set(ByVal Value As String)
            btnAdmin.ForeColor = ColorTranslator.FromHtml(Value)
        End Set
 End Property

Any help would be really appreciated!!




 
Old April 7th, 2006, 07:22 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

There are at least two ways to fix this.

1. When you have place the control on the design / code surface of the page, there really isn't a reason to New up a new instance of the control. Just reference it by its ID in the markup and access its properties like this:

TopNav1.AdminButtonColor = "#FFFFFF"

This assumes you have a control of type TopNav with an ID of TopNav1 in your page.

2. Add a reference to the control at the top of the page near the page directive, like this:

<%@ Reference Control="TopNav.ascx" %>

or

<%@ Reference VirtualPath="~/TopNav.ascx" %>)

Then in the code behind of the page, you can now New up instances of TopNav.

Take a look here for more detailed informatioN:

http://msdn2.microsoft.com/en-us/library/w70c655a(en-US,VS.80).aspx

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old April 9th, 2006, 11:44 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar that worked like a charm :) You rock!

Quote:
quote:Originally posted by Imar
 Hi there,

There are at least two ways to fix this.

1. When you have place the control on the design / code surface of the page, there really isn't a reason to New up a new instance of the control. Just reference it by its ID in the markup and access its properties like this:

TopNav1.AdminButtonColor = "#FFFFFF"

This assumes you have a control of type TopNav with an ID of TopNav1 in your page.

2. Add a reference to the control at the top of the page near the page directive, like this:

<%@ Reference Control="TopNav.ascx" %>

or

<%@ Reference VirtualPath="~/TopNav.ascx" %>)

Then in the code behind of the page, you can now New up instances of TopNav.

Take a look here for more detailed informatioN:

http://msdn2.microsoft.com/en-us/library/w70c655a(en-US,VS.80).aspx

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Help! Custom Server Control using User Control diehard ASP.NET 1.0 and 1.1 Professional 2 January 4th, 2006 12:33 PM
User Control: Tab key does not work alhad VB.NET 2002/2003 Basics 0 November 3rd, 2005 03:20 AM
How to stop user from view js code but still work bekim Javascript How-To 4 January 7th, 2005 09:08 PM
cant get tab control to work tmc Access 1 August 4th, 2004 10:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.