Академический Документы
Профессиональный Документы
Культура Документы
A few days ago, Microsoft also released a VS2008 SP1 Hotfix to support all JavaScript files including jQuery intellisense for Visual Studio 2008. Note that this hotfix works only if you have VS 2008 with SP1 or Visual Web Developer Express Edition with SP1. You can download the Hotfix from here (Downloads tab).
Now drag and drop the jquery-1.2.6.js file from the Solution Explorer on to your page to create a reference as shown below
Note: Since you have applied the hotfix, you do not have to manually add a reference to the jquery-1.2.6-vsdoc.js file in your page. Once the reference to the runtime library has been added, Visual Studio automatically searches for the vsdoc file and loads it. You just need to keep both the runtime library and the documentation file next to each other. To test intellisense, add a <script> block and key in $(. You will get an intellisense similar to the one shown below:
Example 2: Demonstrating some animation on button click using jQuery Our first example was a simple Hello World example to keep up with the tradition while introducing a new language. Let us enhance our sample and display some animation using jQuery. We will animate an <asp:Panel> on a button click when the page loads. This sample is loosely based on the demo over here: To do so, add a HTML button and a <asp:Panel> to the page as shown below: <form id="form1" runat="server"> <input id="btnAnimate" type="button" value="Animate" /> <asp:Panel ID="Panel1" runat="server"> Some sample text in this panel </asp:Panel> </form> Also add some css to beautify the div (remember asp:Panel renders as a <div>). The <style> tag has been kept in the <head> element of the page. <style type="text/css"> div { background-color:#D5EDEF; color:#4f6b72; width:50px; border: 1px solid #C1DAD7; } </style> Finally add the jQuery code to animate the panel on button click. We would be using the animate function. Observe how the intellisense helps us out with the parameters
After adding in the parameters for the animate() function, the complete code will look similar to the following: <script type="text/javascript"> $(document).ready(function() { $("#btnAnimate").click(function() { $("#Panel1").animate( { width: "350px", opacity: 0.5, fontSize: "16px" }, 1800); }); }); </script> Here the animation occurs while changing the width, opacity and font properties of the Panel when the button is clicked. The value 1800 represents the number of milliseconds to run the animation. Run the sample and see the Panel getting animated as shown below:
Well thats it for an introduction. Remember that this article is only an introduction and in no way demonstrates the capabilities of jQuery. I have written an EBook which demonstrates the capabilities of doing client side development in ASP.NET using jQuery. It's called "51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls". In the forthcoming articles, we will see how jQuery can further be used with ASP.NET and AJAX. If you liked the article, Subscribe to my RSS Feed.
jQuery has gained popularity as the de facto standard for JavaScript development and many ASP.NET developers have adopted jQuery as part of their development kits. One question that I am asked frequently is how to use jQuery with an ASP.NET Master Page. Where should the jQuery file and code be placed in a Master Page? Note: If you are using jQuery with ASP.NET Controls, check out my EBook called 51 Recipes with jQuery and ASP.NET Controls. In this article, I am going to answer these questions and will demonstrate how to use jQuery with an ASP.NET MasterPage. We will see some jQuery code that clones the content
of one TextBox into another TextBox. If you are new to jQuery, check my article Using jQuery with ASP.NET Beginners Guide. Lets get started. Step 1: Create a Master Page (MasterPage.master) and add a reference to the jQuery Library. Now create a folder called Scripts and download the latest jQuery library into this folder. Now add references to any CSS files, if any. Create a separate folder to store all CSS and images files. You should always make it a habit to keep your resources in separate folders for better maintainability. The code should look similar to the following:
Step 2: Now create a Content Page called Default.aspx and add two TextBox controls to this page as shown below:
Step 3: Now in the Scripts folder, create a textboxclone.js file and add the following code in it.
Using the code shown above, as the user types in the first TextBox, we retrieve the value of the first TextBox and set it to the value of the second TextBox. Note: Since we are capturing the keyup event, whenever the user pastes text in the first textbox using Ctrl+v, the contents are cloned into the second textbox, This is however not true when the user right clicks the textbox and chooses paste from the context menu The last step is to reference this JavaScript file (textboxclone.js) in your Content Page as shown below:
You must be wondering why did we add a link to the textboxclone.js from a Content page rather than doing it from a Master Page. The reason is, if we had referenced the textboxclone.js file in the MasterPage directly, the code would be downloaded for every content page, even if it was not needed. Referencing it from a Content Page makes sure that the resource is used only when this content page is requested. Thats it. Run the code and you will see the textbox text cloning in action.
I hope you liked this article and I thank you for viewing it. The entire source code of this article can be downloaded over here.
In this article, we will see how to add values of multiple ASP.NET Checkboxes using jQuery. This example could be useful in scenarios where users are asked to select services they would like to avail and each service has a rate displayed like in a webhosting service. Using jQuery, the total service cost should be automatically calculated, as the services are selected.
Note: If you are using jQuery with ASP.NET Controls, check out my EBook called 51 Recipes with jQuery and ASP.NET Controls. Create an ASP.NET Website. Add the following markup
Now add a reference to the latest jQuery script file and write the following code:
Let us see what we just did: When a checkbox is clicked, a variable checked keeps track of the checked checkboxes.
We then iterate the checked collection using $().each() and add the text of the checkbox to a variable total.
Observe how we are accessing the value of a checkbox using $(this).next().text()); This is because the checkbox gets rendered like this:
To access the value, which is inside the label, we use next() which finds the very next sibling of each checkbox, which in our case is the label control. The parseFloat( )parses strings into numbers. The result is displayed in a paragraph (tot) using the toFixed() method, which rounds the result to the specified number of decimal places, in our case 2. $('#tot').text("Your Total Amount Is: " + total.toFixed(2)); The output is shown here:
See a Live Demo. This demo has been tested in the following browsers: IE 7, IE 8, Firefox 3, Chrome 2, Safari 4. The entire source code of this article can be downloaded over here
In this simple article, we will see how to convert an ASP.NET DropDownList into Multi Select ListBox, in order to select multiple items. This article is a chapter from my EBook called 51 Recipes with jQuery and ASP.NET Controls. The chapter has been modified a little to publish it as an article and use the latest jQuery version. Note that for demonstration purposes, I have included jQuery code in the same page. Ideally, these resources should be created in separate folders for maintainability. Its also always best to choose appropriate server side controls at the time of designing the application. If the user turns off JavaScript in his browser, the jQuery code won't work. So use this example with caution. The code shown below has been tested on IE7, IE8, Firefox 3, Chrome 2 and Safari 4. Multiple items cannot be selected in a DropDownList unless you change the default behavior of this control by writing custom code. Its the ListBox control that allows the user to select multiple items when its SelectionMode property is set to multiple. I faced this requirement recently during a client visit. The client was keen on keeping a DropDownList to save screen space. However they also wanted to convert the DropDownList to a ListBox, if the user wanted to select multiple items. So to solve this requirement client-side, a jQuery was used to 'make it appear' that we are converting the DropDownList to a MultiSelect ListBox control on the click of a Button. Let us see the entire code first
As you can see, to convert the DropDownList to a MultiSelect ListBox control on the click of a Button, we just requires a single line of jQuery code as shown here:
Once the DropDownList is converted into a ListBox control, the user can select multiple items and list them by using the code shown below
After the Button is clicked, the DropDownList is converted to a Multi Select Listbox as shown below:
After the user selects multiple values and clicks the Button, the output changes as shown here:
See a Live Demo I hope you found this article useful and I thank you for viewing it. This article was taken from my EBook called 51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls which contains similar recipes that you can use in your applications. The entire source code of this article can be downloaded over here