Wednesday, 14 August 2013

JavaScript Query Fundamentals IN ASP.NET


  • 1. What's jQuery? How to use in your Application?
  • 2. What is CDN and its use?
  • 3. JavaScript Query Selectors
  • 4. JavaScript Query Events
  • 5. JavaScript Query Effects
  • 6. JavaScript Query HTML/CSS
  • 7. JavaScript Query Ajax
  • 8. JavaScript Query Methods
  • 9. JavaScript Query Examples

  • 1. What's jQuery? How to use in your Application?
  • 2. What is CDN and its use?
  • 3. JavaScript Query Selectors
  • 4. JavaScript Query Events
  • 5. JavaScript Query Effects
  • 6. JavaScript Query HTML/CSS
  • 7. JavaScript Query Ajax
  • 8. JavaScript Query Methods
  • 9. JavaScript Query Examples

Wednesday, 7 August 2013

JSON Object render from Server Side using C#

On the server you need to serialize the data as JSON so that you can pass it to the client side (Browser).
First of all we need a Data Model, So i'm creating a class for this.
 public class TestClass
    {
        public int UserID { get; set; }
        public string Fullname { get; set; }
        public string EmailAddress { get; set; }
    }

Now fill this Data Model
  private static List<testclass> FillDataModel()
        {
            List<testclass> testList = new List<testclass>();
            for (int i = 0; i < 10; i++)
            {
                TestClass test = new TestClass();
                test.UserID = i;
                test.Fullname = "Name " + (i + 1);
                test.EmailAddress = (i + 1) + "@yahoo.com";
                testList.Add(test);

            }
            return testList;
        }

Page load code will be,
  List<testclass> testList = FillDataModel();
            var jsonSerialiser = new JavaScriptSerializer();
            var json = jsonSerialiser.Serialize(testList);
            HiddenField jsonField = new HiddenField
            {
                ID = "data"
            };
            jsonField.Value = json;
            this.Form.Controls.Add(jsonField);

Now code at the client side (inside .aspx) will be


 function LetsTestJson()
        {
            var field = document.getElementById('data');
            var data = JSON.parse(field.value);
            for (var i = 0; i < data.length; i++) {
                var values = "ID : " + data[i].UserID + "\nFull Name : " + data[i].Fullname + "\nEmail : " + data[i].EmailAddress;
                alert(values);
            }
            
        }

Input Button
 

The Final Result is,


On the server you need to serialize the data as JSON so that you can pass it to the client side (Browser).
First of all we need a Data Model, So i'm creating a class for this.
 public class TestClass
    {
        public int UserID { get; set; }
        public string Fullname { get; set; }
        public string EmailAddress { get; set; }
    }

Now fill this Data Model
  private static List<testclass> FillDataModel()
        {
            List<testclass> testList = new List<testclass>();
            for (int i = 0; i < 10; i++)
            {
                TestClass test = new TestClass();
                test.UserID = i;
                test.Fullname = "Name " + (i + 1);
                test.EmailAddress = (i + 1) + "@yahoo.com";
                testList.Add(test);

            }
            return testList;
        }

Page load code will be,
  List<testclass> testList = FillDataModel();
            var jsonSerialiser = new JavaScriptSerializer();
            var json = jsonSerialiser.Serialize(testList);
            HiddenField jsonField = new HiddenField
            {
                ID = "data"
            };
            jsonField.Value = json;
            this.Form.Controls.Add(jsonField);

Now code at the client side (inside .aspx) will be


 function LetsTestJson()
        {
            var field = document.getElementById('data');
            var data = JSON.parse(field.value);
            for (var i = 0; i < data.length; i++) {
                var values = "ID : " + data[i].UserID + "\nFull Name : " + data[i].Fullname + "\nEmail : " + data[i].EmailAddress;
                alert(values);
            }
            
        }

Input Button
 

The Final Result is,


Tuesday, 30 July 2013

Connect Entity Framework with Sqllite database


