Featured

Sticky Any Part of Webpage

Hi Friends,

After log time today i published new article from my experience which recently i have faced. In my recent project i need to sticky left portion of the website. Using Hc-Sticky.js i achieve the task.

Step-01 : First download the Code from Git : Download

Step-02 : After download add “jquery.hc-sticky.js” in your page or in your master page.

Step-03 : In Page Ready paste the below code.

$(document).ready(function () {

// Id of Element want to stick.

$(‘#divsticky’).hcSticky({
bottom: 100
});
});

 

Thank you.

If you have any query fill free to contact me or send email

 

Advertisements

Joins in LINQ to SQL

The following post shows how to write different types of joins in LINQ to SQL. I am using the Northwind database and LINQ to SQL for these examples.

   1: NorthwindDataContext dataContext = new NorthwindDataContext();

INNER JOIN :

   1: var q1 = from c in dataContext.Customers

   2:          join o in dataContext.Orders on c.CustomerID equals o.CustomerID

   3:          select new

   4:          {

   5:              c.CustomerID,

   6:              c.ContactName,

   7:              o.OrderID,

   8:              o.OrderDate

   9:          };

LEFT JOIN :

   1: var q2 = from c in dataContext.Customers

   2:          join o in dataContext.Orders on c.CustomerID equals o.CustomerID into g

   3:          from a in g.DefaultIfEmpty()

   4:          select new

   5:          {

   6:              c.CustomerID,

   7:              c.ContactName,

   8:              a.OrderID,

   9:              a.OrderDate

  10:          };

INNER JOIN ON MULTIPLE :

   1: var q3 = from c in dataContext.Customers

   2:          join o in dataContext.Orders on new { a = c.CustomerID, b = c.Country } equals new { a = o.CustomerID, b = "USA" }

   3:          select new

   4:          {

   5:              c.CustomerID,

   6:              c.ContactName,

   7:              o.OrderID,

   8:              o.OrderDate

   9:          };

INNER JOIN ON MULTIPLE WITH “OR” :

   1: var q4 = from c in dataContext.Customers

   2:          from o in dataContext.Orders.Where(a => a.CustomerID == c.CustomerID || c.Country == "INDIA")

   3:          select new

   4:          {

   5:              c.CustomerID,

   6:              c.ContactName,

   7:              o.OrderID,

   8:              o.OrderDate

   9:          };

LEFT JOIN ON MULTIPLE WITH “OR” :

   1: var q5 = from c in dataContext.Customers

   2:          from o in dataContext.Orders.Where(a => a.CustomerID == c.CustomerID || c.Country == "USA").DefaultIfEmpty()

   3:          select new

   4:          {

   5:              c.CustomerID,

   6:              c.ContactName,

   7:              o.OrderID,

   8:              o.OrderDate

   9:          };

More Example base on this article will post shortly 🙂

What’s New in ASP.NET 4.0 – Better ViewState Control

  • This article is Part II of the multi-part series covering new features on ASP.NET 4.0.
  • In ASP.NET 2.0/3.5, you could disable ViewState for individual controls. However what you could not do is disable ViewState at a Page level and then enable it individually for controls on that page that require it. ASP.NET 4.0 changes that and gives more control to developers.
  • Let’s see this with an example. We will first create a page in ASP.NET 3.5 and disable ViewState on the Parent level and then observe the behavior of ViewState in child controls. We will then use the same example in ASP.NET 4.0 using the ViewStateMode property and observe the changes.

ViewState in ASP.NET 2.0/3.5

  • Create an ASP.NET application. Place two Label controls and one Button control on the page. Now disable view state on the Page by setting the ‘EnableViewState’ property to false. On the first Label control, set ‘EnableViewState’ property to true as shown below:
   1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" EnableViewState="false" Inherits="_Default" %>

   2:  

   3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   4:  

   5: <html xmlns="http://www.w3.org/1999/xhtml">

   6: <head runat="server">

   7:     <title>ViewState Demo in ASP.NET 3.5</title>

   8: </head>

   9: <body>

  10:     <form id="form1" runat="server">

  11:     <div>

  12:         <asp:Label ID="Label1" runat="server" Text="First" EnableViewState="true"></asp:Label><br />

  13:         <asp:Label ID="Label2" runat="server" Text="Second"></asp:Label><br /><br />

  14:         <asp:Button ID="Button1" runat="server" Text="PostBack" />

  15:     </div>

  16:     </form>

  17: </body>

  18: </html>

