Apr 16, 2012

COMET or Reverse Ajax

This concept is the solution of problem that,


How to send data from a server to a client without a native HTTP request made by client.This can be implemented in Chatting Applications, Automatic updates of some data.


So what is this ? 
Comet is a programming technique that enables web servers to send data to the client without having any need for the client to request it. It allows creation of event-driven web applications which are hosted in the browser.
Try reading from this . http://en.wikipedia.org/wiki/Comet_%28programming%29

From where do i got to know of this is http://stackoverflow.com/questions/3226688/web-chat-application-asp-net-jabber-ajax-wcf-comet-reverseajax-issues-faced

Here is finally wot i got and believe me its Awesome :)


1. create a new c# web application
2. Add a new page name it Services.aspx
3. in the code file i.e. Services.aspx.cs add the following code.
using System;
using System.Data;
using System.Configuration;
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 Service : System.Web.UI.Page 
{
    public static string Delimiter = "|";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = false;

        while (true)
        {
            Response.Write(Delimiter + DateTime.Now.ToString("HH:mm:ss.FFF"));
            Response.Flush();

            // Suspend the thread for 1/2 a second
            System.Threading.Thread.Sleep(500);
        }

        // Yes I know we'll never get here, it's just hard not to include it!
        Response.End();
    }
}

4. Good Now add a html page say home.html and add this code to it.


<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Comet AJAX Sample</title>
    
    <script language="javascript">
      function getData()
      {
          loadXMLDoc("Service.aspx");
      }
        
      var req = false;
        
      function createRequest()
      {
        // branch for native XMLHttpRequest object
        if(window.XMLHttpRequest && !(window.ActiveXObject))
        {
          try {
            req = new XMLHttpRequest();
          } catch(e) {
   req = false;
          }
        // branch for IE/Windows ActiveX version
        } else if(window.ActiveXObject) {
          try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
          } catch(e) {
            try {
       req = new ActiveXObject("Microsoft.XMLHTTP");
       } catch(e) {
       req = false;
            }
     }
        }
      }
        
      function loadXMLDoc(url) {
          try
          {
              if (req) {
                  req.abort();
                  req = false;
              }             
                
              createRequest();
              
              if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send("");
         }
         else {
             alert('unable to create request');
         }
     }
     catch (e) {
         alert(e.message);
     }
      }
        
      function processReqChange() {
          if (req.readyState == 3) {
              try
              {
                  ProcessInput(req.responseText);
                    
                  // At some (artibrary) length 
                  // recycle the connection
                  if (req.responseText.length > 3000) {
                      lastDelimiterPosition = -1;
                      getData();
                  }
              }
              catch (e) {
                  alert(e.message);
              }
          }
      }
        
      var lastDelimiterPosition = -1;
        
      function ProcessInput(input)
      {
          // Make a copy of the input
          var text = input;
          // Search for the last instance of the delimiter
          var nextDelimiter = 
                text.indexOf('|', lastDelimiterPosition+1);
          if (nextDelimiter != -1) {
              // Pull out the latest message
              var timeStamp = text.substring(nextDelimiter+1);
              if (timeStamp.length > 0) {
                  lastDelimiterPosition = nextDelimiter;
                  ProcessTime(timeStamp);
              }
          }
      }
        
      function ProcessTime(time)
      {
          var out = document.getElementById('outputZone');
          out.innerHTML = time;
      }
    </script>
</head>
<body onload="getData()">
    <b>Server Time:</b>&nbsp;&nbsp;<span id="outputZone"></span>
</body>
</html>

5. And yes you have done it Now run your html page.

You will see that the time is Continuously updating and yeah this is server time also check out that the browser is not sending any AJAX requests.


Apr 14, 2012

Adding a custom control Dynamically to a page in ASP.NET

Adding a custom control Dynamically to a page in ASP.NET

1. Create a control .ascx file into your project Header.ascx (say).

2. Then add a refrence to your control

<%@ Reference Control="~/modules/controls/Header.ascx"%>


3. Then in C# use below code
     (controls are listed in ASP namespace)
ASP.Header header_ctrl= (ASP.Header)LoadControl("~/Header.ascx"); 
Placeholder.Controls.Add(header_ctrl); 

here  Header is custom control and Placeholder is a panel.


Jan 25, 2011

Getting Started With PHP

PHP - Stands for Hypertext PreProcessor


What is PHP ?
PHP is a server side scripting language, it is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document. As a general-purpose programming language, PHP code is processed by an interpreter application in command-line  mode performing desired operating system operations and producing program output on its standard output channel. It may also function as a graphical application. PHP is available as a processor for most modern web servers and as a standalone interpreter on most operating systems and computing platforms.. It is An open source language So feel free to create your website.

How to begin with PHP ? 
To Start doing Php all you need is a php server eg. Apache Or Wamp Server will do. in these tutorials im using the wamp server for testing. you can Download it from here  .after installing start the wamp server then at the taskbar click on the icon of of wamp server >> localhost if the webpage of wamp sserver is displayed then it is ok to proceed. now you have to place all your php files in the www directory of wamp server. you can reach there by click wamp server in taskbar>> www directory and paste your php file there, then open the wamp server start page again this time you will find the name of your folder or php file on the page click it and select the file to run.


Programming Style: 
The PHP interpreter only executes PHP code within its delimiters. Anything outside its delimiters is not processed by PHP (although non-PHP text is still subject to control structures described within PHP code). The most common delimiters are <?php to open and ?> to close PHP sections. <script language="php"> and </script> delimiters are also available, as are the shortened forms <? or <?= (which is used to echo back a string or variable) and ?> as well as ASP-style short forms <% or <%= and %>. While short delimiters are used, they make script files less portable as support for them can be disabled in the PHP configuration, and so they are discouraged. The purpose of all these delimiters is to separate PHP code from non-PHP code, including HTML.

The first form of delimiters, <?php and ?>, in XHTML and other XML documents, creates correctly formed XML 'processing instructions'.This means that the resulting mixture of PHP code and other markup in the server-side file is itself well-formed XML.

Variables are prefixed with a dollar symbol and a type does not need to be specified in advance. Unlike function and class names, variable names are case sensitive. Both double-quoted ("") and heredoc strings allow the ability to embed a variable's value into the string. PHP treats newlines as whitespace in the manner of a free-form language (except when inside string quotes), and statements are terminated by a semicolon. PHP has three types of comment syntax: /* */ marks block and inline comments; // as well as # are used for one-line comments. The echo statement is one of several facilities PHP provides to output text (e.g. to a web browser).

In terms of keywords and language syntax, PHP is similar to most high level languages that follow the C style syntax. if conditions, for and while loops, and function returns are similar in syntax to languages such as C, C++, Java and Perl.


Example :


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>PHP Test</title>
  </head>
  <body>
  <?php
  echo 'Hello World';
  /* echo("Hello World"); works as well, although echo isn't a
  function, but a language construct. In some cases, such
  as when multiple parameters are passed to echo, parameters
  cannot be enclosed in parentheses. */
  ?>
  </body>
</html>