Page name ::
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PackageList.aspx.cs" Inherits="json.PackageList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<%-- <script src="js/jquery.min.js"></script>--%>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/SearchPackage.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="container">
<!-- leftcontent -->
<div class="row" style="height:250px;">
<div class="span7 well">
<div class="" style="float:left">
<div id="golftitle" >
<span style="font-family:Calibri;font-size:5px;"></span>
</div>
<div id="divList">
</div>
<div id="paginglist">
</div>
</div>
</div>
<div class="span4">
<div class="well" >
<h5 >
Golf Packages By City:</h5><hr/>
<ul id="ul_cityList">
</ul>
</div>
</div>
</div>
</div>
</div>
<asp:HiddenField ID="hdnTourID" runat="server" />
<asp:HiddenField ID="hdnPageID" runat="server" />
<asp:HiddenField ID="hdnCountryID" runat="server" />
<asp:HiddenField ID="hdnStateID" runat="server" />
<asp:HiddenField ID="hdnCityID" runat="server" />
</form>
</body>
</html>
.CS
protected void Page_Load(object sender, EventArgs e)
{
hdnCountryID.Value ="0";
hdnStateID.Value = "0";
hdnCityID.Value = "0";
int page = 0;
if (page == 0)
{
hdnPageID.Value = "1";
}
else
{
hdnPageID.Value = page.ToString();
}
}
Web Service in service folder as
package.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Data;
using System.Text;
using System.Web.Script.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace json.service
{
/// <summary>
/// Summary description for package
/// </summary>
[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 package : System.Web.Services.WebService
{
[WebMethod]
public string[] SearchPackage(int cid, int sid, int cityid, int page)
{
try
{
string[] json = new string[3];
DAL.SearchPageDAL dal = new DAL.SearchPageDAL();
DataSet ds = dal.SearchPages(cid, sid, cityid, page);
if (ds.Tables[0].Rows.Count <= 0)
{
json[0] = string.Empty;
json[1] = string.Empty;
json[2] = string.Empty;
return json;
}
else
{
json[0] = Util.GetJsonString(ds.Tables[0]);
}
if (ds.Tables[1].Rows.Count > 0)
{
int count = Convert.ToInt32(ds.Tables[1].Rows[0]["total"].ToString());
int mode = count % 4;
if (mode > 0)
{
mode = 1;
}
int totalpage = count / 4;
json[1] = (totalpage + mode).ToString();
json[2] = count.ToString();
}
else
{
json[1] = string.Empty;
json[2] = string.Empty;
}
return json;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
DAL
public DataSet SearchPages(int CountryID, int StateID, int CityID, int PageName)
{
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlParameter[] prm = new SqlParameter[4];
prm[0] = new SqlParameter("@CountryID", CountryID);
prm[1] = new SqlParameter("@StateID", StateID);
prm[2] = new SqlParameter("@CityID", CityID);
prm[3] = new SqlParameter("@pageID", PageName);
DataSet ds = SqlHelper.ExecuteDataset(connStr, CommandType.StoredProcedure, "rw_usp_SearchPackage", prm);
return ds;
}
Util that stringfy data set into json format
public static string GetJsonString(DataTable dt)
{
string json = "[";
int count = 0;
foreach (DataRow row in dt.Rows)
{
count++;
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
Newtonsoft.Json.JsonWriter jsonWriter = jsonWriter = new Newtonsoft.Json.JsonTextWriter(sw);
jsonWriter.WriteStartObject();
foreach (DataColumn column in dt.Columns)
{
jsonWriter.WritePropertyName(column.ColumnName);
jsonWriter.WriteValue(row[column]);
}
if (count == dt.Rows.Count)
{
json = json + sw.ToString() + "}";
}
else
{
json = json + sw.ToString() + "},";
}
jsonWriter.WriteEndObject();
sw.Close();
}
json = json + "]";
return json;
}
JS that show value return by webservice
var _CountryID = 0; var _StateID = 0; var _CityID = 0; var _pageID = 0; var _SearchText='';
var _TourID = 0;
$(document).ready(function ()
{
try {
_CountryID = $('#hdnCountryID').val();
_StateID = $("#hdnStateID").val();
_CityID = $('#hdnCityID').val();
_pageID = $('#hdnPageID').val();
_SearchText = $('#hdnSearchText').val();
LoadResults();
}
catch (Error) {
alert(Error);
}
});
function LoadResults() {
try {
var urlgetcode = 'service/package.asmx/SearchPackage';
$.ajax({
type: "POST",
url: urlgetcode,
data: '{cid:"' + _CountryID + '",sid:"' + _StateID + '",cityid:"' + _CityID + '",page:"' + _pageID + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {
try {
if (msg.d[0] == '') {
$('#divList').before('<h2>No Golf found.</h2>Sorry, no Golf matched found in this country. View all Golf');
return;
}
var obj = jQuery.parseJSON(msg.d[0]);
var list = '';
for (i = 0; i < obj.length; i++) {
$page = $('<div>').html(obj[i].pagename);
if (i == 0 && _CountryID != 0) {
$('#golftitle').append('<h1>Golf IN <span id="place">' + obj[i].destination + ' </span></h1>');
}
else if (i == 0 && _CountryID == 0) {
$('#golftitle').append('<h1>View All Golf Travel</h1>');
}
list = list + '<div > ';
list = list + '<div >';
list = list + '<a href="offer-detail.aspx?id=' + obj[i].id + '"><img style="width:235px;height:165px;" src="images/ImgProduct/' + obj[i].photo + '" alt=""><span >View More Details</span></a>';
list = list + '</div>';
list = list + '<div >';
list = list + '<div >';
list = list + '<h2><a href="offer-detail.aspx?id=' + obj[i].id + '">' + obj[i].holiday + '</a></h2>';
list = list + '</div>';
list = list + '<div >';
list = list + $page.find("p:lt(2)").text(); ;
list = list + '</div>';
list = list + '</div>';
list = list + '<div class="re-bot">';
list = list + '<a href="offer-detail.aspx?id=' + obj[i].id + '" title="">' + obj[i].CityName + '</a> ';
list = list + '<a href="offer-detail.aspx?id=' + obj[i].id + '" title="">Photos</a>';
list = list + '</div>';
list = list + '<div ></div>';
list = list + '</div>';
}
$('#divList').before(list);
$('.count').append(parseInt(msg.d[2]) + ' available offers');
if (parseInt(msg.d[1]) <= 1) return;
var redirecturl = '&id=' + _CountryID + '&state=' + _StateID + '&city=' + _CityID;
var pageing = '<div ><div ><span >Pages :';
for (i = 1; i <= msg.d[1]; i++) {
if (_pageID == i) {
pageing = pageing + '<strong >' + _pageID + '</strong>';
}
else {
pageing = pageing + '<strong><a href="Golf-list.aspx?page=' + i + redirecturl + '">' + i + '</a></strong>';
}
}
pageing = pageing + '</span></div> <div ></div> </div>';
$('#paginglist').append(pageing);
}
catch (Error) {
alert(Error);
}
},
error: function (request) {
alert(request.responseText);
}
});
}
catch (Error) {
alert(Error);
}
}
References Added
Newtonsoft.Json