Wednesday 20 January 2016

JQuery get the queryString values.

Hello there,

I have come across the situation to get QueryString in JQuery and have some logic inside that so I am sharing the same.


<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
$(document).ready(function () {
var val = GetQuery('a');
if (val == 'someval') {

//HERE YOU CAN WRITE THE LOGIC AS PER THE VALUE YOU GET
}
function GetQuery(p) {
var url = window.locaion.href.slice(window.location.href.indexof('?') + 1).split('&');
for (var i = 0 ; i < url.length; i++) {
var urlp = url[i].split('=');
if (urlp[0] == p) {
return urlp[1];
}
}
}
});
</script>
</head>
<body>
</body>
</html>

Thursday 14 January 2016

Data Binding with AJAX\jquery in clinet side

aspx

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

<!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="js/jquery_1.8.3.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        function fillsubcountry() {
            var countryval = document.getElementById("<%=ddlcountry.ClientID %>").value;
            var response = WebForm2.state(countryval);
            binddrops(response.value, "<%=ddlstate.ClientID %>");
        }

        function fillsubstate() {
            var stateval = document.getElementById("<%=ddlstate.ClientID %>").value;
            var response = WebForm2.city(stateval);
            binddrops(response.value, "<%=ddlcity.ClientID %>");
        }

        function binddrops(dictObj, ddlObj) {
            $('#' + ddlObj).empty();
            $('#' + ddlObj).append($("<option>").attr('value''-1').text('--Select--'));
            $(dictObj).each(function () {

                $('#' + ddlObj).append($("<option>").attr('value'this.Key).text(this.Value));
            })
        }
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlcountry" runat="server" onchange="return fillsubcountry();">
        </asp:DropDownList>
        <asp:DropDownList ID="ddlstate" runat="server" onchange="return fillsubstate();">
        </asp:DropDownList>
        <asp:DropDownList ID="ddlcity" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

aspx.cs

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


    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(WebForm2), Page);
            if (!IsPostBack)
            {
                country();
            }
        }
        public void country()
        {
            List<Country> c = new List<Country>();
            c.Add(new Country
            {
                id = "1",
                name = "india"
            });
            c.Add(new Country
            {
                id = "2",
                name = "usa"
            });

            ddlcountry.DataValueField = "id";
            ddlcountry.DataTextField = "name";
            ddlcountry.DataSource = c;
            ddlcountry.DataBind();
            ddlcountry.Items.Insert(0, new ListItem("--Select--""0"));

        }
        [Ajax.AjaxMethod]
        public Dictionary<stringstring> state(string val)
        {
            Dictionary<stringstring> value = new Dictionary<stringstring>();
            List<State> s = new List<State>();
            s.Add(new State
            {
                id = "1",
                fid = "1",
                name = "maharashtra"
            });
            s.Add(new State
            {
                id = "2",
                fid = "1",
                name = "MP"
            });
            s.Add(new State
            {
                id = "3",
                fid = "1",
                name = "AP"
            });

            s.Add(new State
            {
                id = "4",
                fid = "2",
                name = "texas"
            });
            s.Add(new State
            {
                id = "5",
                fid = "2",
                name = "glasgo"
            });

            var f = from c in s
                    where c.fid.Equals(val)
                    select c;

            value = f.ToDictionary(x => x.id, x => x.name);
            return value;
        }
        [Ajax.AjaxMethod]
        public Dictionary<stringstring> city(string val)
        {
            Dictionary<stringstring> value = new Dictionary<stringstring>();
            List<City> t = new List<City>();
            t.Add(new City
            {
                id = "1",
                fid = "1",
                name = "mumbai"
            });
            t.Add(new City
            {
                id = "2",
                fid = "1",
                name = "goa"
            });
            t.Add(new City
            {
                id = "3",
                fid = "2",
                name = "mp_asas"
            });
            t.Add(new City
            {
                id = "4",
                fid = "2",
                name = "mp_weasd"
            });
            t.Add(new City
            {
                id = "5",
                fid = "3",
                name = "ap_asas"
            });
            t.Add(new City
            {
                id = "6",
                fid = "4",
                name = "texas_weasd"
            });
            t.Add(new City
            {
                id = "7",
                fid = "5",
                name = "glasgo_weasd"
            });
            var f = from c in t
                    where c.fid.Equals(val)
                    select c;

            value = f.ToDictionary(x => x.id, x => x.name);
            return value;
        }
    }
    class Country
    {
        public string id { getset; }
        public string name { getset; }
    }
    class State
    {
        public string id { getset; }
        public string fid { getset; }
        public string name { getset; }
    }
    class City
    {
        public string id { getset; }
        public string fid { getset; }
        public string name { getset; }
    }


web.config
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
<system.web>
    <httpHandlers>
      <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
    </httpHandlers>
<compilation debug="true" targetFramework="4.0">
</compilation>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
</configuration>