p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   VB: Time-dependent greeting (http://p2p.wrox.com/showthread.php?t=69604)

Rich57 July 24th, 2008 09:07 AM

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

planoie July 24th, 2008 11:08 AM

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

Rich57 July 31st, 2008 05:51 AM

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

robzyc July 31st, 2008 06:00 AM

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

Rich57 July 31st, 2008 12:47 PM

Thanks, Rob.

Richard
Rugeley, UK
WinXP Pro SP2
VBW Express + SQL Server 2005 Express

robzyc July 31st, 2008 01:40 PM

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

Rob
http://cantgrokwontgrok.blogspot.com

Rich57 August 1st, 2008 04:43 AM

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

robzyc August 1st, 2008 07:24 AM

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


All times are GMT -4. The time now is 02:27 AM.

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