Friday, April 26, 2013

Delegate in C#

==>>A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure..

==>>A Delegate is a special type  function means it can point to those function which are having identical signature with delegate.
you can also call delegate as a interface of methods as this acts same as interface in c# but that implemented for classes and this is implemented for methods..





* Step to create a delegate:
1 . Declaration
2. Instantiation
3. Invocation 




* Why we need Delegate.......
  1. We can call many of method by single delegate no need of writ many of function.
  2. We  set up event based system easily.
  3. We can calling many method that in different but having same signature.
  4. We can  passing in parameter field a function. 
  5. we can use select method.

 You take a new console application in visual studio and write the sample code in cs file:---

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    public delegate string MyDelegate();//Declaration

    public class TestDelegat
    {
        public string TestMethod()
        {
            return "This is Delegate Sample Program in C#.";

        }

    }

    class Program
    {

        static void Main(string[] args)
        {
            TestDelegat TD = new TestDelegat();
            MyDelegate MDel = new MyDelegate(TD.TestMethod);//Insatantiation
            Console.Write("Output: " + MDel());//Invocation


        }
    }
}


 first we define a delegate name with its signature,in this case its return type is string and no arguments. then we create instance of delegate which is MyDelegate now this can point to (reference to) all those function which having same signature as in our case function is TestMethod() and at the last we invoke this and receive this in message.


The main advantage of delegate is multi cast ...

*For use multi cast we need write this code

In The Class..

  public void TestMethodMulticast()
        {
           console.write("Multicast");

        }


and in the Static Void Main


MyDelegate  TD= null;
            TD+= new MyDelegate  (TD.TestMethod);
            TD+= new MyDelegate  (TD.TestMethodMulticast);



Hope this post will help you for find your solution

Happy Coding



Thursday, April 18, 2013

Get Selected Value on Dropdownlist selectedindexchanged event using JQuery

==>> In this post  I explain how Get Selected Value on Dropdownlist selectedindexchanged event using  JQuery this is help us for get value of dropdown without any page post back in asp .net

We use the change event  in the JQuery for get value of dropdown 
on Dropdownlist selectedindexchanged event in JQuery

In the JQuery we write code like :-


 <script type="text/javascript">
        $(document).ready(function() {
            $('#ddlName').change(function() {
                $("#lName").text($("#ddlName option:selected").text());
                $("#lValue").text($("#ddlName").val());
                return false;
            })
        });
    </script>


.ASPX  Page Code:-



<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Get Selected Value on Dropdownlist selectedindexchanged event using JQuery
    </title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#ddlName').change(function() {
                $("#lName").text($("#ddlName option:selected").text());
                $("#lValue").text($("#ddlName").val());
                return false;
            })
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <table width="50%">
        <tr>
            <td align="right">
                Select Name:
            </td>
            <td>
                <select id="ddlName" onchange="GetName()" runat="server">
                    <option value="0">Select</option>
                    <option value="1">Subhash</option>
                    <option value="2">Rakesh</option>
                    <option value="3">Satendra</option>
                    <option value="4">Raju</option>
                    <option value="5">Sachin</option>
                </select>
               
            </td>
        </tr>
        <tr>
            <td align="right" style="font-family: Century Gothic; font-size: 12pt; color: Black">
                Selected Name:-
            </td>
            <td>
                <b>
                    <label id="lName" style="font-family: Century Gothic; font-size: 12pt; color: Aqua" />
                </b>
            </td>
        </tr>
        <tr>
            <td align="right" style="font-family: Century Gothic; font-size: 12pt; color: Black">
                Selected Name value:-
            </td>
            <td>
                <b>
                    <label id="lValue" style="font-family: Century Gothic; font-size: 12pt; color: Aqua" />
                </b>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>




 



DEMO:-



Get Selected Value on Dropdownlist selectedindexchanged event using JQuery
Select Name:
Selected Name:-
Selected Name value:-

Get Selected Value on Dropdownlist selectedindexchanged event using JavaScript

==>> In this post  I explain how Get Selected Value on Dropdownlist selectedindexchanged event using  JavaScript this is help us for get value of dropdown without any page post back in asp .net

We use the onchange event  in the java script for get value of dropdown 
on Dropdownlist selectedindexchanged event in JavaScript 

In the java script we write code like :-

<script type="text/javascript">
function GetName() {
    var name = document.getElementById("ddlName");
    var strname = name.options[name.selectedIndex].text;
    document.getElementById('lName').innerHTML = strname;
    document.getElementById('lValue').innerHTML = name.options[name.selectedIndex].value;
}
    </script>


.ASPX  Page Code:-


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Get Selected Value on Dropdownlist selectedindexchanged event using  JavaScript</title>

    <script type="text/javascript">