This walkthrough will get you started with an application that uses the Entity Framework (EF) to read and write data from a SQLite database. It is intended to be similar to the Code First to a New Database walkthrough.

There are currently two SQLite providers for EF that I know of: System.Data.SQLite andDevart's dotConnect for SQLite. Devart's provider has a much richer set of features, but it is also a commercial product. In the spirit of FOSS, we will be using the System.Data.SQLite provider for this walkthrough. I encourage you to keep the Devart provider in mind, however, if your project requires that extra level of support.

Create the Application

For simplicity, we will be using a Console Application, but the basic steps are the same regardless of project type.
  1. Open Visual Studio
  2. Select File -> New -> Project...
  3. Select Console Application
  4. Name the project
  5. Click OK

Create the Model

For our model, we'll be borrowing pieces from the Chinook Database (a cross-platform, sample database). Specifically, we will be using Artists and Albums.

Add the following two classes to your project.


public class Artist
{
    public Artist()
    {
        Albums = new List();
    }
 
    public long ArtistId { get; set; }
    public string Name { get; set; }
 
    public virtual ICollection Albums { get; set; }
}
 
public class Album
{
    public long AlbumId { get; set; }
    public string Title { get; set; }
 
    public long ArtistId { get; set; }
    public virtual Artist Artist { get; set; }
}

Create a Context

In EF, the context becomes your main entry point into the database. Before we define our context though, we will need to install Entity Framework.
  1. Select Tools -> Library Package Manager -> Package Manager Console
  2. Inside the Package Manager Console (PMC) run Install-Package EntityFramework
Now, add the context class to your project.


class ChinookContext : DbContext
{
    public DbSet Artists { get; set; }
    public DbSet Albums { get; set; }
 
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Chinook Database does not pluralize table names
        modelBuilder.Conventions
            .Remove();
    }
}

Install the Provider

In order to connect to SQLite databases, we will need to install an appropriate ADO.NET and Entity Framework provider. Luckily, the provider we're using is available via NuGet.
  1. Inside PMC, run Install-Package System.Data.SQLite.x86
We also need to register the provider. Open App.config, and anywhere inside the configuration element, add the following fragment.





  
    
  



Add the Database

Unfortunately, System.Data.SQLite does not support creating databases. So instead of letting Code First create our database, we will need to manually add a database to our project. It's a good thing we're using a sample database that's available for SQLite!
  1. Download and extract the SQLite version of the Chinook Database
  2. Select Project -> Add Existing Item...
  3. Browse to the folder where you extracted the database
  4. Select All Files from the dropdown next to File name
  5. Select the Chinook_Sqlite_AutoIncrementPKs.sqlite file
  6. Click Add
  7. Select the file in Solution Explorer
  8. In Properties, set Copy to Output Directory to Copy if newer
Also, add a connection string to the App.Config that points to the database file. Anywhere inside the configuration element, add the following fragment.




  

Start Coding

Ok, we should be ready to start coding our application. Let's see what artists exist in the database. Inside Program.cs, add the following to Main.



using (var context = new ChinookContext())
{
    var artists = from a in context.Artists
                  where a.Name.StartsWith("A")
                  orderby a.Name
                  select a;
 
    foreach (var artist in artists)
    {
        Console.WriteLine(artist.Name);
    }
}
Hmm, it looks like one of my favorite bands is missing. Let's add it.


        using (var context = new ChinookContext())
{
    context.Artists.Add(
        new Artist
        {
            Name = "Anberlin",
            Albums =
            {
                new Album { Title = "Cities" },
                new Album { Title = "New Surrender" }
            }
        });
    context.SaveChanges();
}

Conclusion

Hopefully by now, you have enough information to get started using the Entity Framework with a SQLite database. For many, many more articles on how to use EF, check out our team's official Getting Started page on MSDN.

This walkthrough will get you started with an application that uses the Entity Framework (EF) to read and write data from a SQLite database. It is intended to be similar to the Code First to a New Database walkthrough.

