Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 24th, 2008, 09:07 AM
Authorized User
 
Join Date: Sep 2007
Location: Rugeley, , United Kingdom.
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default VB: Time-dependent greeting

I am specifying the introductory message text on my Update.aspx page according to role and I want to include an optional "good morning", "good afternoon", "good evening" greeting. I have defined a label control in the content template, and some tests in the code-behind file determine which greeting to use. However, the error "Name 'Greeting' is not declared" has popped up, and I don't know how to fix it.

An extract of my page source is here:

Code:
    <asp:loginview ID="UpLoginView" runat="server">
        <rolegroups>
            <asp:rolegroup Roles="leaders">
                <contenttemplate>
                    <p><asp:label ID="Greeting" runat="server" />
                    <asp:loginname ID="GreetingName" runat="server" />
                    Here you can enter details of a new event, 
or modify details already entered, so long as the event hasn't 
happened already!</p>
            </contenttemplate>
            </asp:rolegroup>
            <asp:rolegroup Roles="administrators">
                <contenttemplate>
                    <p>Welcome back
                    <asp:loginname ID="GreetingName" runat="server" />
                    Here you can enter details of a new event,
 or modify details of future events, or delete events!
</p></contenttemplate>
            </asp:rolegroup>
        </rolegroups>
        <anonymoustemplate>
            This is the anonymous template.
        </anonymoustemplate>
        <loggedintemplate>
            This is the logged in template.
        </loggedintemplate>
    </asp:loginview>
The code-behind is here:

Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
 System.EventArgs) Handles Me.Load
        Dim dt As DateTime = DateTime.Now()

        If dt.Hour < 12 Then
            Greeting.Text = "Good morning"
        Else
            If dt.Hour < 18 Then
                Greeting.Text = "Good afternoon"
            Else
                Greeting.Text = "Good evening"
            End If
        End If
    End Sub
Richard
Rugeley, UK
WinXP Pro SP2
VBW Express + SQL Server 2005 Express
__________________
Richard
Rugeley, UK
WinXP Pro SP2
VBW Express + SQL Server 2005 Express
Reply With Quote
  #2 (permalink)  
Old July 24th, 2008, 11:08 AM
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 think this is a problem with using the content templates. Technically, you could have the same instance of a control with the same ID in two different content templates. This is a case of the difficulties caused by "naming containers". The label control in question is buried inside a active template based on the state of the control so you need to go and find it.

Try doing something like this (my VB.NET may not be exactly correct, I've been using C# for the last several years):

Dim ctlGreeting As Label = CType(UpLoginView.FindControl("Greeting"), Label)
ctlGreeting.Text = ...

-Peter
compiledthoughts.com
Reply With Quote
  #3 (permalink)  
Old July 31st, 2008, 05:51 AM
Authorized User
 
Join Date: Sep 2007
Location: Rugeley, , United Kingdom.
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply, Peter, and sorry for the delay in getting back.

I changed the code to:
Code:
  Dim dt As DateTime = DateTime.Now()
        Dim ctlGreeting As Label = 
CType(UpLoginView.FindControl("ctlGreeting"), Label)

        If dt.Hour < 12 Then
            ctlGreeting.Text = "Good morning"
        Else
            If dt.Hour < 18 Then
                ctlGreeting.Text = "Good afternoon"
            Else
                ctlGreeting.Text = "Good evening"
            End If
        End If

but this produced a different error on the 'good morning' line:
Object reference not set to an instance of an object.

However, to eliminate the duplication of ID=Greeting (on the source page), perhaps the label containing the greeting should be moved to some outer container since it's not role-dependent?

Richard
Rugeley, UK
WinXP Pro SP2
VBW Express + SQL Server 2005 Express
Reply With Quote
  #4 (permalink)  
Old July 31st, 2008, 06:00 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Richard,

Yeah I think the problem here is that the control is nested within a template. I think (and feel free to correct me anyone!) to overcome any issues with naming collisions, container controls are responsible for ensuring their child controls are uniquely named (normally done by prefixing the ID's with their own).

I had similar problems when creating a custom control of mine. You can see if this is taking place by commenting out your greeting code (to get it to run) and then view the page source to check the ID of the label control.

To get around this? I ended up exposing the child controls collection, now I am assuming some sort of similar mechanism is in place for templated controls?

Try examining the controls hierarchy, you may find the path to your control there (e.g. UpLoginView.Controls["ctlGreeting"] or something of the like).

Sorry I cant be more descriptive as I have not had a chance to delve too deeply into this myself, as its all kind of new to me also! :)

I hope this at least helps a little :D

Rob
http://cantgrokwontgrok.blogspot.com
Reply With Quote
  #5 (permalink)  
Old July 31st, 2008, 12:47 PM
Authorized User
 
Join Date: Sep 2007
Location: Rugeley, , United Kingdom.
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Rob.

Richard
Rugeley, UK
WinXP Pro SP2
VBW Express + SQL Server 2005 Express
Reply With Quote
  #6 (permalink)  
Old July 31st, 2008, 01:40 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

No problem Richard, did that help/point you in the right direction?

Rob
http://cantgrokwontgrok.blogspot.com
Reply With Quote
  #7 (permalink)  
Old August 1st, 2008, 04:43 AM
Authorized User
 
Join Date: Sep 2007
Location: Rugeley, , United Kingdom.
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rob - I decided not to go there and moved the greeting outside of the loginview control.
I may re-visit this issue again when I have a little more experience/confidence.
Thanks again.

Richard
Rugeley, UK
WinXP Pro SP2
VBW Express + SQL Server 2005 Express
Reply With Quote
  #8 (permalink)  
Old August 1st, 2008, 07:24 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Ah, sorry to hear that, but I have been there myself! :)

Be sure to ask if you have any more problems :)

Rob
http://cantgrokwontgrok.blogspot.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Very first "greeting" app not working cothedo BOOK: Beginning Ruby on Rails 3 December 6th, 2008 01:04 AM
Wrox Greeting Cards rai.ashis BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 December 17th, 2007 03:25 AM
Greeting Card in C# ? KellyRainmaster BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 October 30th, 2007 05:06 PM
Chapter 1: Greeting Not Working Joe3000 BOOK: Beginning Ruby on Rails 2 June 14th, 2007 10:54 AM
How to Play greeting(wave file) on modem mun24 Visual C++ 0 October 30th, 2006 09:45 AM



All times are GMT -4. The time now is 01:05 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.