function GetName() {
    var name = document.getElementById("ddlName");
    var strname = name.options[name.selectedIndex].text;
    document.getElementById('lName').innerHTML = strname;
    document.getElementById('lValue').innerHTML = name.options[name.selectedIndex].value;
}
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <table width="50%">
        <tr>
            <td align="right">
                Select Name:
            </td>
            <td>
                <asp:DropDownList ID="ddlName" runat="server" onchange="GetName()">
                    <asp:ListItem Text="Select" Value="0" />
                    <asp:ListItem Text="Subhash" Value="1" />
                    <asp:ListItem Text="Rakesh" Value="2" />
                    <asp:ListItem Text="Satendra" Value="3" />
                    <asp:ListItem Text="Raju" Value="4" />
                    <asp:ListItem Text="Sachin" Value="5" />
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td align="right" style="font-family: Century Gothic; font-size: 12pt; color: Black">
                Selected Name:-
            </td>
            <td>
                <b>
                    <label id="lName" style="font-family: Century Gothic; font-size: 12pt; color: Aqua" />
                </b>
            </td>
        </tr>
        <tr>
            <td align="right" style="font-family: Century Gothic; font-size: 12pt; color: Black">
                Selected Name value:-
            </td>
            <td>
                <b>
                    <label id="lValue" style="font-family: Century Gothic; font-size: 12pt; color: Aqua"/>
                </b>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>


Get Selected Value on Dropdownlist selectedindexchanged event using JavaScript
Select Name:
Selected Name:-
Selected Name value:-

Tuesday, April 16, 2013

Async File Upload control example in C# asp.net to upload files to server using Ajax

==>> In this post  I explain how to use Ajax AsyncFileUpload  file upload in C# Asp .Net and showing  progress bar  when file uploading and confirmation message of Success and Fail

 AsyncFileUpload Control Features:-- 

   1  AsyncFileUpload works within the Update Panel
   2  AsyncFileUpload  uploads the file without any postback
   3  AsyncFileUpload  provides Client Side and Server side events
   4  There are different coloring options for showing file upload. As for example, it shows green   color if upload is successful, otherwise it shows red if there is unsuccessful upload.
   5  You can show the loading image while file uploading is in progress.

First you need install AJAX ,create a website add a folder name 'Store'  After that you need add AjaxControlToolkit reference to your website  add the line in your aspx page that given below...... 

   

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>




1.  ASPX PAGE CODE:-



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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>***Ajax Files Uploader Sample***</title>

    <script type="text/javascript">

        function uploadSuccess() {
            document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "Your File Uploaded Successfully";
        }

        function uploadFail() {
            document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "Your File upload Failed.";
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <ajax:ToolkitScriptManager ID="scriptManager1" runat="server" />
    <div>
        <ajax:AsyncFileUpload ID="AjaxSamplefileUpload" OnClientUploadComplete="uploadSuccess"
            OnClientUploadError="uploadFail" CompleteBackColor="White" Width="350px" runat="server"
            UploaderStyle="Modern" UploadingBackColor="#CCFFFF" ThrobberID="imgLoading" OnUploadedComplete="fileUploadSuccess" />
        <br />
        <asp:Image ID="imgLoading" runat="server" ImageUrl="loading.gif" />
        <br />
        <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>


AsyncFileUpload Events, Properties and Methods  :-


Events

  • UploadedComplete :- Fired on the server side when the file successfully uploaded.
  • UploadedFileError :- Fired on the server side when the uloaded file is corrupted.
 Properties

  • CompleteBackColor:- The control's background color on upload complete. Default value - 'Lime'.
  • ContentType :- Gets the MIME content type of a file sent by a client.
  • ErrorBackColor :- The control's background color on upload error. Default value - 'Red'.
  • FileContent :- Gets a Stream object that points to an uploaded file to prepare for reading the contents of the file.
  • FileName :- Gets the name of a file on a client to upload using the control. 
  • HasFile :- Gets a bool value indicating whether the control contains a file.
  • OnClientUploadComplete :- The name of a javascript function executed in the client-side after the file successfully uploaded
  • OnClientUploadError :- The name of a javascript function executed in the client-side if the file uploading failed.
  • OnClientUploadStarted :-The name of a javascript function executed in the client-side on the file uploading started.
  • ThrobberID :- ID of control that is shown while the file is uploading. 
  • UploaderStyle :- The control's appearance 2 style Traditional and Modern. Default value - 'Traditional'.
Methods

  • SaveAs(string filename) - Saves the contents of an uploaded file. 



2. ASPX.CS PAGE CODE:-   




using System;
using System.Threading;
using System.Web.UI;
using AjaxControlToolkit;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void fileUploadSuccess(object sender, AsyncFileUploadEventArgs e)
    {
        Thread.Sleep(1000);
        string FileName = System.IO.Path.GetFileName(AjaxSamplefileUpload.FileName);
        AjaxSamplefileUpload.SaveAs(Server.MapPath("Store/") + FileName);
    }
}



Download sample code 

Thursday, April 11, 2013

Asp.Net Textbox Readonly using JQuery

==>> In this post i want to make asp .net text box read only using JQuery .Some time we need this type of functionality  in your website so you can easily implement  in your website just copy paste blow code on your aspx page   :----

1.  ASPX PAGE CODE:-

 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Asp.Net Textbox Readonly using JQuery</title>
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#<%=txtBoxName.ClientID%>').attr('readonly', true);
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtBoxName" runat="server" />
</div>
</form>
</body>
</html>



