<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
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...
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
<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...
.. 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
"Hans Kesting" <news.2...@spamgourmet.com> wrote in message
news:mn.faa87d678...@spamgourmet.com...
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
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