Click here to Skip to main content
11,478,562 members (67,995 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Ajax
How to use ajax with handler in asp.net.
and i want to return result as JSON and handle in ajax response.
Posted 4-Feb-13 17:40pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Edo Tzumer at 5-Feb-13 3:30am
   
5+
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think you need to read
Simple JSON Handler for ASP.NET 2.0 to Implement AJAX Functionality[^]


Hope it helps.
--Amit
  Permalink  
Comments
Edo Tzumer at 5-Feb-13 3:31am
   
5+
_Amy at 6-Feb-13 3:36am
   
Thanks Edo. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

$(document).ready(function () {
         var param = { action: 'AllCategories' };
         var actionData = JSON.stringify(param);
         
         $.ajax({
            url: "AjaxHandler.ashx", // ?action=AllCategories
            data: { action: 'AllCategories' },
            dataType: '',
            success: SuccessCall_AJAXCall_Category
         });      });



AjaxHandler.ashx

case "AllCategories":
               DataSet dsCategories = TaskUtilities.GetCategories();
               if (dsCategories != null && dsCategories.Tables.Count > 0)
               {
                  dsCategories.Tables[0].TableName = "Tbl_Category";
                  context.Response.Write(Serialize.ToJson(dsCategories.Tables[0]));
               }
               break;
 
Serialize.cs
 
 public static string ToJson(DataTable dtFilteredData)
        {
            var jsonData = new StringBuilder();
 
            try
            {
                string[] tmp = dtFilteredData.TableName.Split('_');
 
                jsonData.Append("\"tbl_" + tmp[1].ToLower() + "\":[");
 
                foreach (DataRow row in dtFilteredData.Rows)
                {
                    jsonData.Append("{");
                    foreach (DataColumn col in dtFilteredData.Columns)
                    {
                        jsonData.Append("\"" + col.ColumnName.ToLower() + "\":\"");
                        if (col.DataType.ToString() == "System.DateTime")
                            jsonData.Append(Convert.ToDateTime(row[col]).ToString("dd MMM yyyy hh:mm:ss t\\M") + "\",");
                        else
                        {
                            jsonData.Append(row[col].ToString().Replace("\\", "\\\\").Replace("\"", "\\\"") + "\",");
                        }
                    }
                    string tmp1 = jsonData.ToString();
                    jsonData.Remove(0, tmp1.Length);
 
                    jsonData.Append(tmp1.Substring(0, tmp1.LastIndexOf(",", System.StringComparison.Ordinal)));
                    jsonData.Append("},");
                }
                string tmp2 = jsonData.ToString();
                jsonData.Remove(0, tmp2.Length);
                jsonData.Append(tmp2.Substring(0, tmp2.LastIndexOf(",", System.StringComparison.Ordinal)));
                jsonData.Append("]");
            }
            catch
            {
                return string.Empty;
            }
 
            return jsonData.ToString();
        }
    }
jScript.cs

function SuccessCall_AJAXCall_Category(data) {
   // debugger;
   var ddl = $('select[id$=ddlCategories]');
   ddl.html("");
   var options = ''; // '<option value = 0>All</option>';
   //Data have only data with name tbl_continents not in valid format so i added flower brackets before and after. for validation of json Use "http://jsonlint.com/"
   // console.log(data);
   var jsval = $.parseJSON('{' + data + '}');
   // debugger;
   var tabCategory = jsval.tbl_category; // all small letters

   for (var i = 0; i < tabCategory.length; i++) {
      options += '<option value="' + tabCategory[i].ntaskcategoryid + '">' + tabCategory[i].strcategorytext + '</option>'; // field names, all small letters
   }
   // $("#ddlCategories").html(options); // --- HTML Control
   ddl.html(options); // --- Server Control

};
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 229
1 F-ES Sitecore 170
2 Sascha Lefèvre 160
3 Frankie-C 120
4 OriginalGriff 105
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,321
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,325


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100