In the code behind file, write the following code which set’s the text of the Label controls:

   1: if(!IsPostBack)

   2: {

   3:     Label1.Text = "My First Example";

   4:     Label2.Text = "My Second Example";

   5: }

Now what is ‘ideally’ expected out of this setup is that although ViewState is disabled for the entire page, yet Label1 should save ViewState and retain its new value ‘Label1 Changed’ after postback; since ViewState is explicitly enabled on it.

You will observe that on hitting the button (causing a postback), Label1 does not retain the value (Label1 Changed) explicitly set on it. Not as we expected!

ViewState in ASP.NET 4.0

We will now create a similar web application in ASP.NET 4.0. The only difference is that here we will use the new ‘ViewStateMode’ property.

The ViewStateMode property accepts three values: Enabled, Disabled, andInherit.

Enabled – enables view state for that control and any child controls that are set to ‘Inherit’ or that have nothing set.

Disabled – disables view state

Inherit – specifies that the control uses the ViewStateMode setting from the parent control.

The markup looks similar to the following:

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

   2:  

   3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   4:  

   5: <html xmlns="http://www.w3.org/1999/xhtml">

   6: <head id="Head1" runat="server">

   7:     <title>View State Demo in ASP.NET 4.0</title>

   8: </head>

   9: <body>

  10:     <form id="form1" runat="server">

  11:     <div>

  12:         <asp:Label ID="Label1" runat="server" Text="First" ViewStateMode="Enabled"></asp:Label><br />

  13:         <asp:Label ID="Label2" runat="server" Text="Second"></asp:Label> <br /><br />      

  14:         <asp:Button ID="Button1" runat="server" Text="PostBack" />

  15:     </div>

  16:     </form>

  17: </body>

  18: </html>

Observe that ViewStateMode on the Page is set to Disabled. The child Label1 control has ViewStateMode set to Enabled, so Label1 ‘ideally’ should save view state. Since the ViewStateMode property is not set on Label2 , so the Label2 control inherits this property value from its parent (Page) and therefore saves no view state.

Note: The default value of ViewStateMode is Enabled for Pageobject. The default value of ViewStateMode is Inherit for controls.

Run the application and hit the button. What do you expect now?

When the page first loads, both the Label controls display the text as set in the codebehind file.

However, after hitting the button and causing a postback, Label1 does retain its value as we had expected!

 

What’s New in ASP.NET 4.0

With the ASP.NET 4.0 release round the corner, I thought of starting an article series that covers the new features introduced in ASP.NET 4.0, one at a time. This article is Part I of this multi-part series.

In this article we will learn 2 new properties added to Pages Class.

  1. Metkeywords.
  2. MetaDescription.

The MetaDescription is important from an SEO perspective. The META Description Tag is an integral part which identifies a page and irrespective of the contrary belief, I think search engines take this meta tag seriously.

Now in ASP.NET 2.0/3.5, you could use the HTMLMeta class to define HTML <meta> elements for your page as shown below:

   1: HtmlMeta meta1 = new HtmlMeta();

   2: meta1.Name = "keywords name here";

   3: meta1.Content = "some keywords desctiption here";

   4:  

   5: HtmlMeta meta2 = new HtmlMeta();

   6: meta2.Name = "keywords name here";

   7: meta2.Content = "some keywords desctiption here";

   8:  

   9: Page.Header.Controls.Add(meta1);

  10: Page.Header.Controls.Add(meta2);

Too much code!

However in ASP.NET 4.0, you now have two new properties added to the Page class – MetaKeywords and MetaDescription.

   1: <%@ Page Title="New Features in ASP.NET 4" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"

   2: MetaKeywords="ASP.NET 4, SEO" MetaDescription="This page contains information about ASP.NET 4 and SEO enhancements" %>

In order to retain the older meta tag contents and also use the new ones, just concatenate the two programmatically as shown below:

   1: protected void Page_Load(object sender, EventArgs e)

   2: {

   3:         Page.MetaDescription = Page.MetaDescription + " This page contains information about ASP.NET 4 and SEO enhancements";

   4:         Page.MetaKeywords = Page.MetaKeywords + " ASP.NET 4, SEO";

   5: }

Cookies and jQuery

To use cookies with jQuery, you will need the special Cookie plugin.

Setting cookies :

