Wednesday, July 10, 2013

How to call code behind method from jquery

ASPX PAGE
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!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 runat="server">
    <title></title>
    <script src="myjavascript.js" type="text/javascript"></script>
    <script type = "text/javascript">
        function DisplayMessageCall() {

            var pageUrl = '<%=ResolveUrl("~/WebService/HelloWorld.asmx")%>'

            $('#<%=lblOutput.ClientID%>').html('Please wait...');

            $.ajax({
                type: "POST",
                url:  "Default4.aspx/DisplayMessage",
                data: "{'NameInfo':'" + $('#<%=txtName.ClientID%>').val()
                    + "','Location':'" + $('#<%=txtLocation.ClientID%>').val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccessCall,
                error: OnErrorCall
            });

        }

        function OnSuccessCall(response) {
            $('#<%=lblOutput.ClientID%>').html(response.d);
        }

        function OnErrorCall(response) {
            alert(response.status + " " + response.statusText);
        }
</script>


</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>&nbsp;Call Webmethod using JQuery AJax (With Multiple Input)</h2>
Enter your Name: <asp:TextBox ID="txtName" runat="server" Text=""></asp:TextBox><br />
Enter Location: <asp:TextBox ID="txtLocation" runat="server" Text=""></asp:TextBox><br />
<asp:Button ID="btnGetMsg" runat="server" Text="Click Me"
            OnClientClick="DisplayMessageCall();return false;" onclick="btnGetMsg_Click" /><br />
<asp:Label ID="lblOutput" runat="server" Text=""></asp:Label>

    </div>
    </form>
</body>
</html>



CODE BEHIND PAGE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class Default4 : System.Web.UI.Page
{
    [WebMethod]
        public static string DisplayMessage(string NameInfo, string Location)
        {
            //Creating Delay
            System.Threading.Thread.Sleep(1000);
            return "Welcome " + NameInfo + ", Your location is " + Location;
        }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnGetMsg_Click(object sender, EventArgs e)
    {

    }

}

How to use toggle in Jquery

ASPX PAGE

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!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 runat="server">
    <script src="myjavascript.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {

        alert("I am ready!");
            $("div.test").add("p.quote").html("a little test").toggle(3000);
      });

   
</script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div class="test">
  <p class="quote"> this is division...</p>
    </div>
    </form>
</body>
</html>

How to bind gridview with Jquery

ASPX Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default9.aspx.cs" Inherits="Default9" %>

<!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 runat="server">
    <title></title>
    <script src="myjavascript.js" type="text/javascript"></script>
    <script type="text/javascript">
       
        $(function () {
            $("div.test").toggle(15000)
            $.ajax({

            type: "POST",
            url: "Default9.aspx/GetCustomers",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });
    });

    function OnSuccess(response) {
        var xmlDoc = $.parseXML(response.d);
        var xml = $(xmlDoc);
        var customers = xml.find("Table");
        var row = $("[id*=gvCustomers] tr:last-child").clone(true);
        $("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
        $.each(customers, function () {
            var customer = $(this);
            $("td", row).eq(0).html($(this).find("RBDID").text());
            $("td", row).eq(1).html($(this).find("BATCHID").text());
            $("td", row).eq(2).html($(this).find("REQUESTID").text());
            $("[id*=gvCustomers]").append(row);
            row = $("[id*=gvCustomers] tr:last-child").clone(true);
        });
    }
</script>


</head>
<body>
    <form id="form1" runat="server">
    <div class="test">
    <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" Font-Names="Arial"
    Font-Size="10pt" RowStyle-BackColor="#A1DCF2" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor = "White">
    <Columns>
        <asp:BoundField ItemStyle-Width="150px" DataField="RBDID" HeaderText="CustomerID" />
        <asp:BoundField ItemStyle-Width="150px" DataField="BATCHID" HeaderText="CustomerID" />
        <asp:BoundField ItemStyle-Width="150px" DataField="REQUESTID" HeaderText="City" />
    </Columns>
</asp:GridView>

<asp:Button runat="server" ID="b1" Text="click me" />
    </div>
    </form>
</body>
</html>


Code Behind Page

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
using System;

public partial class Default9 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindDummyRow();
        }
    }

    private void BindDummyRow()
    {
        DataTable dummy = new DataTable();
        dummy.Columns.Add("RBDID");
        dummy.Columns.Add("BATCHID");
        dummy.Columns.Add("REQUESTID");
        dummy.Rows.Add();
        gvCustomers.DataSource = dummy;
        gvCustomers.DataBind();
    }



    [WebMethod]
    public static string GetCustomers()
    {
        string query = "select top 10 RBDID,BATCHID,REQUESTID From dbo.RG_ResponseBatchData";
        SqlCommand cmd = new SqlCommand(query);
        return GetData(cmd).GetXml();
    }
    private static DataSet GetData(SqlCommand cmd)
    {
        string strConnString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    return ds;

                }
            }
        }
    }

}


Sunday, July 7, 2013

Access server side function using JQuery.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Website1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script lang="javascript" src="JQuery.js" type="text/javascript"></script>
    <script lang="javascript">
            function chk()
            {
            $.ajax (
            {
                type: "POST",
                url: "WebForm1.aspx/show",  // show is the name of function 
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                async: true,
                cache: false,
                success: function (msg) 
                {
                    $('#div1').text(msg.d); 
                },
                error: function (x, e)
                {
                alert("The call to the server side failed. " + x.responseText);
                }
            });
                return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div id="div1" style="width:500px; height:40px; background-color:aqua">

            This is hidable.

        </div>

    <div>
        <asp:TextBox ID="Text"  CssClass="abc" Text="" runat="server"></asp:TextBox>
        <asp:Button ID="btnShow" OnClientClick="return chk();" runat="server" OnClick="btnShow_Click" Text="Javascript" />
    </div>
    </form>
</body>
</html>

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...