Tags: call, code, displaymap, forms, function, myjs, net, web

How to call JS function From C# Code Behind

On .Net » Web Forms

2,459 words with 4 Comments; publish: Sun, 06 Jan 2008 06:18:00 GMT; (10062.50, « »)

i want to call a function DisplayMap that is in my MyJS.js from C# code behind.

All Comments

Leave a comment...

  • 4 Comments
    • You can't.

      At thepoint that your code-behind is being processed, the page, and thereforethe javascript has not yet been created, so it doesn't exist. Once theresponse is sent to the browser, and the javascript exists, yourcode-behind no longer exists in the context of that particular page.

      #1; Sun, 06 Jan 2008 06:19:00 GMT
    • JS code is being run on the client side, code behind code is being run in the server side. Thus, you can't call smth. on the client from the server.

      What you can is to call your js function when your page gets appeared on the client, try this one:

      Page.ClientScript.RegisterStartupScript(GetType(),"myKey", "DisplayMap()",true);
      #2; Sun, 06 Jan 2008 06:20:00 GMT
    • its fairly easy...

      just add your MyJs.js in aspx page like below...

      <head runat="server">

      <title>Untitled Page</title>

      <script type="text/javascript" language="javascript" src="/app/links/?src=MyJs.js"></script>

      </head>

      <body>

      <form id="form1" runat="server">............

      suppose there is just one function in this MyJs.js file like below.,

      function test()

      {

      alert('Hello');

      }

      then this can be cold in this way...

      Page_load()

      {

      if (!Page.IsPostBack)

      Button1.Attributes.Add("onclick", "test();");

      }

      hope this helps./.

      #3; Sun, 06 Jan 2008 06:21:00 GMT
    • Hello ,

      You can use this.

      if
      (e.Item.ItemType ==ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem)

      {

      Button _myButton = (Button)e.Item.FindControl("button3");_myButton.Attributes.Add("onclick","return confirm_delete();");

      }

      Regards

      #4; Sun, 06 Jan 2008 06:22:00 GMT