Hope this will help you...

Asp.Net Textbox Readonly using JavaScript

==>> In this post i want to make asp .net text box read only using java script .Some time we need this type of functionality  in your website so you can easily implement  in your website just copy paste blow code on your aspx page   :----

1.  ASPX PAGE CODE:-  
  


 <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Asp.Net Textbox Readonly using JavaScript </title>
<script type="text/javascript">
    function SettxtboxReadOnly() {
    var txtName = document.getElementById("txtboxName");
txtName.readOnly = "readonly";
}
</script>
</head>
<body onload=" SettxtboxReadOnly()">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtboxName" runat="server" Text="Read Only"></asp:TextBox>
</div>
</form>
</body>
</html>



Mouse or Cursor Position on the web page using JQuery in Asp .Net

==>> In this post i want to get current mouse or cursor position using JQuery :----

you just need paste this code on .aspx  page you will find the your cursor position in browser....



1.  ASPX PAGE CODE:-  



 <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Mouse or Cursor Position on the web page using JQuery in Asp .Net</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$(document).mousemove(function(e) {
$('#lblAxistxt').html("X Axis: "+e.pageX+"<br/>"+ "Y Axis: "+e.pageY);
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label id="lblAxistxt" style="background-color:Aqua;" />
</div>
</form>
</body>
</html>


DEMO

Mouse or Cursor Position on the web page using JQuery in Asp .Net

Wednesday, April 10, 2013

Calculating difference between two date in Years, Months and Days & Geting Age from DOB to current date in C# ASP .NET

==>> Some time we need calculate date in project i need get Year Month and Day from the date of birth to current date so i work on find this solution and now sharing with you .This is perfect solution for find difference between two date.



You need create asp .net  website and add page that having name DateDiffe.aspx . You paste the below code according to page extension :-


1.  ASPX PAGE CODE:-  


 <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td>
                    Date 1</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Date 2</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="DateDiffBtn" runat="server" Text="Click"
                        onclick="DateDiffBtn_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                </td>
            </tr>
        </table>
   
    </div>
    </form>




2. ASPX.CS PAGE CODE:-  



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DateDiffe : System.Web.UI.Page
{
    private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    private DateTime fromDate;
    private DateTime toDate;
    private int year;
    private int month;
    private int day;
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Text = DateTime.Now.Date.ToString();
    }
    protected void DateDiffBtn_Click(object sender, EventArgs e)
    {      
       

       
        DateTime dtDOB = new DateTime();

        dtDOB = Convert.ToDateTime(TextBox1.Text);
      
        int Year1 = dtDOB.Year;
        int Month1 = dtDOB.Month;
        int Date1 = dtDOB.Day;

        int Year2 = DateTime.Now.Year;
        int Month2 = DateTime.Now.Month;
        int Date2 = DateTime.Now.Day;

        int Month = 0;
        int Year = 0;
        int Date = 0;

       
       

        fromDate = Convert.ToDateTime(TextBox1.Text);
        toDate = Convert.ToDateTime(TextBox2.Text);


        //Day Calculation

        int increment = 0;
        if (this.fromDate.Day > this.toDate.Day)
        {
            increment = this.monthDay[this.fromDate.Month - 1];
        }



        if (increment == -1)
        {
            if (DateTime.IsLeapYear(this.fromDate.Year))
            {
                increment = 29;
            }
            else
            {
                increment = 28;
            }
        }



        if (increment != 0)
        {
            day = (this.toDate.Day + increment) - this.fromDate.Day;
            increment = 1;
        }
        else
        {
            day = this.toDate.Day - this.fromDate.Day;
        }



        //Month Calculation

        if ((this.fromDate.Month + increment) > this.toDate.Month)
        {
            this.month = (this.toDate.Month + 12) - (this.fromDate.Month + increment);
            increment = 1;
        }
        else
        {
            this.month = (this.toDate.Month) - (this.fromDate.Month + increment);
            increment = 0;
        }

        //Year Calculation

        this.year = this.toDate.Year - (this.fromDate.Year + increment);



        Label1.Text = year + "Year" + month + "Month" + day + "Day";

             
    }
}







Hope this will help you.........

Happy coding.............................






Thursday, April 4, 2013

.NET Framework



MicroSoft .NET Framework

ASP.NET is part of the Microsoft .NET Framework. To build ASP.NET pages, you need to take advantage of the features of the .NET Framework. The .NET Framework consists of two parts: The Base Class Library (BCL) & The Common Language Runtime.

Understanding the Base  Class Library
The .NET Framework contains thousands of classes that you can use when building an application. The Class Library was designed to make it easier to perform the most common programming tasks.
Some Examples are

File class Enable  you to represent a file on your hard drive.

Graphics class Enable  you to work with different types of images such as GIF, PNG, BMP, and JPEG images Etc...

These are some examples of classes in the Framework. The .NET Framework contains almost 13,000 classes you can use when building applications.

You can view all the classes contained in the Framework by opening the Microsoft .NET Framework SDK documentation and expanding the Class Library node