JQuery to locate items by value or by text in combobox

Here is code snippet to locate items by value or by text in  combobox.

<html>
<head id=”Head1″ runat=”server”>
<script src=”Scripts/jquery-2.0.3.js”></script> <title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:DropDownList ID=”ListBox1″ runat=”server”>
<asp:ListItem Value=”1″>One</asp:ListItem>
<asp:ListItem Value=”2″>Two</asp:ListItem>
<asp:ListItem Value=”3″>Third</asp:ListItem>
<asp:ListItem Value=”4″>Four</asp:ListItem>
<asp:ListItem Value=”5″>Five</asp:ListItem>
</asp:DropDownList>
<br />
<asp:TextBox ID=”text1″ runat=”server”></asp:TextBox><input id=”Button1″ type=”button” value=”By Value” /><input id=”Button2″ type=”button” value=”By Text” /><input id=”Button3″ type=”button” value=”By Text 1″ />
</div>
</form>
</body>
</html>

<script type=”text/javascript”>
//$(‘#ListBox1’).click(function () {
// //$(“input#text1”).val($(“#ListBox1 option:selected”).val());
// alert($(“input#text1”).val == $(“#ListBox1 option:selected”).val());
//});
$(‘#Button1’).click(function () {
var valToSelect=$(“input#text1”).val();
$(“#ListBox1 option[value='” + valToSelect + “‘]”).attr(“selected”, “true”);
//alert($(“input#text1”).val == $(“#ListBox1 option:selected”).val());
alert(valToSelect);
});
$(‘#Button2’).click(function () {
var valToSelect = $(“input#text1”).val();
$(“#ListBox1 option:selected”).text(valToSelect);
alert(valToSelect);
});

$(‘#Button3’).click(function () {
var valToSelect = $(“input#text1”).val();
$(“#ListBox1 option”).each(function () {
if ($(this).text() == valToSelect) {
$(this).attr(‘selected’, ‘selected’);
}
});
});
</script>

Observable Collection

WPF ObservableCollection

An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated. This happens because, when binding to an observable collection, WPF automatically adds a CollectionChanged event handler to the ObservableCollecion’s events.

The ObservableCollection class exists in the System.Collections.ObjectModel namespace.

I have a window with a Button, two TextBoxes and a ListView and each time you click the Button the text of the TextBox is added to the collection and the ListView is updated automatically.

ObservableEx

Drag Drop GridView Rows With JQuery

This Example Implement Drag And Drop GridView Rows Functionality Using JQuery to Rearrange Row On Client Side.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Sortable - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
#sortable li span { position: absolute; margin-left: -1.3em; }
</style>
<script>
$(function() {
$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
});
</script>
</head>
<body>
<ul id="sortable">
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 4</li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 5</li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 6</li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 7</li>
</ul>
</body>
</html>

Difference between IEnumerable, IQueryable and IList

In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, hence it has all the features of it and except this, it has its own features. Both have their own importance to query data and data manipulation. Let’s see both the features and take the advantage of both the features to boost your LINQ Query performance.

IEnumerable

  1. IEnumerable exists in System.Collections Namespace.
  2. IEnumerable can move forward only over a collection, it can’t move backward and between the items.
  3. IEnumerable is best to query data from in-memory collections like List, Array etc.
  4. While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.
  5. IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
  6. IEnumerable supports deferred execution.
  7. IEnumerable doesn’t supports custom query.
  8. IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.
  9. Extension methods supports by IEnumerable takes functional objects.

IEnumerable Example

MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10);

Generated SQL statemLinksents of above query will be :

SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
WHERE [t0].[EmpName] LIKE @p0

*Notice that in this query “top 10” is missing since IEnumerable filters records on client side.

IQueryable

  1. IQueryable exists in System.Linq Namespace.
  2. IQueryable can move forward only over a collection, it can’t move backward and between the items.
  3. IQueryable is best to query data from out-memory (like remote database, service) collections.
  4. While query data from database, IQueryable execute select query on server side with all filters.
  5. IQueryable is suitable for LINQ to SQL queries.
  6. IQueryable supports deferred execution.
  7. IQueryable supports custom query using CreateQuery and Execute methods.
  8. IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
  9. Extension methods supports by IEnumerable takes expression objects means expression tree.

IQueryable Example

MyDataContext dc = new MyDataContext ();
IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10);

*Notice that in this query “top 10” is existing since IQueryable executes query in SQL server with all filters.

Generated SQL statements of above query will be :

SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
WHERE [t0].[EmpName] LIKE @p0

IList

  1. IList exists in System.Collections Namespace.
  2. IList is used to access an element in a specific position/index in a list.
  3. Like IEnumerable, IList is also best to query data from in-memory collections like List, Array etc.
  4. IList is useful when you want to Add or remove items from the list.
  5. IList can find out the no of elements in the collection without iterating the collection.
  6. IList supports deferred execution.
  7. IList doesn’t support further filtering.

Points of Interest

  1. IEnumerable<T> represents a forward-only cursor of T, .NET 3.5 added extension methods that included the LINQ standard query operators like Where and First, with any operators that require predicates or anonymous functions taking Func<T>.
  2. IQueryable<T> implements the same LINQ standard query operators, but acceptsExpression<Func<T>> for predicates and anonymous functions. Expression<T> is a compiled expression tree, a broken-up version of the method (“half-compiled” if you will) that can be parsed by the queryable’s provider and used accordingly.