Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display variable contents in asp.net page 1

Status
Not open for further replies.

aaronjonmartin

Technical User
Jul 9, 2002
475
GB
Hi all, hopefully I can explain this clearly enough. If anyone can shed some light on this i would be very greatful.

I have a javascript countdown timer in one of my .aspx pages which countsdown to a particular date time. The datetime to be counted down to is retrieved from a database. This is how I have attempted this so far:

Code:
<script language="JavaScript">
TargetDate = "<asp:Label ID="kickofftime" runat="server" Text="Label" />";
BackColor = "none";
ForeColor = "black";
CountActive = true;
CountStepper = -1;
DisplayFormat = "%%D%%d %%H%%h %%M%%m %%S%%s";
FinishMessage = "Games currently in progress";
</script>

this script section appears in the body of my html, and i am trying to pass the value by setting the text of the label to the value of the variable which contains the date. Unfortunately this doesnt work as the label generates the following code:

Code:
<script language="JavaScript">
TargetDate = "<span id="kickofftime">29/04/2006 12:30:00</span>";
BackColor = "none";
ForeColor = "black";
CountActive = true;
CountStepper = -1;
DisplayFormat = "%%D%%d %%H%%h %%M%%m %%S%%s";
FinishMessage = "Games currently in progress";
</script>

as you can see the label generates the <span> tag which is causing the code to not work correctly.

So how can i get the value of a variable into the javascript section of code?

Thanks for any help in advance.

Aaron

&quot;It's so much easier to suggest solutions when you don't know too much about the problem.&quot;
Malcolm Forbes (1919 - 1990)
 
You can output the variable like so in the required section of your html source. You would have to populate 'string strDateTime' with the database value.

Code:
<script language="JavaScript">
TargetDate = "<%=strDateTime%>";
BackColor = "none";
ForeColor = "black";
CountActive = true;
CountStepper = -1;
DisplayFormat = "%%D%%d %%H%%h %%M%%m %%S%%s";
FinishMessage = "Games currently in progress";
</script>

Sean. [peace]
 
aaron: couple of thoughts, though not quite familiar with the Javascript counter. Try perhaps using the "innerHTML" approach (property of span object) and substitute you time in that way (if you need some example code just post back) and two, use the getElementbyID("myTexbox").Value in an attempt to pass a javascript variable to an asp label or textbox. I'm sure you'll get a few opinions on this during the day; just my 2 cents at frist glance. Another thought is to pass the java variabler into a hiddent textbox and pick it up in codebehind (if you want to handle 2 sets of code) using say the FindControl method, etc. Many ways to approach this.
 
sean4e: That is also another approach, and I have used that in the past with routine success. However, I have seen a few argue here at Tek-Tips to avoid using the old asp approach and try and handle most situations server side if possible; that argument may apply here as well.
 
Thanks for the replies.

sean4e: i tried that but I recieved an error message saying that the variable is not declared. It is declared as a string in my DataSub Sub which is executed on pageload. Any ideas why this is?

Isadore: your suggestion sounds interesting, could you go into more detail? im guessing using any kind of control would cause similar problems as im experiencing with the label.

&quot;It's so much easier to suggest solutions when you don't know too much about the problem.&quot;
Malcolm Forbes (1919 - 1990)
 
aaron: I'm unfamiliar with:

TargetDate = "<%=strDateTime%>";

in javascript so I can't really say anything about your approach. My suggestion was that if the span object became a spersistent problem perhaps you could turn it to your favor by setting the time value, an example would be:
Code:
function changeText(spanName,text){
 document.getElementById(spanName).innerHTML = text;
}
So the approach would be perhaps to set within the html a span object and take advantage of an assignment via javascript. The other approach was to perhaps throw the tiem value into a hidden textbox using javascript and then capturing the value on in codebhind. Just a couple of thoughts. If you can get sean's suggestion working I can't see any reason why that could not be reliable.
 
Don't use the classic asp approach that was suggested (as that's whatASP.NET is trying to get away from). Instead, use Page.RegisterClientScriptBlock (or ClientScript.RegisterClientScriptBlock) to register your javascript and simply create the function in your ASP.NET code.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top