Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 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 November 10th, 2003, 11:40 AM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get user control to refresh

I have a user control template that I am using on every page. The control displays a system message that is pulled from a database. One of the pages in my site enabes users to change the message displayed on the user control. When I use a handler to submit the page the old message shows in the user control because I assume it did not get refreshed. How can I refresh the message in the user control?
 
Old November 10th, 2003, 01:41 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What's happening is that your usercontrol is loading up (with the old message), then your handler on the page is setting the message to the new value the user entered. You need to create a publicly accessible method on the user control that you can call from the update button's event handler in the update page code. This method should make another call to the code that gets the message.

This is fairly simple if you are including the user control on the ASPX page.

In the end your ASCX code should include something like this:

    Private Sub Page_Load(...)
        GetMessage()
    End Sub

    Public Sub GetMessage()
        'Here's where you'll call the DB to get the message
    End Sub

And here's what your ASPX code will include:

    Private Sub cmdMyButton_Click(...) Handles cmdMyButton.Click
        ucMyMessageUserControl.GetMessage()
    End Sub

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 10th, 2003, 02:52 PM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,
I did that but in my user control I am using a label to show the contents of the message. Since the lablel is not a public variable I am getting a page error on the aspx that says "Object reference not set to an instance of an object. " in reference to Me.lblPrivateMessage.Text. How should I change my sub GetMessage so that the lbl is no longer a private object?


This is the sub in my ascx page:
 Public Sub GetMessage()
        'Run oledbreader to get message.
            Me.lblPrivateMessage.Text = 'value from db
End Sub

This is the sub on my aspx page:
 Private Sub cmdMyButton_Click(...) Handles cmdMyButton.Click
        ucMyMessageUserControl.GetMessage()
  End Sub
 
Old November 10th, 2003, 03:05 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to make that label a object in the page code. In visual studio, this happens automatically. Try this...

Protected lblPrivateMessage As System.Web.UI.WebControls.Label

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 10th, 2003, 03:24 PM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I put that line on my right under the class declaration on my aspx page but I am still getting errors. Could you be more specific.

Thanks a lot!
Anne


Quote:
quote:Originally posted by planoie
 You need to make that label a object in the page code. In visual studio, this happens automatically. Try this...

Protected lblPrivateMessage As System.Web.UI.WebControls.Label

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 10th, 2003, 03:33 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I can't really get more specific than that.

What are you developing in? VS.Net?
What's the error you are getting? Still the same error?

If all else fails, paste your code here so we can look it over.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 10th, 2003, 03:46 PM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the code. I am stil getting the error "Object reference not set to an instance of an object." in regards to lablemessage.text because it is a protected variable on the user control. I hope this explains it better.

The sub on the ASCX page. The sub GetSystemMessage() inherits another class called clsSystemMessage to do all the database stuff. The object should return the text from the db:
--------------------------------------------------------
Public Class UCTop_Secure
 Private Sub Page_Load(...)
    GetSystemMessage()
 End sub

 Public Sub GetSystemMessage()
        Dim objSystemMessage As New clsSystemMessage
        objSystemMessage.ShowSystemMessage("A")
        labelMessage.Text = objSystemMessage.SystemMessage
        'labelMessage is an html label placed on the ascx design

  End Sub
End Class


Here is the sub on the ASPX page.
-------------------------------------------
 Private Sub cmdSendNote_Click(..) Handles cmdSendNote.Click
        UpdateSystemMessage()
        Dim objUCTop_Secure As New UCTop_Secure
        objUCTop_Secure.GetSystemMessage()
    End Sub
 
Old November 10th, 2003, 04:07 PM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The error that I mentioned in the previous post only happens when the Sub cmdEmail_Click handler is run. This sub is trying to access the user control class and it is failing because it cannot access the labelmessage protected variable.

Anne
 
Old November 10th, 2003, 04:21 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Public Class UCTop_Secure
 Protected lblPrivateMessage As System.Web.UI.WebControls.Label
 Private Sub Page_Load(...)
...

That should do it. Perhaps I mislead you earlier. I meant to say that the label needed to be in user control code (not page code).

And just a small note on terminology:
"The sub GetSystemMessage() inherits another class " is not quite right. That sub is just using an instance of another class, not inheriting it. Only classes and interfaces can inherit (to the best of my knowledge).

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto-refresh user control in .NET 1.1 r_adarsh .NET Framework 1.x 2 February 1st, 2008 05:51 AM
Refresh Page when User Clicks on Back Button testsubject Visual Studio 2005 1 June 26th, 2006 03:46 AM
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
Help with control initialization in user control mike_remember ASP.NET 1.0 and 1.1 Professional 7 December 19th, 2005 11:08 AM





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