Setting cookies with the Cookie plug-in is quite intuitive:

   1: $(document).ready(function(){

   2:  

   3:     // Setting a kittens cookie, it will be lost on browser restart:

   4:     $.cookie("kittens","Seven Kittens");

   5:  

   6:     // Setting demoCookie (as seen in the demonstration):

   7:     $.cookie("demoCookie",text,{expires: 7, path: '/', domain: 'demo.tutorialzine.com'});

   8:  

   9:     // "text" is a variable holding the string to be saved

  10: });

Reading cookies :

Reading a cookie is even simpler. Just call the $.cookie() function with a single cookie-name parameter, and the value of the cookie will be returned:

   1: $(document).ready(function(){

   2:  

   3:     // Getting the kittens cookie:

   4:     var str = $.cookie("kittens");

   5:  

   6:     // str now contains "Seven Kittens"

   7: });

Deleting cookies :

To delete a cookie, again use the $.cookie() function, but pass null as its second parameter.

   1: $(document).ready(function(){

   2:  

   3:     // Deleting the kittens cookie:

   4:     var str = $.cookie("kittens",null);

   5:  

   6:     // No more kittens

   7: });

PageMethods In ASP.NET AJAX

Page Methods is a new mechanism in ASP.Net application where the server code cab be bound to Asp.Net pages.  It is an easy way to communicate asynchronously with the server using Asp.Net Ajax technologies. It’s an alternate way to call the page which is reliable and low risk.  In a nutshell, it is used to expose the web methods to the client script.

 

Sample Application Demonstrating Page Methods

  • To enable Page methods we need to drag a ScriptManager control to the page and mark the EnablePageMethods to True.

   1: <asp:ScriptManager ID="ScriptManager1" runat="server"   

   2:       EnablePageMethods="True">

   3: </asp:ScriptManager>

  • Go the Code behind file of the page and add a static method and mark them as WebMethod. Lets Say the method name is GetPageMethod which returns a status value.
   1: [System.Web.Services.WebMethod]        

   2: public static string GetPageMethod()

   3: {

   4:       return "Welcome PageMethods";

   5: }

  • Now that the method is exposed add a button control and call a client script which accesses the Webmethods of the page.
   1: <asp:Button ID="Button1" runat="server" Text="Call Page Methods"  

   2:       OnClientClick="return fun()"/>

  • In javascript function we can use PageMethods object to call the WebMethod of the page.
       1: function fun()

       2: {    

       3:      PageMethods.updateProduct(GetPageMethod,OnSucceeded,OnError);    

       4: }

       5:  

       6: function OnSucceeded(result, userContext, methodName) 

       7: {

       8:     if (methodName == "GetPageMethod")

       9:     {

      10:   alert(result);

      11:     }

      12: }

      13:  

      14: function OnError(result,userContext,methodName)

      15: {

      16:     if(methodName == "GetPageMethod")

      17:     {

      18:         IsError = 1;

      19:     }

      20: }

 

  • Now run the application and check the result which shows the alert box with value “Welcome PageMethods”. The sample application calls the webmethods of the page using client script without full page cycle.

Value Type Vs Reference Type

Value Type :

  1. When a value-type instance is created, a single space in memory is allocated to store the value.
  2. Primitive types such as integer, float, Boolean and char are also value types.
  3. Structure are value types.
  4. Memory Allocation : Value type local variable stored in Stack.
  5. C# parameters are (by default) passed by value.

Reference Type :

  1. With reference types, however, an object is created in memory, and then handled through a separate reference – rather like a pointer.
  2. Class is Reference types.
  3. Memory Allocation : The reference portion of reference type local variable store in Stack and content of reference type object store in Heap.

Examples :

   1: // Suppose Point is a struct, and Form is a class

   2: Point p1 = new Point();         // Point is a *struct*

   3: Form f1 = new Form();           // Form is a *class*

 

  • In the first case , one space of memory is allocated of p1 whereas in the second case, two spaces are allocated: one for a Form object and another for its reference (f1).
   1: Size s = new Size (100, 100);          // struct = value type

   2: Font f = new Font (“Arial”,10);        // class = reference type

   3:  

   4: // Form is Reference Type Variable

   5: Form myForm = new Form();

   6:  

   7: // To set the form's size and font, we can assign the objects s 

   8: //and f to the form via its properties:

   9: myForm.Size = s;

  10: myForm.Font = f;

               Here’s what it now looks like in memory:

image006