There are currently two SQLite providers for EF that I know of: System.Data.SQLite andDevart's dotConnect for SQLite. Devart's provider has a much richer set of features, but it is also a commercial product. In the spirit of FOSS, we will be using the System.Data.SQLite provider for this walkthrough. I encourage you to keep the Devart provider in mind, however, if your project requires that extra level of support.

Create the Application

For simplicity, we will be using a Console Application, but the basic steps are the same regardless of project type.
  1. Open Visual Studio
  2. Select File -> New -> Project...
  3. Select Console Application
  4. Name the project
  5. Click OK

Create the Model

For our model, we'll be borrowing pieces from the Chinook Database (a cross-platform, sample database). Specifically, we will be using Artists and Albums.

Add the following two classes to your project.


public class Artist
{
    public Artist()
    {
        Albums = new List();
    }
 
    public long ArtistId { get; set; }
    public string Name { get; set; }
 
    public virtual ICollection Albums { get; set; }
}
 
public class Album
{
    public long AlbumId { get; set; }
    public string Title { get; set; }
 
    public long ArtistId { get; set; }
    public virtual Artist Artist { get; set; }
}

Create a Context

In EF, the context becomes your main entry point into the database. Before we define our context though, we will need to install Entity Framework.
  1. Select Tools -> Library Package Manager -> Package Manager Console
  2. Inside the Package Manager Console (PMC) run Install-Package EntityFramework
Now, add the context class to your project.


class ChinookContext : DbContext
{
    public DbSet Artists { get; set; }
    public DbSet Albums { get; set; }
 
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Chinook Database does not pluralize table names
        modelBuilder.Conventions
            .Remove();
    }
}

Install the Provider

In order to connect to SQLite databases, we will need to install an appropriate ADO.NET and Entity Framework provider. Luckily, the provider we're using is available via NuGet.
  1. Inside PMC, run Install-Package System.Data.SQLite.x86
We also need to register the provider. Open App.config, and anywhere inside the configuration element, add the following fragment.





  
    
  



Add the Database

Unfortunately, System.Data.SQLite does not support creating databases. So instead of letting Code First create our database, we will need to manually add a database to our project. It's a good thing we're using a sample database that's available for SQLite!
  1. Download and extract the SQLite version of the Chinook Database
  2. Select Project -> Add Existing Item...
  3. Browse to the folder where you extracted the database
  4. Select All Files from the dropdown next to File name
  5. Select the Chinook_Sqlite_AutoIncrementPKs.sqlite file
  6. Click Add
  7. Select the file in Solution Explorer
  8. In Properties, set Copy to Output Directory to Copy if newer
Also, add a connection string to the App.Config that points to the database file. Anywhere inside the configuration element, add the following fragment.




  

Start Coding

Ok, we should be ready to start coding our application. Let's see what artists exist in the database. Inside Program.cs, add the following to Main.



using (var context = new ChinookContext())
{
    var artists = from a in context.Artists
                  where a.Name.StartsWith("A")
                  orderby a.Name
                  select a;
 
    foreach (var artist in artists)
    {
        Console.WriteLine(artist.Name);
    }
}
Hmm, it looks like one of my favorite bands is missing. Let's add it.


        using (var context = new ChinookContext())
{
    context.Artists.Add(
        new Artist
        {
            Name = "Anberlin",
            Albums =
            {
                new Album { Title = "Cities" },
                new Album { Title = "New Surrender" }
            }
        });
    context.SaveChanges();
}

Conclusion

Hopefully by now, you have enough information to get started using the Entity Framework with a SQLite database. For many, many more articles on how to use EF, check out our team's official Getting Started page on MSDN.

Thursday, 25 July 2013

Text Fit Plugin

Plugin will auto resize the text if text is overflowed to the applied element.

Download Link
Plugin will auto resize the text if text is overflowed to the applied element.

Download Link

Wednesday, 24 July 2013

Jquery Selectors

