473,394 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Calling javascript from C#--help please

// I wrote the following code but it is not working.
// Any help please?
// ------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
Nov 19 '05 #1
10 1564
Bishoy,

What calls your javascript function?

I don't see any code to fire it. Is it attached to a button click?

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Bishoy George" <bi****@bishoy.com> wrote in message
news:uo**************@TK2MSFTNGP14.phx.gbl...
// I wrote the following code but it is not working.
// Any help please?
// ------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Nov 19 '05 #2
Bishoy George wrote:
// I wrote the following code but it is not working.
// Any help please?
// ------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}


What isn't working? is the script there when you view the source from
your asp page?

--
Rob Schieber
Nov 19 '05 #3
is there anything on the page calling it?

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"Bishoy George" wrote:
// I wrote the following code but it is not working.
// Any help please?
// ------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Nov 19 '05 #4
// Here you are and still not working!!!
// -------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}
}
}
Nov 19 '05 #5
// Although Page_Load is a trigerring event method.
// Here you are and still not working!!!
// -------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}
}
}
Nov 19 '05 #6
you seem confused. after you click the button, view source and you should
see the javascript function defined on the page. you still need some client
(javascript) code to call it. if you just wnat it to run then try inline
script:

string toRed = "<script language=\"javascript\">document.bgColor =
\"red\"; </script>";

-- bruce (sqlwork.com)
"Bishoy George" <bi****@bishoy.com> wrote in message
news:eJ**************@TK2MSFTNGP15.phx.gbl...
// Although Page_Load is a trigerring event method.
// Here you are and still not working!!!
// -------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}
}
}

Nov 19 '05 #7

A little bit of reading on Client Side scripting with ASP may help

http://www.c-sharpcorner.com/Code/20...eScripting.asp

"Bishoy George" wrote:
// I wrote the following code but it is not working.
// Any help please?
// ------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Nov 19 '05 #8
Bishoy,

You're confused about how javascript works...

Javascript fires client side. What you've written is a piece of code that
places your javascript on the browser of the client machine in the web page,
but you are not telling the client to run the code.

There are two ways to do this. One way requires a post back to the server
which is what you originally attempted. Here's how that would have to change
to work:

private void Button1_Click(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

RegisterStartupScript("bgColor",toRed);
}

But if you have to post back to the server in order to then fire your
javascript it's silly to use javascript at all; since you can just change
the background color via server side code. so here is what you probably
intended to do (Note that I've added a "return false" line to your original
javascript method and moved it to the page load):

namespace Testings
{
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\";
toRed += "return false;} ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}

//attach a javascript to your button that calls the RedIt method on the
client.
this.Button1.Attributes.Add("onclick", "javascript:RedIt();");

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

You don't even need the button click's event handler because the client
won't post back.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Bishoy George" <bi****@bishoy.com> wrote in message
news:eJ**************@TK2MSFTNGP15.phx.gbl...
// Although Page_Load is a trigerring event method.
// Here you are and still not working!!!
// -------------------------------------

Nov 19 '05 #9
try changing the script like below and if that is the result you are looking
for, please read through
http://www.c-sharpcorner.com/Code/20...eScripting.asp

private void Button1_Click(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "document.bgColor = \"red\";";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterStartupScript("bgColor",toRed);
}
}
}
"Bishoy George" wrote:
// Although Page_Load is a trigerring event method.
// Here you are and still not working!!!
// -------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}
}
}

Nov 19 '05 #10
Bruce Barker:
You just wrote what I did write.

S.Justin Gengo:
1- I know that javascript is a client side code, I want to run that client
side code with server side controls.
2- The first code you wrote actually did it
3- I would greatly appreciate you if you write to me a server side code that
can change the background color on click of a button
4- In the second code you wrote, you should write the javascript code in the
..aspx page not the .aspx.cs page so it doesn't work

Sreejith Ram:
1- Your code also worked
2- Thank you for the reading source

The conclusion:
the keyword function ****()
should only be written when you will call it later by onclick="****();" in
the inline code page
"Sreejith Ram" <Sr*********@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...

A little bit of reading on Client Side scripting with ASP may help

http://www.c-sharpcorner.com/Code/20...eScripting.asp

"Bishoy George" wrote:
// I wrote the following code but it is not working.
// Any help please?
// ------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptFromServerCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string toRed = "<script language=\"javascript\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgColor = \"red\"; } ";
toRed += "</script>";

if(!IsClientScriptBlockRegistered("bgColor"))
{
RegisterClientScriptBlock("bgColor",toRed);
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Nov 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: StealthMonkey | last post by:
Let me prefix this by saying that I know next to nothing about Java (so please try to keep explainations simple). I use PHP for my server-side web programming. Here is my dilemma: I need a...
1
by: questionr | last post by:
There is a spell checker function which is written in VB Script. The function works well when tested seperately. But when the function is called from Java Script, the function shows an Error saying...
5
by: Krishna | last post by:
Hi all, Can i call my javascript functions from the web controls.Any appropriate site which will be tell more on this will be helpfull. Regards.., Krishna
6
by: szabelin | last post by:
Hello, I am trying to call a function inside the javascript block from asp.net during the postback (NOT button's OnClick event handler though). The javascript function creates new popup window. I...
4
by: Martin Feuersteiner | last post by:
Dear Group I'm using VB to write an aspnet application. I would like to call a javascript function from within a VB Sub or VB Function, is it possible? My code is something like this: VB...
5
by: srini.venkatesan | last post by:
I am trying to call a javascript from Datagrid which is using OnUpdatecommand, I dont see update being invoked, what am I missing, Is it the right way to call. I have just cut and pasted only the...
0
by: cs2007 | last post by:
Hi, My question on custom webpart creation in c#. One quick question, can you please tell me the syntax or step to call the java script from Output.write(<a href=.....) SCENARIO: click here ...
1
by: neerom | last post by:
Hi I need to call webservice from xslt in HTTP POST Method. The MSXML should be used. I am using javascript. I have an XML File which is having Integer Node.....Shown Below <?xml version="1.0"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.