Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Thursday, February 02, 2012

How to open and auto close JQuery UI Dialog box on click of Hyperlink or Anchor Tag

In this article i will be explaining, How to open and auto close and change content of JQuery UI Dialog box on click of Hyperlink or Anchor Tag.

I have assumed that you have already created VS.Net Web Project and add JQuery UI reference into your web project.  If you need help in adding JQuery UI into asp.net website, then please read my post on how to add JQuery UI into asp.net website

Create a asp.net content page and add following code
Click Me to Open and auto close Dialog Box


Please wait for 5 Seconds and JQuery UI Dialog box will auto close.




   

This is My Dialog box Description/Content

   

This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.







Understanding above code
1) I have declared a simple asp.net hyperlink control (Since it has runat="server" it is asp.net control)
2) setTimeOut javascript function will cause dealy, this helps us to close JQuery UI dialog box after few seconds.
Syntax: setTimeout("javascript function declaration",milliseconds);
Example:
   setTimeout(
                function () {
                    $("#dialog").dialog("close");
                }, 5000);

Above line will cause a delay 5 seconds and then auto close dialog box.

Watch out Live Demo

For More on JQuery Tutorials

How to open and close JQuery UI Dialog box on click of Hyperlink or Anchor Tag

In this article i will be explaining, How to open and close JQuery UI Dialog box on click of Hyperlink or Anchor Tag

I have assumed that you have already created VS.Net Web Project and add JQuery UI reference into your web project.  If you need help in adding JQuery UI into asp.net website, then please read my post on how to add JQuery UI into asp.net website

Create a asp.net content page and add following code


Click Me to Open Dialog Box



   

This is My Dialog box Description/Content

   

This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.








Understanding above code
1) I have declared a simple asp.net hyperlink control (Since it has runat="server" it is asp.net control)
2) $("#dialog").dialog({ autoOpen: false });  - By default dialog box will open on page load, this line will explicitly disallow dialog box to open on page load.
3) Inside hyperlink click event, I have wrote code to open JQuery UI dialog box.
        $("#<%=hlOpenMe.ClientID%>").click(
            function () {
                $("#dialog").dialog('open');
                return false;
            }
        );

Similarly if you want to close JQuery UI dialog box  change below line with "close" rather than "open"
                $("#dialog").dialog('close');

Watch out Live Demo


For More on JQuery Tutorials

How to open and close JQuery UI Dialog box on Button Click

In this article i will be explaining, How to open and close JQuery UI Dialog box on Button Click.

I have assumed that you have already created VS.Net Web Project and add JQuery UI reference into your web project.  If you need help in adding JQuery UI into asp.net website, then please read my post on how to add JQuery UI into asp.net website

Create a asp.net content page and add following code





   

This is My Dialog box Description/Content

   

This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.







Understanding above code
1) I have declared a simple asp.net button control
2) $("#dialog").dialog({ autoOpen: false });  - By default dialog box will open on page load, this line will explicitly disallow dialog box to open on page load.
3) Inside button click event, I have wrote code to open JQuery UI dialog box.

        $("#<%=btnOpenMe.ClientID%>").click(
            function () {
                $("#dialog").dialog('open');
                return false;
            }
        );

Similarly if you want to close JQuery UI dialog box  change below line with "close" rather than "open"
                $("#dialog").dialog('close');


Watch out Live Demo


For More on JQuery Tutorials

How to open JQuery UI Dialog box on Page Load - AutoOpen

In this article i will be explaining, How to open JQuery UI Dialog box on Page Load - AutoOpen.

I have assumed that you have already created VS.Net Web Project and add JQuery UI reference into your web project.  If you need help in adding JQuery UI into asp.net website, then please read my post on how to add JQuery UI into asp.net website

Create a asp.net content page and add following code


   

This is My Dialog box Description/Content


This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.







Understanding above code.
- You can be able to change dialog title by editing title.

   

