Calling javascript from C#--help please

Discussion in 'ASP .Net' started by Bishoy George, Oct 5, 2005.

  1. // 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
    }
    }
    Bishoy George, Oct 5, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:uo1Sk$...
    > // 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. Justin Gengo, Oct 5, 2005
    #2
    1. Advertising

  3. Bishoy George

    Rob Schieber Guest

    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
    Rob Schieber, Oct 5, 2005
    #3
  4. 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
    > }
    > }
    >
    >
    >
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=, Oct 5, 2005
    #4
  5. // 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);
    }
    }
    }
    }
    Bishoy George, Oct 5, 2005
    #5
  6. // 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);
    }
    }
    }
    }
    Bishoy George, Oct 5, 2005
    #6
  7. Bishoy George

    Bruce Barker Guest

    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" <> wrote in message
    news:...
    > // 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);
    > }
    > }
    > }
    > }
    >
    Bruce Barker, Oct 5, 2005
    #7
  8. A little bit of reading on Client Side scripting with ASP may help

    http://www.c-sharpcorner.com/Code/2002/July/CleintSideScripting.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
    > }
    > }
    >
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Oct 5, 2005
    #8
  9. 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!!!
    > // -------------------------------------
    >


    >
    >
    S. Justin Gengo, Oct 5, 2005
    #9
  10. 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);
    }
    }
    }


    "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);
    > }
    > }
    > }
    > }
    >
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Oct 5, 2005
    #10
  11. 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" <> wrote in message
    news:...
    >
    > A little bit of reading on Client Side scripting with ASP may help
    >
    > http://www.c-sharpcorner.com/Code/2002/July/CleintSideScripting.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
    >> }
    >> }
    >>
    >>
    >>
    Bishoy George, Oct 5, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    502
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    546
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,740
    Mike Hewson
    Jan 7, 2005
  4. mera
    Replies:
    7
    Views:
    1,096
  5. Lisa Calla
    Replies:
    0
    Views:
    270
    Lisa Calla
    Nov 9, 2004
Loading...

Share This Page