The jQuery library harnesses the power of Cascading Style Sheets (CSS) selectors to let us quickly and easily access elements or groups of elements in the Document Object Model (DOM).
A jQuery Selector is a function which makes use of expressions to find out matching elements from a DOM based on the given criteria.

The $() factory function:

All type of selectors available in jQuery, always start with the dollar sign and parentheses: $().
The factory function $() makes use of following three building blocks while selecting elements in a given document:
jQueryDescription
Tag Name:Represents a tag name available in the DOM. For example $('p') selects all paragraphs in the document.
Tag ID:Represents a tag available with the given ID in the DOM. For example $('#some-id') selects the single element in the document that has an ID of some-id.
Tag Class:Represents a tag available with the given class in the DOM. For example $('.some-class') selects all elements in the document that have a class of some-class.
All the above items can be used either on their own or in combination with other selectors. All the jQuery selectors are based on the same principle except some tweaking.
NOTE: The factory function $() is a synonym of jQuery() function. So in case you are using any other JavaScript library where $ sign is conflicting with some thing else then you can replace $ sign by jQuery name and you can use function jQuery() instead of $().

Example:

Following is a simple example which makes use of Tag Selector. This would select all the elements with a tag name p.
    

the title
   
   
   


   
This is a paragraph.
This is second paragraph.
This is third paragraph.
To understand it in better way you can Try it yourself.

How to use Selectors?

The selectors are very useful and would be required at every step while using jQuery. They get the exact element that you want from your HTML document.
Following table lists down few basic selectors and explains them with examples.
SelectorDescription
NameSelects all elements which match with the given element Name.
#IDSelects a single element which matches with the given ID
.ClassSelects all elements which match with the given Class.
Universal (*)Selects all elements available in a DOM.
Multiple Elements E, F, GSelects the combined results of all the specified selectors E, F or G.
Similar to above syntax and examples, following examples would give you understanding on using different type of other useful selectors:
  • $('*'): This selector selects all elements in the document.
  • $("p > *"): This selector selects all elements that are children of a paragraph element.
  • $("#specialID"): This selector function gets the element with id="specialID".
  • $(".specialClass"): This selector gets all the elements that have the class of specialClass.
  • $("li:not(.myclass)"): Selects all elements matched by <li> that do not have class="myclass".
  • $("a#specialID.specialClass"): This selector matches links with an id of specialID and a class of specialClass.
  • $("p a.specialClass"): This selector matches links with a class of specialClass declared within <p> elements.
  • $("ul li:first"): This selector gets only the first <li> element of the <ul>.
  • $("#container p"): Selects all elements matched by <p> that are descendants of an element that has an id of container.
  • $("li > ul"): Selects all elements matched by <ul> that are children of an element matched by <li>
  • $("strong + em"): Selects all elements matched by <em> that immediately follow a sibling element matched by <strong>.
  • $("p ~ ul"): Selects all elements matched by <ul> that follow a sibling element matched by <p>.
  • $("code, em, strong"): Selects all elements matched by <code> or <em> or <strong>.
  • $("p strong, .myclass"): Selects all elements matched by <strong> that are descendants of an element matched by <p> as well as all elements that have a class of myclass.
  • $(":empty"): Selects all elements that have no children.
  • $("p:empty"): Selects all elements matched by <p> that have no children.
  • $("div[p]"): Selects all elements matched by <div> that contain an element matched by <p>.
  • $("p[.myclass]"): Selects all elements matched by <p> that contain an element with a class of myclass.
  • $("a[@rel]"): Selects all elements matched by <a> that have a rel attribute.
  • $("input[@name=myname]"): Selects all elements matched by <input> that have a name value exactly equal to myname.

  • $("input[@name^=myname]"): Selects all elements matched by <input> that have a name value beginning with myname.
  • $("a[@rel$=self]"): Selects all elements matched by <p> that have a class value ending with bar

  • $("a[@href*=domain.com]"): Selects all elements matched by <a> that have an href value containing domain.com.
  • $("li:even"): Selects all elements matched by <li> that have an even index value.
  • $("tr:odd"): Selects all elements matched by <tr> that have an odd index value.
  • $("li:first"): Selects the first <li> element.
  • $("li:last"): Selects the last <li> element.
  • $("li:visible"): Selects all elements matched by <li> that are visible.
  • $("li:hidden"): Selects all elements matched by <li> that are hidden.
  • $(":radio"): Selects all radio buttons in the form.
  • $(":checked"): Selects all checked boxex in the form.
  • $(":input"): Selects only form elements (input, select, textarea, button).
  • $(":text"): Selects only text elements (input[type=text]).
  • $("li:eq(2)"): Selects the third <li> element
  • $("li:eq(4)"): Selects the fifth <li> element
  • $("li:lt(2)"): Selects all elements matched by <li> element before the third one; in other words, the first two <li> elements.
  • $("p:lt(3)"): selects all elements matched by <p> elements before the fourth one; in other words the first three <p> elements.
  • $("li:gt(1)"): Selects all elements matched by <li> after the second one.
  • $("p:gt(2)"): Selects all elements matched by <p> after the third one.
  • $("div/p"): Selects all elements matched by <p> that are children of an element matched by <div>.
  • $("div//code"): Selects all elements matched by <code>that are descendants of an element matched by <div>.
  • $("//p//a"): Selects all elements matched by <a> that are descendants of an element matched by <p>
  • $("li:first-child"): Selects all elements matched by <li> that are the first child of their parent.
  • $("li:last-child"): Selects all elements matched by <li> that are the last child of their parent.
  • $(":parent"): Selects all elements that are the parent of another element, including text.
  • $("li:contains(second)"): Selects all elements matched by <li> that contain the text second.