This is My Dialog box Description/Content

 
You can be able to change dialog description/message by editing paragraph tag.  You can write anything which is valid html inside div tag.


    $(function () {
        $("#dialog").dialog(); - Please note in above example, JQuery UI dialog box has ID as "dialog", so we have $("#dialog") and call dialog method to show dialog box.

By default JQuery UI dialog box opens on page load, show we haven't done anything specific to open it on page load.


Preview of code.


You can also show image or any valid html inside JQuery UI dialog box.  Here is the example.

Watch out Live Demo

For More on JQuery Tutorials

Wednesday, February 01, 2012

JQueryUI Demo and Tutorial for Asp.net Developers

This article contains all links for JQuery UI Demo and Tutorials for Asp.net Developers.

**Please note: I am in middle of making JQuery Tutorial, so this page will be updating frequently until this tutorial is complete, their is lot to come yet.  I will be using this post as a placeholder to track my progress and making tutorial

How to add JQuery UI to Asp.net Website

JQuery UI contains sets of controls and themes which you can simply add and start using it, just the way you can use any other third party jquery control.  Incase if you have used Asp.net Ajax Control toolkit in past, then Jquery UI controls are very similar to that.  Let me cut this discussion here and directly show how you can start using JQuery UI in asp.net website, later you can decide how helpful this is for your asp.net projects.

Step 1: Download JQuery UI
Click this link to Download JQuery UI


On Themes Page, click on Gallery tab and select the theme best suitable for your project, you can even customize its color and setting by clicking on edit button, ones you are done, click on download button.

On Download Page, on very right side you will find "Download" button. Its little confusing if you are visiting this page for first time.  Atleast i found it confusing, because i don't read all details :)   

Extract the .zip file, it comes up with 3 folders and 1 index.html file
  1. css folder
  2. development-bundle folder and
  3. js folder
The only folder we are interested in is css folder.  Copy "css" folder and put in your VS.Net Solution directory.  (Incase you haven't created a VS.Net Web Project Please create it now)

Ones you have copied "css" folder inside VS.Net solution directory, click on "Show All Files" from solution explorer.

This make "css" directory to appeared in VS.Net Solution, now right click "css" directory and select "Include in Project" option.

You are done adding necessary files to start "JQueryUI".

Step 2: Open Master Page of your project and add following lines just before
<%--For JQuery--%>


<%--For JQuery UI--%>


Step 3: Add CSS File reference in your project.  You can do so by simply drag and drop .css file from css folder.


That's it you are ready to use JQueryUI.

Incase you want to test whether JQueryUI is working or not simply create one asp.net page and add following lines of code in your content page.

This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.







And this will display JQueryUI dialog box on page load.
We will learn how this code works and more in my next coming blog post

Saturday, January 21, 2012

JQuery Ajax DB Call to Retrieve data based on user Criteria

In this article, i will be retrieving data from database with asp.net web service using jQuery Ajax.

If you are new to jQuery and don't know anything about it, I will recommend you to first read following articles before reading any further.


In this article we will take input country name and display region information. Check out Live Demo

Step 1: Create Asp.net Web Application


Step 2: Open Site.Master and include jQuery Reference by adding below line just before tag.






Step 3: Open "Default.aspx" and add following code
I have added here h2 title tag, asp.net button, textbox and label.  Please note, that i have declared ShowRegionInfo function, I will be calling javascript this function, which will internally make jQuery Ajax call to asp.net web service.



Example 4: JQuery DB Call to Retrieve data based on user Criteria




Retrieve region based on country name


Enter Country Name:

OnClientClick="ShowRegionsInfo();return false;" />




Step 4: Add new folder to solution named "WebService"


Step 5: Right click on "WebService" folder and click add new item, to add new web service.


Step 6: Add code to make database call to retrieve region data using jQuery Ajax.  Please add following code inside "wsJQueryDBCall.asmx.cs" file
Add following namespaces

using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Text;


Add following code



[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class wsJQueryDBCall : System.Web.Services.WebService
{


    [WebMethod]
    public string ReadRegion(string CountryName)
    {
        String strConnString = ConfigurationManager.AppSettings["connStr"].ToString();


        String strQuery = "Select Region " +
                            "from Regions " +
                            "Inner Join CountriesNew on CountriesNew.CountryId = Regions.CountryId " +
                            "where UPPER(CountriesNew.Country) = @CountryName";


        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@CountryName", CountryName.ToUpper());
                cmd.CommandText = strQuery;
                cmd.Connection = con;
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                StringBuilder sb = new StringBuilder();


                if (sdr.HasRows)
                {
                    sb.Append("" + CountryName + " has following regions:
");



                    while (sdr.Read())
                    {
                        sb.Append(sdr["Region"].ToString() + "
");

                    }
                }
                else
                {
                    sb.Append("No Records found");
                }


                con.Close();


                return sb.ToString();
            }
        }
    }
}

As you have noticed ReadRegion Method is regular method which takes input country name and makes database call to retrieve region data from sql server database and return result as appended string of region.


Important Note:  Please UnComment line
[System.Web.Script.Services.ScriptService]
In order to allow this Web Service to be called from script, using ASP.NET AJAX



Step 7:  Open "Default.aspx" and Write jQuery function to make webservice call to retrieve data from database.






Understanding jQuery Ajax call parameter

Now, run the web application and input your country name and hit button to see region which is retrieved from sql server database.


Check out Live Demo of Calling Webservice using JQuery Ajax Example

More JQuery Tutorials

JQuery Ajax Examples by calling asp.net webservice

In this article, i will be calling asp.net web service using jQuery Ajax.

With JQuery Ajax you can do

  • Make a database call and perform CRUD operation (Create, Read, Update and Delete) without page postback
  • If you have used Ajax Control Toolkit in past then you will be able to do almost all such operations and lot more by using jQuery Ajax and JQuery UI.

If you are new to jQuery and don't know anything about it, I will recommend you to first read following article before reading any further.

OK, so lets start the real fun

Since it is our first example, lets keep it simple and display "Hello World". Check out Live Demo

Step 1: Create Asp.net Web Application


Step 2: Open Site.Master and include jQuery Reference by adding below line just before tag.






Step 3: Open "Default.aspx" and add following code
I have added here h2 title tag, asp.net button and label.  Please note, that i have declared OnClientClick, I will be calling javascript function, which will internally make jQuery Ajax call to asp.net web service.

Example 1: Call Webservice using JQuery AJax (Without Input)




OnClientClick="DisplayMessageCall();return false;" />





Step 4: Add new folder to solution named "WebService"


Step 5: Right click on "WebService" folder and click add new item, to add new web service.


Step 6: Add code to display message inside "HelloWorld.asmx.cs" file
///
/// Summary description for HelloWorld
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class HelloWorld : System.Web.Services.WebService
{
    [WebMethod]
    public string DisplayMessage()
    {
        return "Hello World using jQuery Ajax";
    }
}
Important Note:  Please UnComment line
[System.Web.Script.Services.ScriptService]
In order to allow this Web Service to be called from script, using ASP.NET AJAX



Step 7:  Open "Default.aspx" and Write jQuery function to make webservice call to display "Hello World" message.

Understanding jQuery Ajax call parameter
  • type: Can be POST or GET.
  • url: Path of webservice file (Example here: path of HelloWorld.asmx file) In the above code you can find that i have declare path of webservice in pageurl variable.  Since we are going to call "DisplayMessage" method name inside webservice we have write pageUrl + "/DisplayMessage"  (i.e. Webservice Path + Method Name)
  • data: In this example the data will remain empty, as we are only calling a method which return a simple string and don’t accept any parameter. If the method has some parameters then we will pass the parameters. I will explain on passing parameters in my coming posts.
  • contentType: Should remain the same.
  • datatype: Should remain as it is.
  • success: Here I have called the OnSuccessCall when the call is complete successfully. If you check the OnSuccessCall method you will see that I have set the returned result from the web service to the label. In the OnSuccessCall method body you see ‘data.d’. The ‘d’ here is the short form of data.
  • Error: Same as I have done with OnSuccessCall. If any error occurred while retrieving the data then the OnErrorCall method is invoked.
Now, run the web application and hit button to see message.

Check out Live Demo of Calling Webservice using JQuery Ajax Example

Note: Please remember, JQuery is Javascript library so you will NOT be able to retrieve large amount of data or do heavy database operations.  Example: If you want to load 7000 Records from database then JQuery Ajax is not good approach.  To best of my knowledge you will never run into such situation except loading cascading dropdown, incase you run in any such situation than you should rethink your approach.

Friday, January 20, 2012

jQuery Ajax Example - Call Webservice using JQuery AJax (With Multiple Input)

In this article, i will be calling asp.net web service using jQuery Ajax with Multiple Input parameter.

If you are new to jQuery and don't know anything about it, I will recommend you to first read following article before reading any further.


In this article we will take input name and location and display greeting message. I will be also adding small delay to create Ajaxify effect.  Check out Live Demo

Step 1: Create Asp.net Web Application


Step 2: Open Site.Master and include jQuery Reference by adding below line just before tag.






Step 3: Open "Default.aspx" and add following code
I have added here h2 title tag, asp.net button, textbox and label.  Please note, that i have declared OnClientClick, I will be calling javascript function, which will internally make jQuery Ajax call to asp.net web service.


Example 3: Call Webservice using JQuery AJax (With Multiple Input)


Enter your Name:

Enter Location:

OnClientClick="DisplayMessageCall();return false;" />



Step 4: Add new folder to solution named "WebService"


Step 5: Right click on "WebService" folder and click add new item, to add new web service.


Step 6: Add code to display message inside "HelloWorld.asmx.cs" file


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class HelloWorld : System.Web.Services.WebService
{


    [WebMethod]
    public string DisplayMessage(string NameInfo, string Location)
    {
        //Creating Delay
        System.Threading.Thread.Sleep(1000);
        return "Welcome " + NameInfo + ", Your location is " + Location;
    }
}

Important Note:  Please UnComment line
[System.Web.Script.Services.ScriptService]
In order to allow this Web Service to be called from script, using ASP.NET AJAX



Step 7:  Open "Default.aspx" and Write jQuery function to make webservice call to display greeting message.



In above code before calling webservice i have assign "Please wait..." to output label.


Understanding jQuery Ajax call parameter

Now, run the web application and input your name and hit button to see greeting message.

You might notice that code will cause small delay, it will show "Please wait..." message while it is causing delay.



Check out Live Demo of Calling Webservice using JQuery Ajax Example

More JQuery Tutorials

jQuery Ajax Example - Call Webservice using JQuery AJax (With Input)

In this article, i will be calling asp.net web service using jQuery Ajax with Input parameter.

If you are new to jQuery and don't know anything about it, I will recommend you to first read following article before reading any further.


In this article we will take input name and display greeting message. Check out Live Demo

Step 1: Create Asp.net Web Application


Step 2: Open Site.Master and include jQuery Reference by adding below line just before tag.






Step 3: Open "Default.aspx" and add following code
I have added here h2 title tag, asp.net button, textbox and label.  Please note, that i have declared OnClientClick, I will be calling javascript function, which will internally make jQuery Ajax call to asp.net web service.

Example 2: Call Webservice using JQuery AJax (With Input)



OnClientClick="DisplayMessageCall();return false;" />



Step 4: Add new folder to solution named "WebService"


Step 5: Right click on "WebService" folder and click add new item, to add new web service.


Step 6: Add code to display message inside "HelloWorld.asmx.cs" file

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class HelloWorld : System.Web.Services.WebService
{


    [WebMethod]
    public string DisplayMessage(string NameInfo)
    {
        return "Welcome " + NameInfo + " to the world of JQuery AJax";
    }
}

Important Note:  Please UnComment line
[System.Web.Script.Services.ScriptService]
In order to allow this Web Service to be called from script, using ASP.NET AJAX



Step 7:  Open "Default.aspx" and Write jQuery function to make webservice call to display greeting message.


Understanding jQuery Ajax call parameter

Now, run the web application and input your name and hit button to see greeting message.


Check out Live Demo of Calling Webservice using JQuery Ajax Example

More JQuery Tutorials

Wednesday, September 28, 2011

JQuery Focus Event

In this article we will learn how JQuery focus events work

Focus event triggers whenever control receives focus.  Example: change textbox background color on textbox focus.

Example 1 - Focus Event: Change textbox background color on Focus (Live Demo)
I have a 3 html input control, whose id is "txtInput1", "txtInput2" and "txtInput3" on receiving focus it should change its background color to Yellow, Red and Green.


Input 1:


Input 2:


Input 3:



JQuery script for changing background color on receiving focus





Example 2 - FocusIn Event: FocusIn Event is similar to focus event.  Let try to Change textbox background color on Focus with focusin JQuery event (Live Demo)
I have a 3 html input control, whose id is "txtInput1", "txtInput2" and "txtInput3" on receiving focus it should change its background color to Yellow, Red and Green.


Input 1:


Input 2:


Input 3:



JQuery script for changing background color on receiving focus




Example 3 - FocusOut Event: Change textbox background color on Focus out (Live Demo)
I have a 2 html input control, whose id is "txtInput1" and "txtInput2"  

Input 1:


Input 2:



JQuery script for changing background color on receiving focus


I will be discussing more JQuery examples in JQuery example series.

JQuery Select Event

In this article we will learn how JQuery select events work

The select event is sent to an element when the user makes a text selection inside it. This event is limited to fields and


JQuery script for showing alert on text content selection






I will be discussing more JQuery examples in JQuery example series.

JQuery Change Event

In this article we will learn how JQuery change events work

Change event will fire, when value of element is getting changed.  Example:  If we try to change textbox value it should try to validate it.

Example 1: Show Alert when textbox value is changed using JQuery Change Event (Live Demo)
I have a html textbox control, whose id is "txtInput" on changing value of textbox content, I will display alert.

Type anything in below textbox to trigger, change event

JQuery script for showing alert on text content change




I will be discussing more JQuery examples in JQuery example series.

JQuery Keydown, Keypress and Keyup Event

In this article we will learn how JQuery keydown, Keypress and Keyup events work.

  • Keydown Event bind an event handler to the "keydown" JavaScript event, or trigger that event on an element.
  • Keypress Event bind an event handler to the "keypress" JavaScript event, or trigger that event on an element.
  • Keyup Event bind an event handler to the "keyup" JavaScript event, or trigger that event on an element.
This events are very similar and you will find very minor difference between them.  To  understand difference between Keydown, Keypress and Keyup event is sequence in which they triggers.
  • First Keydown Event occurs (Triggers as soon as key is press)
  • Second Keypress Event occurs (Triggers when key character is typed)
  • Third Keyup Event occurs (Triggers when key is up, after it is been pressed)

Example 1 - Keydown Event: We will show alert in all situation whenever user press any key in textbox. (Live Demo)

I have a html input control, whose id is "txtInput" on writing any character inside textbox it should display alert.


Type anything in below textbox to trigger, keydown event



JQuery script for showing alert on keydown event





Example 2 - Keypress Event: We will show alert in all situation whenever user press any key in textbox. (Live Demo)

In above example you might have notice problem with event.keyCode, it triggers as soon as we press the key.  That is we cannot used this event when we want to check combination of key (Example: shift + 2 .i.e. "@" symbol is pressed or not)  For such situation were we want to identify which character is pressed, it is good to use event.which event.  Let understand that with example

I have a html input control, whose id is "txtInput" on writing any character inside textbox it should display alert.


Type anything in below textbox to trigger, keydown event



JQuery script for showing alert on keypress event






Example 3 - Keyup Event: We will show alert in all situation whenever user press any key in textbox. (Live Demo)

I have a html input control, whose id is "txtInput", when user releases key on keyboard this event will trigger and it should display alert.


Type anything in below textbox to trigger, keydown event



JQuery script for showing alert on keyup event




I will be discussing more JQuery examples in JQuery example series.

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape