JQuery in Visual Studio

A big part of the appeal of jQuery is that it allows you to elegantly (and efficiently) find and manipulate HTML elements with minimum lines of code. jQuery supports this via a nice “selector” API that allows developers to query for HTML elements, and then apply “commands” to them. One of the characteristics of jQuery commands is that they can be “chained” together – so that the result of one command can feed into another. jQuery also includes a built-in set of animation APIs that can be used as commands. The combination allows you to do some really cool things with only a few keystrokes.

For example, the below JavaScript uses jQuery to find all <div> elements within a page that have a CSS class of “product”, and then animate them to slowly disappear:

$(“div.product”).slideUp(‘slow’).addClass(“removed”);

As another example, the JavaScript below uses jQuery to find a specific <table> on the page with an id of “datagrid1”, then retrieves every other <tr> row within the datagrid, and sets those <tr> elements to have a CSS class of “even” – which could be used to alternate the background color of each row:

$(“#datagrid1 tr:nth-child(even)”).addClass(“even”);

The jQuery library also works well on the same page with ASP.NET AJAX and the ASP.NET AJAX Control Toolkit.

Visual Studio figures out external script references, such as to JQuery, by following special reference comments it finds at the top of .js files:

/// <reference path="jquery-1.2.3.js" />

You can download the JQuery from here. You can get more details on JQuery from http://jquery.com/ .

For intellisense support for JQuery you need to install this Hotfix.

Comment for javascript is written inside for intellisense support. JavaScript has the interesting feature that calling toString on a function returns the code of this function including the comments and thus the doc comments. Here’s a similar example in JavaScript where the documentation is written inside of a class constructor:

jsintellisense

Another difference with C# or VB.NET is that property and event accessors are two different entities in JavaScript. For this reason, to choose where the documentation should be for those members. Properties should be documented in the getter and events in the “adder” for this reason:

js

Use CS and VB files in App_Code

We all know that App_Code folder contains source code files written as traditional class files — that is, files with a .vb extension, .cs extension, and so on.

But when we keep .cs and .vb file together in one folder and try to build the project it throws errors. So to successfully build the project we follow two simple steps .

Step#1 : Create two folders VBCode for .vb files and CSCode for .cs files.

Step#2:  Modify web.config file.

[code]<compilation>
<codeSubDirectories>
<add directoryName="CSCode"/>
<add directoryName="VBCode"/>
</codeSubDirectories>
</compilation>[/code]

That’s all, Hope this helps.
Enjoy !! 🙂

Reading Database value in Javascript

Today while developing a page, we felt need to read database into javascript array. And we were confused if at all we can do this. So finally after googling on this we found a workaround. So I just thought of sharing this with you because I think this is really helpful.

[code]<script language="JavaScript" type="text/javascript">
var allRecords = new array();
var count=0;
<%While (NOT rs_yours.EOF)%>
allrecords[count]="<%= rs_yours.Fields.Item("foo1").Value %>"
<%
rs_yours.MoveNext()
Wend
%>
</script>[/code]

This script will read the value from your datareader into javascript array.

Also if you want to read a single value from the server side into your javascript like this,

[code]var location='<%=Session["location"]%>’;[/code]

Hope this helps 🙂

SQL Execute with character string

Today I just got went though this, this helps you to execute a string in SQL. This is how you can execute a string in SQL,

[code]Declare @query Varchar(500)

Set @query = ‘Select * From Employees’

Exec (@query)[/code]

Another alternate option is as follows,

[code]EXEC (‘USE AdventureWorks; SELECT EmployeeID, Title FROM HumanResources.Employee;’)[/code]

This way you can execute a string in SQL.

This is extremely helpful when you need to add the Where clause based on some criteria. You can have your query in a string and based on that criteria you can append the clause in that string and finally execute the string at the end. This works perfectly.

However this does not work if you want to execute the query and get the result in the Dataset in your .Net application. For that you need to create a #Temp table execute the string and get the result in the #Temp table and then fire the select again on your #Temp table.

Note: Don’t forget to get the brackets ( & ) around your query else SQL will throw an error, Unrecognized stored procedure.