Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Calling javascript from C#--help please

Reply
Thread Tools

Calling javascript from C#--help please

 
 
Bishoy George
Guest
Posts: n/a
 
      10-05-2005
// 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
}
}


 
Reply With Quote
 
 
 
 
S. Justin Gengo
Guest
Posts: n/a
 
      10-05-2005
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" <(E-Mail Removed)> wrote in message
news:uo1Sk$(E-Mail Removed)...
> // 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
> }
> }
>
>



 
Reply With Quote
 
 
 
 
Rob Schieber
Guest
Posts: n/a
 
      10-05-2005
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
 
Reply With Quote
 
=?Utf-8?B?Q3VydF9DIFtNVlBd?=
Guest
Posts: n/a
 
      10-05-2005
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
> }
> }
>
>
>

 
Reply With Quote
 
Bishoy George
Guest
Posts: n/a
 
      10-05-2005
// 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);
}
}
}
}


 
Reply With Quote
 
Bishoy George
Guest
Posts: n/a
 
      10-05-2005
// 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);
}
}
}
}


 
Reply With Quote
 
Bruce Barker
Guest
Posts: n/a
 
      10-05-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> // 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);
> }
> }
> }
> }
>



 
Reply With Quote
 
=?Utf-8?B?U3JlZWppdGggUmFt?=
Guest
Posts: n/a
 
      10-05-2005

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

 
Reply With Quote
 
S. Justin Gengo
Guest
Posts: n/a
 
      10-05-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> // Although Page_Load is a trigerring event method.
> // Here you are and still not working!!!
> // -------------------------------------
>


>
>



 
Reply With Quote
 
=?Utf-8?B?U3JlZWppdGggUmFt?=
Guest
Posts: n/a
 
      10-05-2005
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);
> }
> }
> }
> }
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
output text in control location; calling control javascript from page javascript Lisa Calla ASP .Net Building Controls 0 11-09-2004 02:17 AM
Calling FormsAuthentication.SignOut() after calling Response.Flush =?Utf-8?B?TWFydGluIExlZQ==?= ASP .Net 1 09-28-2004 12:47 PM
Server Side button calling page_load before calling it's own click event. Ryan Ternier ASP .Net 4 07-29-2004 01:06 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments