Calling javascript from C#--help please

B

Bishoy George

// 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
}
}
 
S

S. Justin Gengo

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
 
R

Rob Schieber

Bishoy said:
// 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?
 
B

Bishoy George

// 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);
}
}
}
}
 
B

Bishoy George

// 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);
}
}
}
}
 
B

Bruce Barker

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)
 
S

S. Justin Gengo

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
 
G

Guest

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/2002/July/CleintSideScripting.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);
}
}
}
 
B

Bishoy George

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top