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

Sample Example of JQuery AutoComplete textbox in Asp.net by using WebMethod|| How to Use AutoComplete textbox in Asp.net by using JQuery and Webservice.


In this article I am showing about how to use JQuery AutoComplete textbox in Asp.net .
The following are the step to use AutoComplete textbox in Asp.net by using JQuery.
1.First design the table name Category with two fields Id,Category as follows:


2.Fill the Category details in table which you want like the below:


3. Open VS2010 and FileàNewàEmpty WebsiteàGive the name as JQueryAutoComplete.
4. Select solutionàRight clickà Add new Item àselect WebFormàDefault.aspx
5. Design the form with two fields one is label and one is Textbox to enter text as follows:


6.Then write the following code in Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>AutoComplete Text Box with jQuery</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script> 
                <script type="text/javascript">
                    $(document).ready(function() {
                        SearchText();
                    });
                    function SearchText() {
                        $(".auto").autocomplete({
                            source: function(request, response) {
                                $.ajax({
                                    type: "POST",
                                    contentType: "application/json; charset=utf-8",
                                    url: "Default.aspx/GetAutoCompleteData",
                                    data: "{'CategoryName':'" + document.getElementById('txtCategory').value + "'}",
                                    dataType: "json",
                                    success: function(data) {
                                        response(data.d);
                                    },
                                    error: function(result) {
                                        alert("Error Occurred");
                                    }
                                });
                            }
                        });
                    }
                </script>
</head>
<body>
    <form id="form1" runat="server">
   <div class="demo">
<div class="ui-widget">
    <label for="tbAuto">Enter Category Name: </label>
   <input type="text" id="txtCategory" class="auto" />
</div>
    </form>
</body>
</html>

7. Write the following code in Default.aspx.cs file:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Services;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
[WebMethod]

public static List<string> GetAutoCompleteData(string CategoryName)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=Naresh;Integrated Security=true;Initial Catalog=dbname"))
{
    using (SqlCommand cmd = new SqlCommand("select Category from Emergency_Category where Category LIKE '%'+@CategoryText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@CategoryText", CategoryName);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    result.Add(dr["Category"].ToString());
}
return result;
}
}
} 
}

8.Then build the application and run it, you will get output and type any letter in textbox then the output will be like below:


  In the above I entered  ‘a’ letter in textbox then it is showing  all the details of related to ‘a’ from database table Category.

In this way we can implement JQuery AutoComplete in Asp.net.

Thank You…….


JQuery Selectors and CSS Slectors in JQuery || Selecting Elements in JQuery


Jquery supports many different ways to select an element  or elements that should meet all but the most awkward requirements.
The following table shows the lists example of other selection methods you might want to use:
Example selection methods:
Selector
Selected
$(“#div”)
Select a div with ID of div1
$(“#div.standardDiv”)
Select all divs with a class of standardDiv.
$(“.standardDiv”)
Select all elements with a class of standardDiv
$(“#div4 #div5”)
Select a div with ID of div5 nested inside a div with id of div4
$(“#div”)
Select all divs.
$(“.standardDiv”)[0].innerHTML=”hello Naresh”;
Select the first element of a group of divs with class of standardDiv and set innerHTML property to  “hello Naresh”.

CSS Selectors:
In addition to the previous standard selectors, more modern selectors are also available.
The following table shows the lists example of CSS selection methods you might want to use:
CSS Selectors:
Selector
Selected
$(“div:first”)
First div
$(“div:last”)
Last div
$(“div:even”)
Even-numbered divs
$(“div:odd”)
Odd-numbered divs
$(“div:first-child”)
First child element
$(“div:last-child”)
Last  child element
$(“div:nth-child(3)”)
Third child element
$(“a[href^=http://]”)
Any hyperlink starting with the text http://
$(“a[href$=.zip/]”)
Any hyperlink ending with .zip
$(“a[href*=dotnet”)
Any hyperlink with the text dotnet in it
$(“input[type=button]”)[0].innerText=”hello Naresh”
Selects first input element of type button and changes innerText property to “hello Naresh”
$(“:checked”)
Gets all check boxes that are checked
$(“:disabled”)
All disabled elements
$(“:enabled”)
All enabled elements

jQuery also has provides some inbuilt selectors of its own:

jquery selectors:
Selector
Effect
$(“:button”)[0].innerText=”hello naresh”;
Change inner Text property of first button element.
$(“:contains(naresh)”)
All elements containing text naresh
$(“:hidden”)
All hidden elements
$(“:selected”)
All selected elements
$(“:visible”)
All visible elements
$(“div:not(.standardDiv)”)
Select all div elements that do not have a class of standardDiv



simple jquery menu animation using Asp.net || menu Animation using JQuery in Asp.net


The following article is showing about menu designing using JQuery in Asp.net:

Onething you should remember that, if you use jQuery related coding in your web page then you must add jQuery.js file must be included in that page.

Here, you just include the following Script in your web page.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript">  

Create a Web Form named as "JqueryMenu.aspx".
You should include the images in "Images" Folder Path, which you want to use in your page.

Copy and paste the following code in your .aspx section.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <style type="text/css">
        li
        {
            border:1px solid black;
            padding:1px 1px 1px 1px;
            width:220px;
            color:White;
            cursor:pointer;
        }
        a { color:White; font-family:Tahoma; }
    </style>

    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js"></script>

JQuery Function for animated menu:

    <script type="text/javascript">
        $(function () {
            $("ul.level1 li").hover(function () {
                $(this).stop().animate({ opacity: 1.7, width: "270px" }, "slow");
            }, function () {
                $(this).stop().animate({ opacity: 2, width: "170px" }, "slow");
            });
        });
    </script>
</head>


In Body Section:

<body>
    <form id="form1" runat="server">

                <asp:Image ID="Image1" runat="server" Height="137px"
                    ImageUrl="~/abstract-background.jpg" Width="942px" />
        <asp:Menu ID="Menu1" runat="server" Orientation="Horizontal"RenderingMode="List"
                    BackColor="White" DynamicHorizontalOffset="2" Font-Names="Verdana"
                    Font-Size="Large" ForeColor="Red" StaticSubMenuIndent="10px"
                    Font-Bold="True" Font-Italic="True" BorderColor="Blue"
                    BorderStyle="Double" Font-Overline="True">           
            <DynamicHoverStyle BackColor="#990000" ForeColor="White" />
            <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <DynamicMenuStyle BackColor="#FFFBD6" />
            <DynamicSelectedStyle BackColor="#FFCC66" />
            <DynamicItemTemplate>
                <%# Eval("Text") %>
            </DynamicItemTemplate>
            <Items>
                <asp:MenuItem NavigateUrl="~/Home.aspx" ImageUrl="~/home.jpg"Text="Home"
                    Value="Home" PopOutImageUrl="~/contactus.png"  />
                <asp:MenuItem NavigateUrl="~/About us.aspx" ImageUrl="~/about us.png"
                    Text="About Us" Value="AboutUs" />
                <asp:MenuItem NavigateUrl="~/product.aspx" ImageUrl="~/pro.jpg"
                    Text="Products" Value="Products" />
                <asp:MenuItem NavigateUrl="~/contact us.aspx" ImageUrl="~/contactus.png"
                    Text="Contact Us" Value="ContactUs" />
            </Items>
            <StaticHoverStyle BackColor="#990000" ForeColor="White" />
            <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <StaticSelectedStyle BackColor="#FFCC66" />
        </asp:Menu>
    </form>
</body>
</html>

Then run the application and see the Output Like the below: