How to hide generated javascript file in visual studio code

In this post I am going to show you a cool trick in visual studio code. Let's suppose you are writing typescript code in visual studio code and you want to hide generated javascript code.

Create .vscode folder in your folder and then create settings.json file and paste following code

{ "files.exclude": { "node_modules":true, "**/.git": true, "**/.DS_Store": true, "**/*.js.map": true, "**/*.js": {"when": "$(basename).ts"}     } }

Calling page methods from javascript using ASP.NET AJAX

We can call page methods (static methods declared in scope of asp.net page) using MS Ajax framework for ASP.NET. That is how can we do it : 1) Drop ScriptManager control on page 2) Set EnablePageMethods property of ScriptManager control to true 3) Add static public method in page-behind code (or its parent class) and mark it with [System.Web.Services.WebMethod] attribute: 4) add following javascript code to it
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Untitled Page</title>

   <script type="text/javascript">
   function CallMethod()
   {
       GetTime();
   }

   function GetTime()
 
   {
 
       PageMethods.GetTime(GetTimeCallback,ErrorHandler,TimeOutHandler);
                  
 
   }
   function TimeOutHandler(result)
   {
        alert("Timeout :" + result);
   }
   /// <summary>
   /// Callback function invoked on failure of the page method 
   /// </summary>
 
   function ErrorHandler(result)
   {
       var msg=result.get_exceptionType() + "\r\n";
       msg += result.get_message() + "\r\n";
       msg += result.get_stackTrace();
       alert(msg);
   }
  GetTimeCallback = function (result) {
   /// <summary>
   /// Is called when server sent result back
   /// </summary>
   /// <param name="result">
   /// Result of calling server method, 
   /// string - server time 
   /// </param>
   if(result) {
        $get("resultDiv").innerHTML = result;
   }
}
   </script>

</head>
<body>
   <form id="form1" runat="server">
       <asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release"
           LoadScriptsBeforeUI="true">
       </asp:ScriptManager>
       <div id="resultDiv">
       </div>
       <input value="GetTime" type="button" onclick="GetTime();" />
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {

   }
   [System.Web.Services.WebMethod]
   public static string GetTime()
   {
       return DateTime.Now.ToString();
   }

}

Comments

  1. Your blog is very nice...
    visit my blog asp.net example

    ReplyDelete
  2. Nice example, but not very practical. The C# code-behind method has to be static for the javascript PageMethods to find it. If you need to do things to controls on the page, the C# functions have to be dynamic, not static, or you have to do all sorts of workarounds to get the controls (i.e. Page page = new Page(), page.FindControl("myTextBox"), etc.) and those don't instantiate the actual properties on those controls, like .Text, for example. If you had a way to call a dynamic method, that would be da bomb...

    ReplyDelete
  3. You can pass the control's clientid in PageMethods

    ReplyDelete
  4. i want to do validation on client side but calling server side method in javascript. if you have any solution please email me on patel.jay.40@gmail.com

    ReplyDelete
  5. Doing the same as you did but my code is not working any reason for that. Can some one explain me why it is not working on my side

    ReplyDelete
  6. helpful article.............

    ReplyDelete
  7. 3 different ways to implement Ajax in Asp.Net applitacion

    http://www.docstorus.com/viewer.aspx?code=436b712f-2abb-45c0-9674-5f6e6a686335

    ReplyDelete

Post a Comment

Popular posts from this blog

How to get cell value of gridview using JQUERY

how to convert web page to pdf

Export Html to Pdf using iTextSharp(GridView)