You can use all the above selectors with any HTML/XML element in generic way. For example if selector $("li:first") works for <li> element then $("p:first") would also work for <p> element.

Happy Coding :)
The jQuery library harnesses the power of Cascading Style Sheets (CSS) selectors to let us quickly and easily access elements or groups of elements in the Document Object Model (DOM).
A jQuery Selector is a function which makes use of expressions to find out matching elements from a DOM based on the given criteria.

The $() factory function:

All type of selectors available in jQuery, always start with the dollar sign and parentheses: $().
The factory function $() makes use of following three building blocks while selecting elements in a given document:
jQueryDescription
Tag Name:Represents a tag name available in the DOM. For example $('p') selects all paragraphs in the document.
Tag ID:Represents a tag available with the given ID in the DOM. For example $('#some-id') selects the single element in the document that has an ID of some-id.
Tag Class:Represents a tag available with the given class in the DOM. For example $('.some-class') selects all elements in the document that have a class of some-class.
All the above items can be used either on their own or in combination with other selectors. All the jQuery selectors are based on the same principle except some tweaking.
NOTE: The factory function $() is a synonym of jQuery() function. So in case you are using any other JavaScript library where $ sign is conflicting with some thing else then you can replace $ sign by jQuery name and you can use function jQuery() instead of $().

Example:

Following is a simple example which makes use of Tag Selector. This would select all the elements with a tag name p.
    

the title
   
   
   


   
This is a paragraph.
This is second paragraph.
This is third paragraph.
To understand it in better way you can Try it yourself.

How to use Selectors?

