• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

Client side control equivalent of Label

Greetings,

I have a javascript running, that passes the time to a control. I dragegd a label onto my page and then passed  the time to the Label's text property. Problem - the time is neverupdated, therafter. I would venture to say that this is because the source contains runat="server". Yet removing this produces an error.

How do I solve the problem??


thanks,
   allanmark

0
allanmark
Asked:
allanmark
  • 2
  • 2
2 Solutions
 
allanmarkAuthor Commented:
My code:

<SCRIPT LANGUAGE="JavaScript">
<!--  Clock --
var timerID = null
var timerRunning = false

function stopclock(){
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function startclock(){
    stopclock()
    showtime()
}

function showtime(){
    var now = new Date()
    var hours = now.getHours()
    var minutes = now.getMinutes()
    var seconds = now.getSeconds()
    var timeValue = "" + ((hours > 12) ? hours - 12 : hours)
    timeValue  += ((minutes < 10) ? ":0" : ":") + minutes
    timeValue  += ((seconds < 10) ? ":0" : ":") + seconds
    timeValue  += (hours >= 12) ? " P.M." : " A.M."
       
    document.getElementById("Label3").innerHTML = timeValue;    
           
    timerID = setTimeout("showtime()",1000)
    timerRunning = true
}
//-->
</SCRIPT>
0
 
McExpCommented:
are you sure that the actual control on the webpage is Label3?

Have a look at the source, asp.net takes the id that you specify on the server tag and prepends other info to it.
0
 
allanmarkAuthor Commented:
Thanks for the feedback.

Checked - label name is correct. So that leaves me confused (sob!). I suppose my question would be: how do I get aclient-side control and if that is not possible, how do I placxe a javscript control at the precise location that I want?

My source:

<body onLoad="startclock()" bgcolor="seashell">
    <form id="form1" runat="server" onSubmit="0">
    <div>
        <table style="z-index: 100; left: 0px; position: absolute; top: 0px" bgcolor="seashell" width="100%">
            <tr>
                <td colspan="8" style="text-align: center" bgcolor="blanchedalmond">
                    <strong><span style="font-size: 32pt; background-color: blanchedalmond">&nbsp;SchoolKeeper<br />
                        <span style="font-size: 12pt">Casa Maria RC Pre-Primary
                            <asp:Label ID="Label1" runat="server" BackColor="BlanchedAlmond" Font-Size="X-Small"
                            Style="z-index: 101; left: 0px; position: absolute; top: 5px" Text="CopyRight Allan Van Riel @ 2007"></asp:Label>
                        </span></span></strong>
                </td>
            </tr>
            <tr style="font-size: 12pt">
                <td colspan="8">
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                        <br />
                        <br />
                    </asp:ContentPlaceHolder>
                </td>
            </tr>
            <tr style="font-size: 12pt">
                <td style="width: 142px; height: 5px;" bgcolor="blanchedalmond">
                    &nbsp;&nbsp;                  
                    <asp:Label ID="lblDate" runat="server" Font-Size="Small" Text="Date:  "></asp:Label>
                <td style="width: 116px; height: 5px;" bgcolor="blanchedalmond">
                    &nbsp;&nbsp;                                        
                   <asp:Label ID="Label3" runat="server" Text="Time:  " Font-Size="Small"></asp:Label>&nbsp;
                    </td>
                <td bgcolor="blanchedalmond" style="width: 170px; height: 5px">
                    &nbsp;&nbsp;
                    <asp:Label ID="lblUser" runat="server" Font-Size="Small">User:</asp:Label></td>
                <td bgcolor="blanchedalmond" colspan="5" style="width: 517px; height: 5px;">                
                &nbsp;&nbsp; &nbsp;<asp:Label ID="lblComments" runat="server" Font-Size="Small"></asp:Label>&nbsp;
                </td>
                </tr>
        </table>
        <br />
        <br />
        &nbsp;</div>
    </form>
</body>
</html>
0
 
Sammy AgeilCommented:
To access a server control like a label in server side you have to use the ClientID property of the server control
client script should be like this " I used Label3

<script type="text/javascript">
<!--  Clock --
var timerID = null
var timerRunning = false

function stopclock(){
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function startclock(){
    stopclock()
    showtime()
}

function showtime(){
    var now = new Date()
    var hours = now.getHours()
    var minutes = now.getMinutes()
    var seconds = now.getSeconds()
    var timeValue = "" + ((hours > 12) ? hours - 12 : hours)
    timeValue  += ((minutes < 10) ? ":0" : ":") + minutes
    timeValue  += ((seconds < 10) ? ":0" : ":") + seconds
    timeValue  += (hours >= 12) ? " P.M." : " A.M."
       
    document.getElementById("<%=Label3.ClientID%>").innerHTML = timeValue;    
           
    timerID = setTimeout("showtime()",1000)
    timerRunning = true
}
//-->
</script>
Hope this explains things

Sammy
0
 
McExpCommented:
Probably should have stated that you need to look at the HTML Source. not your aspx source, as sammy1971 stated using the serverside function "ClientId" will return the actual ID (that will be accessable to your javascript) this should solve your problem
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now