使用jquery調用handler文件中的方法,需要使用session,默認生成的文件中,不可以直接使用session。按照以下步驟,即可以通過session與其他的aspx頁面的session進行數據交互。
1,加入命名空間 using System.Web.SessionState;
2,在類的接口中添加IRequiresSessionState :public class ProjectInfo : IHttpHandler, IRequiresSessionState
3,引用session的方法:HttpContext.Current.Session["testSession"]。如果不加前綴會找不到。
按照以上方法就可以正常使用session。
Handler.ashx文件中,程序的入口只有ProcessRequest方法,如果要調用其他的方法,需要在ProcessRequest方法中通過參數來區分。
ajax方法:
function CheckPlanFinishDate() { var strPlanDate = $("#txtPlanFinishDate").val(); var ProPNum = $("#ProPNum").val(); var Page = 1; $.ajax({ type: "GET", contentType: "application/json;utf-8", url: 'handler/ProjectInfo.ashx?type=CheckPlanDateStatus&PlanDate=' + strPlanDate + '&ProPNum=' + ProPNum + '&Page=' + Page, success: function(msg) { if (msg == "1") { art.dialog.tips('項目計划時間改變,請重新做會簽!', 2); } }
Handler.ashx代碼:
public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["type"] == "CheckPlanDateStatus") { string strpropNum = context.Request["ProPNum"]; string strPlanDate = context.Request["PlanDate"]; string strResult = CheckPlanDate(strpropNum, strPlanDate); context.Response.Write(strResult); context.Response.End(); } }
CheckPlanDate是handle.ashx頁面的一個普通方法。
