Saturday, 26 January 2019

Jquery

Can I use multiple versions of jQuery on the same page?

<!-- load jQuery 1.1.3 -->
<script type="text/javascript" src="http://example.com/jquery-1.1.3.js"></script>
<script type="text/javascript">
var jQuery_1_1_3 = $.noConflict(true);
</script>

<!-- load jQuery 1.3.2 -->
<script type="text/javascript" src="http://example.com/jquery-1.3.2.js"></script>
<script type="text/javascript">
var jQuery_1_3_2 = $.noConflict(true);
</script>
Then, instead of $('#selector').function();, you'd do jQuery_1_3_2('#selector').function(); or  jQuery_1_1_3('#selector').function();


DOM Traversing

<html>
   <head>
      <title>The JQuery Example</title>
      <script type = "text/javascript"
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>

      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("li").eq(2).addClass("selected");
         });
      </script>

      <style>
         .selected { color:red; }
      </style>
   </head>

   <body>
      <div>
         <ul>
            <li>list item 1</li>
            <li>list item 2</li>
            <li>list item 3</li>
            <li>list item 4</li>
            <li>list item 5</li>
            <li>list item 6</li>
         </ul>
      </div>
   </body>
</html>

Filter Selectors

SelectorExampleDescription
:first$("p:first")Selects the first <p> element.
:last$("p:last")Selects the last <p> element.
:even$("tr:even")Selects all even <tr> elements, zero-indexed.
:odd$("tr:odd")Selects all odd <tr> elements, zero-indexed.
:eq()$("tr:eq(1)")Select the 2nd <tr> element within the matched set, zero-based index.
:not()$("p:not(:empty)")Select all <p> elements that are not empty.
:lt()$("ul li:lt(3)")Select all <li> elements at an index less than three within the matched set (i.e. selects 1st, 2nd, 3rd list elements), zero-based index.
:gt()$("ul li:gt(3)")Select all <li> elements at an index greater than three within the matched set (i.e. selects 4th, 5th, ... list elements), zero-based index.
:header$(":header")Selects all elements that are headers, like <h1><h2><h3>and so on.
:lang()$(":lang(en)")Selects all elements that have a language value "en" i.e. lang="en"lang="en-us" etc.
:root$(":root")Selects the document's root element which is always <html>element in a HTML document.
:animated$(":animated")Select all elements that are animating at the time the selector is run.

Child Filter Selectors

SelectorExampleDescription
:first-child$("p:first-child")Selects all <p> elements that are the first child of their parent.
:last-child$("p:last-child")Selects all <p> elements that are the last child of their parent.
:nth-child(n)$("p:nth-child(3)")Selects all <p> elements that are the 3rd child of their parent.
:only-child$("p:only-child")Selects all <p> elements that are the only child of their parent.
:first-of-type$("p:first-of-type")Selects all <p> elements that are the first <p>element of their parent.
:last-of-type$("p:last-of-type")Selects all <p> elements that are the last <p>element of their parent.
:only-of-type$("p:only-of-type")Selects all <p> elements that have no siblings with the same element name.
:nth-last-child(n)$("p:nth-last-child(3)")Selects all <p> elements that are the 3rd-child of their parent, counting from the last element to the first.
:nth-of-type(n)$("p:nth-of-type(2)")Selects all <p> elements that are the 2nd <p>element of their parent
:nth-last-of-type(n)$("p:nth-last-of-type(2)")Selects all <p> elements that are the 2nd-child of their parent, counting from the last element to the first.

Content Filter Selectors

SelectorExampleDescription
:contains()$('p:contains("Hello")')Selects all <p> elements that contains the text "Hello".
:empty$("td:empty")Selects all <td> elements that are empty i.e that have no children including text.
:has()$("p:has(img)")Selects all <p> elements which contain at least one <img> element.
:parent$(":parent")Select all elements that have at least one child node either an element or text.

AJAX Call From JQuery

 $.ajax({
                    type: "GET", //GET or POST or PUT or DELETE verb
                    url: ajaxUrl, // Location of the service
                    data: "", //Data sent to server
                    contentType: "", // content type sent to server
                    dataType: "json", //Expected data format from server
                    processdata: true, //True or False
                    success: function (json) {//On Successful service call
                        var result = json.name;
                        $("#dvAjax").html(result);
                    },
                    error: ServiceFailed // When Service call fails
                });

Friday, 25 January 2019

LINQ C#

Find the Second Max in a list of values using linq c#

List<double> ListOfNums = new List<double> {1, 5, 7, -1, 4, 8};

var secondMax = ListOfNums.OrderByDescending(r => r).Skip(1).FirstOrDefault();

OR

var secondMax = ListOfNums.OrderByDescending(r=> r).Take(2).LastOrDefault();

Left Join in linq in C#

from lst1 in TXs
       join lst2 in TYs on lst1.ID equals lst2.ID into yG
       from y1 in yG.DefaultIfEmpty()
       select new { X = lst1, Y =y1 }

Tuesday, 22 January 2019

Value and reference types in .Net

While value types are stored generally in the stack, reference types are stored in the managed heap.


Where should use abstract and interface?

Methods  which are compulsorily need to be used will be declared in abstract ,
Methods which is used depend on the scenario will be declared in interface using segregation(SOLID Principal).

Serialization in c#

In C#, serialization is the process of converting object into byte stream so that it can be saved to memory, file or database. The reverse process of serialization is called deserialization.


Example:
  1. using System;  
  2. using System.IO;  
  3. using System.Runtime.Serialization.Formatters.Binary;  
  4. [Serializable]  
  5. class Student  
  6. {  
  7.     int rollno;  
  8.     string name;  
  9.     public Student(int rollno, string name)  
  10.     {  
  11.         this.rollno = rollno;  
  12.         this.name = name;  
  13.     }  
  14. }  
  15. public class SerializeExample  
  16. {  
  17.     public static void Main(string[] args)  
  18.     {  
  19.         FileStream stream = new FileStream("e:\\sss.txt", FileMode.OpenOrCreate);  
  20.         BinaryFormatter formatter=new BinaryFormatter();  
  21.           
  22.         Student s = new Student(101, "sonoo");  
  23.         formatter.Serialize(stream, s);  
  24.   
  25.         stream.Close();  
  26.     }  
  27. }