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" <> wrote in message
news:...
> // Although Page_Load is a trigerring event method.
> // Here you are and still not working!!!
> // -------------------------------------
>
>
>