Saturday, 25 April 2015

jQuery UI DatePicker (Calendar) Example in ASP.Net

In this article I am explain how to use the jQuery UI DatePicker plugin with the ASP.Net TextBox Control. First of all, to start with jQuery UI, you need to download latest jQuery UI from here or you can use jQuery cdn.

To start first,we need to include jQueryUI js and jQueryUI Css file on top.
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
  <script src="//code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js" type="text/javascript"></script>

Place a textbox on the page, which will be used to bind the datepicker control to it.
<asp:TextBox ID="txtDate" runat="server" ReadOnly="true"></asp:TextBox>

Now let's place the datepicker control.
<script type="text/javascript">
    $(function () {
      $("[id$=txtDate]").datepicker();
    });
  </script>

Designer Source Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>jQuery DatePicker to ASP.Net TextBox</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
  <script src="//code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $("[id$=txtDate]").datepicker({
        changeYear: true,
        showOn: 'button',
        buttonImageOnly: true,
        buttonImage: 'calendar.png'
      });
    });
  </script>
  <script type="text/javascript">// calender with month and year
    $(function () {
      $("[id$=txtDate1]").datepicker({
        changeMonth: true,
        changeYear: true,
        showOn: 'button',
        buttonImageOnly: true,
        buttonImage: 'calendar.png'
      });
    });
  </script>
  <script type="text/javascript">    // calender with month and show current date
    $(function () {
      $("[id$=txtDate2]").datepicker({
        changeMonth: true,
        showOn: 'button',
        buttonImageOnly: true,
        buttonImage: 'calendar.png',
        showAnim: 'fadeIn',
        buttonText: 'Show Date'
      });
      $("#<%=txtDate2.ClientID%>").datepicker('setDate', 'today');

    });
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:TextBox ID="txtDate" runat="server" ReadOnly="true"></asp:TextBox>
    <br />
    <br />
    <asp:TextBox ID="txtDate1" runat="server" ReadOnly="true"></asp:TextBox><br />
    <br />
    <asp:TextBox ID="txtDate2" runat="server" ReadOnly="true"></asp:TextBox><br />
    <br />
    <br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
  </div>
  </form>
</body>
</html>

Code Behind:
 Fetch the value of the TextBox server side
protected void btnSubmit_Click(object sender, EventArgs e)
    {
      string date = Request.Form[txtDate.UniqueID] + "   " + Request.Form[txtDate1.UniqueID];
      ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + date + "');", true);
    }


To see list of all the option please see here.

Saturday, 11 April 2015

How to define and use Enum in JavaScript

In this article I am explain how to use Enumeration in Javascript. JavaScript does not have any Enum data type but still we can create an array of key value pair and use it as Enum.
Enum_in_javascript.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
  <script type="text/javascript">
    var country = { "India": 1, "England": 2, "Australia": 3 };
    function DropdownChange(d) {
      switch (parseInt(d.value)) {
        case country.India:
          alert("Selected Country India");
          break;
        case country.England:
          alert("Selected Country England");
          break;
        case country.Australia:
          alert("Selected Country Australia");
          break;
      }
    }
  </script>
</head>
<body>
  <form>
  <select onchange="DropdownChange(this)">
    <option value="1">India</option>
    <option value="2">England</option>
    <option value="3">Australia</option>
  </select>
  </form>
</body>
</html>


In the above example I have created a simple enum of 3 Country India, England and Australia to which I have assigned integer values 1, 2 and 3 respectively. Now on the change of the HTML SELECT dropdown the function DropdownChange is called which based on the selected value displays the selected Country in Alert.

Thursday, 2 April 2015

The global.asax file in ASP.NET

The global.asax file in ASP.NET as an application file, which is optional and allows us to write code that response to global application events raised by ASP.NET or by HttpModules. These events fire at various points during the lifetime of a web application, including when the application domain is first created.

The global.asax file resides in the root directory of an ASP.NET application. At run time, global.asax is parsed and compiled into a dynamically generated .NET Framework class derived from the HttpApplication base class.

The global.asax file looks very similar to a normal .aspx file. However, instead of containing HTML or ASP.NET tags, it contains event handlers that response to application events. Each ASP.NET application can have only one global.asax file. Once you place it in the appropriate website directory, ASP.NET recognizes it and uses it automatically.

The ASP.NET global.asax file can coexist with the ASP global.asa file.

Basic Application Events

S. No.
Event Handling Method
Description
1
Application_Start()
Application_Start() event occurs when the application starts, which is the first time it receives a request from any user. It doesn’t occur on subsequent requests. This event is commonly used to create or cache some initial information that will be reused later.
2
Application_End()
Application_End() event occurs when the application is shutting down, generally because the web server has restarted. You can insert cleanup code here.
3
Application_BeginRequest()
Application_BeginRequest() event occurs with each request the application receives, just before the page code is executed.
4
Application_EndRequest()
Application_EndRequest() event occurs with each request the application receives, just after the page code is executed.
5
Session_Start()
Session_Start() event occurs whenever a new user request is received and a session is started.
6
Session_End()
Session_End() event occurs when a session times out or is programmatically ended. This event is only raised if you are using in-process session state storage (the InProc mode, not the StateServer or SQLServer modes ).
7
Application_Error()
Application_Error() event occurs in response to an un-handled error.