Label Control?

A

Arpan

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
 
A

Arpan

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 said:
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 said:
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
 
E

Eliyahu Goldin

Arpan,

Method SetTime creates a new instance of Label. It is not the same control
that you have on the form.
 
E

Eliyahu Goldin

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 said:
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 said:
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 said:
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
 
H

Hans Kesting

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 said:
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
 
E

Eliyahu Goldin

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

Hans Kesting said:
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 said:
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.
 
A

Arpan

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 said:
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 said:
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 said:
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]

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
 
E

Eliyahu Goldin

Arpan said:
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 said:
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 said:
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]

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
 
H

Hans Kesting

The one created in the SetTime method is not placed on the form. You just
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
Arpan

Eliyahu said:
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 said:
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]

news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top