Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design 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
  #1 (permalink)  
Old November 12th, 2003, 05:14 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default Web User Control Problem

I have this code in my User Control:

   Public MustInherit Class TestMenuControl
   Inherits System.Web.UI.UserControl
   Public WithEvents Label1 As System.Web.UI.WebControls.Label 'Made public so the web page can access it.


This code is in my page:

Public Class MenuTest
Inherits System.Web.UI.Page
Protected WithEvents UC As TestMenuControl

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'I put in the Protecte WithEvents to expose the control

UC.Label1.Text = "this is a test"

End Sub

I am simply trying to set the text of label1 on the UserControl, but I get this error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. [/i]

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

Source Error:

Line 22:
Line 23: 'UC.uwmTest.CheckBoxes = True
Line 24: UC.Label1.Text = "this is a test"
Line 25: End Sub
Line 26:




Am I missing something?? Thanks

  #2 (permalink)  
Old November 12th, 2003, 05:22 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I trust there is an instance of this user control in the markup for this page. That's where the control is instantiated, so you don't need the New keyword in the codebehind.

I had a very similar problem just recently. I ended up deleting the control (and the @Register directive in the markup and removing the "Protected ..." line in the code behind, then added everything back in and it worked. Nothing *should* have been different, but it was.

Peter
------------------------------------------------------
Work smarter, not harder.
  #3 (permalink)  
Old November 12th, 2003, 05:43 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Peter,

   Thanks, I tried that but I am still having the same problem, it's very strange. If you have any other ideas let me know.
Also, do you know if there is a way to refernce objects on the Page from the UserControl?



  #4 (permalink)  
Old November 12th, 2003, 07:05 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I can't think of any other reason you could have this problem.

Regarding your other question: Yes there is. BUT... that kind of goes against the model of the User Control.

Every object derived from System.Web.UI.Control (Page and UserControl are among all the other WebControls) has a "Page" property. This references the System.Web.UI.Page that the control resides on. So you could potentially reference objects in the Page by using something like this...

Me.Page.FindControl("someControlID")

Presuming that control exists on the page (whether it needs to be public or not, I'm not sure), this should work.

However, I would recommend a different scenario. Just like the web controls you are familiar with, you should use events to handle things coming from the user control. If something happens on a user control that you want have affect the page, raise an event. Then your page can set up an event handler for that user control's event. If that control is on a page which doesn't handle the event, there's no problem. This saves you from writing complicated checking within the user control to determine if the page has the controls you need to affect etc. and follows the Event Driven model of ASP.Net.

You can also set up multiple user controls to talk to each other. You set up public event(s) on one control, public handlers on another control, then the page that both control's live on "wire up" the two controls by adding the handler of one control to the event of the other.

Peter
------------------------------------------------------
Work smarter, not harder.
  #5 (permalink)  
Old November 13th, 2003, 10:26 AM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Thanks for the info, it makes lots of sense what you are saying. What I was thinking was that in my user control I have a menu which will be dropped on several pages. Depending on which page you are on, some choice may be disabled. I thought that by doing the check in the user control would make more sense, but what you said I think makes more sense. If I get my original problem solved, I will let you know what it is.

Thanks

Jim
  #6 (permalink)  
Old November 13th, 2003, 10:46 AM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I'd like to go back to my other question about the user contol affecting the page. Let's take this senerio:
Lets day you have a drop down list of colors in a user contorl. The the user chooses a color, it changes the color of the page. How could something like this be accomplished?

  #7 (permalink)  
Old November 13th, 2003, 04:49 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The user control has to define an event:

Public Event ColorChanged(ByVal sNewColor As String)

Your user control's drop down handler fires that event:

Public Sub ddlColors_onSelectedIndexChanged(...) Handles ddlColors.SelectedIndexChanged
    RaiseEvent ColorChanged(ddlColors.SelectedItem.Text)
End Sub

The page has a handler for the user control's event:

Private Sub ucColorChanger_ColorChanged(sNewColor) Handles ucColorChanger.ColorChanged
    Dim sColorStyle = String.Format("<style type='text/css'>BODY{background-color:{0};}</style>", sNewColor)
    RegisterClientScriptBlock("colorchange", sColorStyle)
End Sub

As long as that is the last thing on the page to set the background style of the page, it should work! :)

Peter
------------------------------------------------------
Work smarter, not harder.
  #8 (permalink)  
Old November 14th, 2003, 11:31 AM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Thanks... I will try this as soon as I get a chance:)

  #9 (permalink)  
Old November 17th, 2003, 06:57 PM
Registered User
 
Join Date: Nov 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am having the same problem as jbenson001.

Here is the code in my .vb file:

************************************************** ***************************************

Imports System.web.ui
Imports System.web.ui.WebControls
Imports System.web.ui.Page

Public Class UserCookie
    inherits System.web.ui.UserControl
    Public WithEvents WelcomeBack As System.Web.UI.WebControls.label

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim WelcomeBack as new System.Web.UI.WebControls.label
        if Request.Cookies("UserName").value <> "" then
            WelcomeBack.text = "Welcome back " & Request.Cookies("UserName").value & "!"
        else
            WelcomeBack.text = "Test"
        end if
    End Sub
End Class

************************************************** ***************************************

Here is the code in my .ascx file:

************************************************** ***************************************
<%@ Control src="UserCookie.ascx.vb" Inherits="UserCookie" %>

<asp:label id="WelcomeBack"></asp:label>
************************************************** ***************************************

Here is the code in my .aspx file:

************************************************** ***************************************
<%@ Page language="vb" autoeventwireup="false" %>
<%@ Register TagPrefix="ucl" TagName="UserCookie" Src="UserCookie.ascx" %>

<form id="myform" runat="server">
<ucl:UserCookie id="UserCookie1" runat="server"/>
</form>
************************************************** ***************************************

I am getting the "Object reference not set to an instance of an object." error as well.

Here is the error output:

************************************************** ***************************************
[NullReferenceException: Object reference not set to an instance of an object.]
   UserCookie.Page_Load(Object sender, EventArgs e) +46
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Control.LoadRecursive() +98
   System.Web.UI.Control.LoadRecursive() +98
   System.Web.UI.Page.ProcessRequestMain() +731
************************************************** ***************************************

Any help would be greatly appreciated.

Thanks,

Ami Schreiber

  #10 (permalink)  
Old November 17th, 2003, 10:16 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Ami,

It looks like the problem you are having is that the cookie isn't there. In classic ASP you could simply test If Request.Cookie("...") to see if there is a value. That is no longer enough do to the strong typing of VB.Net. You now need to test to see if the call to that key in the cookies collection returns an object or not.

        if Not Request.Cookies("UserName") Is Nothing then
            WelcomeBack.text = "Welcome back " & Request.Cookies("UserName").value & "!"
        else
            WelcomeBack.text = "Test"
        end if

This should fix you up.

Peter
------------------------------------------------------
Work smarter, not harder.




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
Web user control EvilGuyWhoEatsBrains General .NET 1 December 14th, 2004 01:21 PM
Web User Control Best Practices flyin General .NET 4 May 24th, 2004 07:57 AM
Web User Control Problem shmacgregor VS.NET 2002/2003 18 February 22nd, 2004 06:29 PM





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