By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

Label Control?

P: n/a
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

Jul 30 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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" <ar******@hotmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
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

Jul 30 '06 #2

P: n/a
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 wrote:
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" <ar******@hotmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
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
Jul 30 '06 #3

P: n/a
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" <ar******@hotmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
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 wrote:
>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" <ar******@hotmail.comwrote in message
news:11*********************@i3g2000cwc.googlegro ups.com...
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

Jul 30 '06 #4

P: n/a
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,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Arpan" <ar******@hotmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
>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 wrote:
>>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" <ar******@hotmail.comwrote in message
news:11*********************@i3g2000cwc.googlegr oups.com...

Jul 31 '06 #5

P: n/a
Yes, sure. Thank you Hans.
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.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. 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,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Arpan" <ar******@hotmail.comwrote in message
news:11*********************@p79g2000cwp.googlegr oups.com...
>>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 wrote:
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" <ar******@hotmail.comwrote in message
news:11*********************@i3g2000cwc.googleg roups.com...



Jul 31 '06 #6

P: n/a
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 wrote:
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" <ar******@hotmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
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 wrote:
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" <ar******@hotmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
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
Jul 31 '06 #7

P: n/a
"Arpan" <ar******@hotmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.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.
>
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 wrote:
>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" <ar******@hotmail.comwrote in message
news:11*********************@p79g2000cwp.googlegr oups.com...
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 wrote:
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" <ar******@hotmail.comwrote in message
news:11*********************@i3g2000cwc.googlegro ups.com...
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


Aug 1 '06 #8

P: n/a
>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:Labelin 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
>
Arpan

Eliyahu Goldin wrote:
>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" <ar******@hotmail.comwrote in message
news:11*********************@p79g2000cwp.googlegr oups.com...
>>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 wrote:
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" <ar******@hotmail.comwrote in message
news:11*********************@i3g2000cwc.googleg roups.com...

Aug 1 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.