Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Label Control?

1 view
Skip to first unread message

Arpan

unread,
Jul 30, 2006, 8:37:21 AM7/30/06
to
Consider the following code snippet (my main intention is to display
the current time in a Label control as & when this ASPX page is
accessed/refreshed):

<script runat="server">
Class Clock
Sub SetTime(ByVal intSec As Integer, ByVal intMin As Integer,
ByVal intHour As Integer)
Dim lblTime As New System.Web.UI.WebControls.Label
lblTime.Text = "Current Time: " & intHour & ":" & intMin &
":" & intSec
End Sub
End Class

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim objClock As New Clock
objClock.SetTime(Second(Now), Minute(Now), Hour(Now))
End Sub
</script>
<form id="frmTime" runat="server">
<asp:Label id="lblTime" runat="server"/>
</form>

The above code doesn't render any text in the Label control i.e the
Label control doesn't show the current time. Why?

Thanks,

Arpan

Eliyahu Goldin

unread,
Jul 30, 2006, 9:44:07 AM7/30/06
to
Arpan,

Method SetTime creates a new instance of Label. It is not the same control
that you have on the form.
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Arpan" <arpa...@hotmail.com> wrote in message
news:1154263041....@i3g2000cwc.googlegroups.com...

Arpan

unread,
Jul 30, 2006, 9:11:01 AM7/30/06
to
But then what is the difference between the new instance of the Label
control that the method SetTime creates & the Label control which
exists in the Form?

Moreover, since you are saying that the SetTime creates a new instance
of a Label control, does that mean that the Form has 2 Label controls?
I am getting a bit confused.

Thanks,

Regards,

Arpan

Eliyahu Goldin

unread,
Jul 30, 2006, 10:32:17 AM7/30/06
to
The one created in the SetTime method is not placed on the form. You just
create another label and don't put it anywhere. I doubt that is what you
intend to do. If all you want to do is to update the label on the form, you
should make SetTime a method of your page class and set the label directly.
Something like:

<script runat="server">


Sub SetTime(ByVal intSec As Integer, ByVal intMin As Integer, ByVal
intHour As Integer)
Dim lblTime As New System.Web.UI.WebControls.Label
lblTime.Text = "Current Time: " & intHour & ":" & intMin & ":" &
intSec
End Sub

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)


SetTime(Second(Now), Minute(Now), Hour(Now))
End Sub
</script>

--

Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Arpan" <arpa...@hotmail.com> wrote in message

news:1154265061....@p79g2000cwp.googlegroups.com...

Hans Kesting

unread,
Jul 31, 2006, 5:20:36 AM7/31/06
to
> The one created in the SetTime method is not placed on the form. You just
> create another label and don't put it anywhere. I doubt that is what you
> intend to do. If all you want to do is to update the label on the form, you
> should make SetTime a method of your page class and set the label directly.
> Something like:
>
> <script runat="server">
> Sub SetTime(ByVal intSec As Integer, ByVal intMin As Integer, ByVal
> intHour As Integer)
> Dim lblTime As New System.Web.UI.WebControls.Label
> lblTime.Text = "Current Time: " & intHour & ":" & intMin & ":" &
> intSec
> End Sub
>
> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
> SetTime(Second(Now), Minute(Now), Hour(Now))
> End Sub
> </script>
>

.. and remove the "Dim lblTime as .." line as I'm sure you intended :-)

By the way, you could also use (translated from C#)
lblTime.Text = "Current time: " & DateTime.Now.ToString("HH:mm:ss")


Hans Kesting

Eliyahu Goldin

unread,
Jul 31, 2006, 6:32:16 AM7/31/06
to
Yes, sure. Thank you Hans.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Hans Kesting" <news.2...@spamgourmet.com> wrote in message
news:mn.faa87d678...@spamgourmet.com...

Arpan

unread,
Jul 31, 2006, 1:54:25 PM7/31/06
to
> The one created in the SetTime method is not placed on the form. You just
> create another label and don't put it anywhere.

Goldin, first of all, why isn't the Label within the class Clock not
put anywhere? What's the reason behind that?

Moreover, since the Label is not put anywhere in the Form, it means
that it won't be visible to users. So does it make any sense to use
built-in server controls present in the .NET Framework in user-defined
classes like what I have done in the class Clock (in post #1)? I guess
no!

Arpan

Eliyahu Goldin

unread,
Aug 1, 2006, 3:50:20 AM8/1/06
to
"Arpan" <arpa...@hotmail.com> wrote in message
news:1154368465.0...@75g2000cwc.googlegroups.com...

>> The one created in the SetTime method is not placed on the form. You just
>> create another label and don't put it anywhere.
>
> Goldin, first of all, why isn't the Label within the class Clock not
> put anywhere? What's the reason behind that?
Because *you* don't put it anywhere. You create it in your code. You are in
control and noone else.

Hans Kesting

unread,
Aug 1, 2006, 4:10:23 AM8/1/06
to
>> The one created in the SetTime method is not placed on the form. You just
>> create another label and don't put it anywhere.
>
> Goldin, first of all, why isn't the Label within the class Clock not
> put anywhere? What's the reason behind that?
>

The <asp:Label> in your form leads to a class-level variable "lblTime"
(of type Label). The fact that your local variable has the same name
doesn't automatically link it to the Label defined on the page (works
just like a "normal" variable).

> Moreover, since the Label is not put anywhere in the Form, it means
> that it won't be visible to users. So does it make any sense to use
> built-in server controls present in the .NET Framework in user-defined
> classes like what I have done in the class Clock (in post #1)? I guess
> no!

You could add it to the form with code like Page.Controls.Add(lblTime).
This would then be a *second* Label.
But in this particular case you could get runtime errors because the
viewstate mechanism can't handle different controls with the same name.
And in this case you want to access the existing Label, not add a new
one.

Hans Kesting

0 new messages