The selectors are very useful and would be required at every step while using jQuery. They get the exact element that you want from your HTML document.
Following table lists down few basic selectors and explains them with examples.
SelectorDescription
NameSelects all elements which match with the given element Name.
#IDSelects a single element which matches with the given ID
.ClassSelects all elements which match with the given Class.
Universal (*)Selects all elements available in a DOM.
Multiple Elements E, F, GSelects the combined results of all the specified selectors E, F or G.
Similar to above syntax and examples, following examples would give you understanding on using different type of other useful selectors:
  • $('*'): This selector selects all elements in the document.
  • $("p > *"): This selector selects all elements that are children of a paragraph element.
  • $("#specialID"): This selector function gets the element with id="specialID".
  • $(".specialClass"): This selector gets all the elements that have the class of specialClass.
  • $("li:not(.myclass)"): Selects all elements matched by <li> that do not have class="myclass".
  • $("a#specialID.specialClass"): This selector matches links with an id of specialID and a class of specialClass.
  • $("p a.specialClass"): This selector matches links with a class of specialClass declared within <p> elements.
  • $("ul li:first"): This selector gets only the first <li> element of the <ul>.
  • $("#container p"): Selects all elements matched by <p> that are descendants of an element that has an id of container.
  • $("li > ul"): Selects all elements matched by <ul> that are children of an element matched by <li>
  • $("strong + em"): Selects all elements matched by <em> that immediately follow a sibling element matched by <strong>.
  • $("p ~ ul"): Selects all elements matched by <ul> that follow a sibling element matched by <p>.
  • $("code, em, strong"): Selects all elements matched by <code> or <em> or <strong>.
  • $("p strong, .myclass"): Selects all elements matched by <strong> that are descendants of an element matched by <p> as well as all elements that have a class of myclass.
  • $(":empty"): Selects all elements that have no children.
  • $("p:empty"): Selects all elements matched by <p> that have no children.
  • $("div[p]"): Selects all elements matched by <div> that contain an element matched by <p>.
  • $("p[.myclass]"): Selects all elements matched by <p> that contain an element with a class of myclass.
  • $("a[@rel]"): Selects all elements matched by <a> that have a rel attribute.
  • $("input[@name=myname]"): Selects all elements matched by <input> that have a name value exactly equal to myname.

  • $("input[@name^=myname]"): Selects all elements matched by <input> that have a name value beginning with myname.
  • $("a[@rel$=self]"): Selects all elements matched by <p> that have a class value ending with bar

  • $("a[@href*=domain.com]"): Selects all elements matched by <a> that have an href value containing domain.com.
  • $("li:even"): Selects all elements matched by <li> that have an even index value.
  • $("tr:odd"): Selects all elements matched by <tr> that have an odd index value.
  • $("li:first"): Selects the first <li> element.
  • $("li:last"): Selects the last <li> element.
  • $("li:visible"): Selects all elements matched by <li> that are visible.
  • $("li:hidden"): Selects all elements matched by <li> that are hidden.
  • $(":radio"): Selects all radio buttons in the form.
  • $(":checked"): Selects all checked boxex in the form.
  • $(":input"): Selects only form elements (input, select, textarea, button).
  • $(":text"): Selects only text elements (input[type=text]).
  • $("li:eq(2)"): Selects the third <li> element
  • $("li:eq(4)"): Selects the fifth <li> element
  • $("li:lt(2)"): Selects all elements matched by <li> element before the third one; in other words, the first two <li> elements.
  • $("p:lt(3)"): selects all elements matched by <p> elements before the fourth one; in other words the first three <p> elements.
  • $("li:gt(1)"): Selects all elements matched by <li> after the second one.
  • $("p:gt(2)"): Selects all elements matched by <p> after the third one.
  • $("div/p"): Selects all elements matched by <p> that are children of an element matched by <div>.
  • $("div//code"): Selects all elements matched by <code>that are descendants of an element matched by <div>.
  • $("//p//a"): Selects all elements matched by <a> that are descendants of an element matched by <p>
  • $("li:first-child"): Selects all elements matched by <li> that are the first child of their parent.
  • $("li:last-child"): Selects all elements matched by <li> that are the last child of their parent.
  • $(":parent"): Selects all elements that are the parent of another element, including text.
  • $("li:contains(second)"): Selects all elements matched by <li> that contain the text second.
You can use all the above selectors with any HTML/XML element in generic way. For example if selector $("li:first") works for <li> element then $("p:first") would also work for <p> element.

Happy Coding :)