Tuesday 22 July 2014

Use of global contextual keyword

The global is contextual keyword, when it comes before the :: operator, refers to the global namespace of .Net

Check example :

namespace TestWinform.test
    public class globalClass
        public void TestMethod()
            // Here you can able to access Default system Namespace of .Net framework

            // This is the class of   "TestWinform.test"  namespace
            System sys = new System();

    public class System
        public void Console()


Thursday 17 July 2014

Stop partial Postback of Form using Javascript

javaScript to stop partial postback of form.

    $(document).ready(function () {
            $("form").submit(function (e) {
                var prm = Sys.WebForms.PageRequestManager.getInstance();
                var v = prm.get_isInAsyncPostBack()
                if ($("#txt").val() == "1") {


       <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <input type="text" id="txt" />
                <asp:Button ID="btn" runat="server" CssClass="temp" Text="Click" />


Bundling in Asp.Net

Install necessary Dlls using Package Manager console.

PM> Install-Package Microsoft.AspNet.Web.Optimization

Now add a class file and copy below code in it : 

using System.Web.Optimization;
public class Bundle

        public void AddBundle()
var JSBundle = new ScriptBundle("~/JSfiles");
            // JSBundle.Include("~/   Path of your file with ext.");

            var styleBundle = new StyleBundle("~/Style");
            // styleBundle.Include("~/   Path of your file with ext.");
            BundleTable.Bundles.Add(styleBundle);        }

Register your bundle in Application_Start event in Global.asax : 

protected void Application_Start(object sender, EventArgs e)
      Bundle bndle = new Bundle();

Now Take .aspx page in which you want to render your bundled css or JS files.

<%@ Page Language="C#" AutoEventWireup="true"
   CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<!DOCTYPE html >

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

 <%:  System.Web.Optimization.Scripts.Render("~/JSfiles") %>
 <%: System.Web.Optimization.Styles.Render("~/Style") %>

        $(document).ready(function () {
    <form id="form1" runat="server">

 Now in Web.config. If you want to enable bundling for your application then you have to make debug = false.

    <compilation debug="false" targetFramework="4.5"/>


Now check if your bundling is working or Not ?

Press F12 in IE.  Then Script Tab and check in drop-down in right side. You will have name of the JS file which you have given in application. if you click on that you will find minified JS of all JS. You will not found actual path of your JS. see Page Source


Monday 7 July 2014

Url Routing in Asp.Net

Write below code in Global.asax :

      using System.Web.Routing;

       protected void Application_Start(object sender, EventArgs e)

        void RegisterRoutes(RouteCollection routes)
            //routes.MapPageRoute("Name of Route"
, "strict Name/{string/int}/{string/int}"
, "Actual Path to Redirect"
, false
, new RouteValueDictionary { { "ID", "12" }, { "BookName", "food" } });
             //  SET DEfault Values for RouteData using 
                          new RouteValueDictionary { { "ID", "12" }, { "BookName", "food" } })

            // you can Use strict Name in Url with directly write string.
                If you want variable string in urk then User {}  bracket

              routes.MapPageRoute("RouteBook", "Book/{ID}/{BookName}", "~/Webform1.aspx"
          , new RouteValueDictionary { { "ID", "12" }, { "BookName", "food" } });

            //  Url will     http://localhost/Book/12/CSharpCoding

                                             , "{Category}/{ID}"
                                            , "~/LoginForm.aspx");

            // Url Will     http://localhost/categoryName/12


Get Querystring Value in Web Page :

string str = Page.RouteData.Values["BookName"] as string;

// "BookName"  = Name of the parameter you have passed while routing