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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling javascript

Status
Not open for further replies.

slatet

Programmer
Sep 11, 2003
116
US
I need to have a webpart with a continuously updating clock, in order words, it changes every second. I found something on the web in javascript that works. I have the webpart itself working but now I have to transfer the javascript code into the c# web part. Does anyone know the best way to handle this?

I'm trying to call the javascript function in C# but I might have the syntax wrong, I'm rusty in C#. "Attach" is located in a .js file. Am I calling the javascript function correctly in C#?


string vScript = "<script language=\"javascript\">" +
" var TZClockSample = new TZClock();" +
"TZClockSample.Attach('MounDay', '');" +
"</script" + ">";
DateLiteral.Text = vScript;

ANy help is appreciated.
 
You should use RegisterClientScriptBlock() method to register a client-side script blocks.
So, I think what there is in .js you should register with the page.
Here is a simple example in which the current date is displayed in textbox ("input1") on the page:
Code:
private void Page_Load(object sender, System.EventArgs e)
{
	RegisterScript();
}

private void RegisterScript()
{
	
	String scriptString = "<script language=\"JavaScript\"> function DoSomething() {";
	scriptString+="document.Form1.input1.value=new Date()";
	scriptString+="}";
	scriptString += "</script>";

	if(!this.IsClientScriptBlockRegistered("clientScript1"))
		this.RegisterClientScriptBlock("clientScript1", scriptString);
}
Now you can call the DoSomething() function in the follwing ways:

For a Web server control :
Code:
this.Button1.Attributes.Add("OnClick","DoSomething()");
For a web HTML control :
Code:
<INPUT id="btn" onclick="DoSomething()" type="button" value="btn">
To understand how it is working, use Web designer to generate a web form and put there a text field named "input1" and switch to HTML code and modify the body line like:
Code:
<body onload="DoSomething()" MS_POSITIONING="GridLayout">
Build and run. You should see the text field populated with current date and time.
So, try that:
Code:
private void Page_Load(object sender, System.EventArgs e)
{
	RegisterScript();
}
private void RegisterScript()
{
	
	String scriptString = "<script language=\"JavaScript\"> function ShowClock() {";
	scriptString+="var TZClockSample = new TZClock();TZClockSample.Attach('MounDay', '');";
	scriptString+="}";
	scriptString += "</script>";

	if(!this.IsClientScriptBlockRegistered("clientScript2"))
		this.RegisterClientScriptBlock("clientScript2", scriptString);
}
<INPUT id="btn" onclick="ShowClock()" type="button" value="btn">
or
<body onload="ShowClock()" MS_POSITIONING="GridLayout">
obislavu
 
It sounds so easy the way you explain it but it is a bit more difficult with a web part. There is no onload or html, everything is built in c#. But I did attempt using your code and it doesn't like

this.IsClientScriptBlockRegistered or tis.RegisterClientScriptBlock

it says that it does not contain a definition. Any ideas?
 
The above code is working.
You should include:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

In "this.IsClientScriptBlockRegistered()" expression , this refers to the current form derived from System.Web.UI.Page and the IsClientScriptBlockRegistered() is a method of Page class.
obislavu
 
We got it to work BUT, we had to put the javascript function in a string because it was so long that it wouldn't recognize all the new lines. Now we are trying to figure out how to assign the c# cell with the value returned from the javascript code

Here's a snippet of the string we created:

private string JavaScriptSource
{
get
{
string vSource = "<script language='javascript'>" +
"function ReturnTimeZone()" +


...............
"StartClocks(1); // Update clocks every 2 seconds"
"</script>";
return vSource;
}
}

Doing it like this, we don't know how to grab the resulting value for each time zone.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top