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
            global::System.Console.Write("");

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

    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") {
                    prm.abortPostBack();
                }
            })
     })


HTML :

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

        </asp:UpdatePanel>

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("~/Scripts/jquery-1.7.1.js");
            JSBundle.Include("~/Scripts/jquery-1.7.1.min.js");
            // JSBundle.Include("~/   Path of your file with ext.");
            BundleTable.Bundles.Add(JSBundle);

            var styleBundle = new StyleBundle("~/Style");
            styleBundle.Include("~/Styles/Site.css");
            // 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();
      bndle.AddBundle();
}

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">
    <title></title>

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

    <script>
        $(document).ready(function () {
            alert("done");
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

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

<system.web>
    <compilation debug="false" targetFramework="4.5"/>

  </system.web>

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


Enjoy................


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)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        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"
           ,false
          , new RouteValueDictionary { { "ID", "12" }, { "BookName", "food" } });

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

            routes.MapPageRoute("CategoryRoute"
                                             , "{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