473,800 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling javascript from C#--help please

// I wrote the following code but it is not working.
// Any help please?
// ------------------------------------------------
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptF romServerCode : System.Web.UI.P age
{
private void Page_Load(objec t sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",toRed);
}
}

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

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

}
#endregion
}
}
Nov 19 '05 #1
10 1590
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******** ******@TK2MSFTN GP14.phx.gbl...
// I wrote the following code but it is not working.
// Any help please?
// ------------------------------------------------
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptF romServerCode : System.Web.UI.P age
{
private void Page_Load(objec t sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",toRed);
}
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptF romServerCode : System.Web.UI.P age
{
private void Page_Load(objec t sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",toRed);
}
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptF romServerCode : System.Web.UI.P age
{
private void Page_Load(objec t sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",toRed);
}
}

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

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

}
#endregion
}
}

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

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Testings
{
public class CallJavaScriptF romServerCode : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on Button1;

private void Page_Load(objec t sender, System.EventArg s e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void Button1_Click(o bject sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",toRed);
}
}
}
}
Nov 19 '05 #5
// Although Page_Load is a trigerring event method.
// Here you are and still not working!!!
// -------------------------------------

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Testings
{
public class CallJavaScriptF romServerCode : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on Button1;

private void Page_Load(objec t sender, System.EventArg s e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void Button1_Click(o bject sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",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=\"java script\">docume nt.bgColor =
\"red\"; </script>";

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

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Testings
{
public class CallJavaScriptF romServerCode : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on Button1;

private void Page_Load(objec t sender, System.EventArg s e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void Button1_Click(o bject sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Testings
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class CallJavaScriptF romServerCode : System.Web.UI.P age
{
private void Page_Load(objec t sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",toRed);
}
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(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(o bject sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

RegisterStartup Script("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 CallJavaScriptF romServerCode : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on Button1;

private void Page_Load(objec t sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\";
toRed += "return false;} ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",toRed);
}

//attach a javascript to your button that calls the RedIt method on the
client.
this.Button1.At tributes.Add("o nclick", "javascript:Red It();");

}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
this.Load += new System.EventHan dler(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******** ******@TK2MSFTN GP15.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(o bject sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "document.bgCol or = \"red\";";
toRed += "</script>";

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

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Testings
{
public class CallJavaScriptF romServerCode : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on Button1;

private void Page_Load(objec t sender, System.EventArg s e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void Button1_Click(o bject sender, System.EventArg s e)
{
string toRed = "<script language=\"java script\"> ";
toRed += "function RedIt() { ";
toRed += "document.bgCol or = \"red\"; } ";
toRed += "</script>";

if(!IsClientScr iptBlockRegiste red("bgColor"))
{
RegisterClientS criptBlock("bgC olor",toRed);
}
}
}
}

Nov 19 '05 #10

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

Similar topics

3
5194
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 JavaScript function to be called with a dynamic parameter, an ID# known by the server. The way I would go about doing this in PHP would be extremely simple: <script> foo(<?PHP echo ID; ?>);
1
1672
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 " A run time error has occured. Do you wish to debug? Line :0 , Object Expected". I just know that it is a Java Script error. Any suggestions on how to resolve this problem ? What could be the possible reasons for this error message?
5
51483
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
751
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 register the javascript code with the RegisterClientScriptBlock. How how do I call the javascript function inside the block from within C# asp.net page. (yes I know popups are bad, please do not reply if you
4
6336
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 Code: Public Sub VBProcedure() .. ..
5
1721
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 part of the code. Thanks In the class file: public void DataGrid2_Edit(Object sender, DataGridCommandEventArgs e) { DataGrid2.EditItemIndex = (int)e.Item.ItemIndex; BindGrid();
0
1253
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 should act as a link. And the link should invoke javascript. for eg: output.Write("<a href="#"" onclick="javascript:validateSubmit();" ">" Click here "</a>"); Here Im getting following errors on compilation 1. "Preprocessor directives must appear...
1
2005
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" encoding="utf-8" ?> <NumberSet> <integer> <a>10</a> <b>12</b>
0
10501
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10273
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10250
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7574
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.