Tuesday, August 2, 2011

asp.net web site handler ( *.ashx ) using ajax

TimeTableSave.ashx үүсгэ

<%@ WebHandler Language="C#" Class="TimeTableSave" %>

using System;
using System.Web;
using System.Xml;

public class TimeTableSave : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        var data = context.Request["data"] ?? "<root></root>";
       
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(data.ToString());
       
        XmlNode root = doc.DocumentElement;
        foreach(XmlNode day in root.ChildNodes)
        {
            foreach (XmlNode item in day.ChildNodes)
            {
                int profileId =  Convert.ToInt32(item.Attributes["profileId"].ToString());
                int WeekDayId = Convert.ToInt32(item.Attributes["dayId"].ToString());
                string Start = item.Attributes["startHourId"].ToString();
                string Finish = item.Attributes["endHourId"].ToString();
                TimeSheetBLL tbll = new TimeSheetBLL();
                tbll.insertTimeTableLine1(profileId, WeekDayId, Start, Finish);
            }
        }
    
        context.Response.Write("OK");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

default.aspx файл дээр

$.ajax({ url: 'TimeTableSave.ashx',
         data: encodeUrlComponent('my data').replace(%20/g, "+"),
         type: 'POST', dataType: 'html'
});

ингээл боллоо доо POST оор хийж байга бол %20/g тэмдэгтийг + болгохгүй бол алдаа заадаг

No comments: