.NET - 使用 jQuery 跟 ASP.NET 泛型處理常式(ASHX) 實作 AJAX
需在原本的頁面上加入附加的功能,那做法不外乎加入 Window.Open 的方式來實作附加功能上的 UI ,不然就是使用圖層的觀念來實作在原本的頁面中,那在此介紹使用圖層的部份來達到這個功能.
那要達到此功能,所選取的方案為泛型處理常式(ASHX)做為 Server 端提供資料的 Service,那 Client 端使用 jQuery 實作 AJAX 來呼叫 Server 端存取資料~
Client 端:
Code
Snippet
- <script type="text/javascript" language="javascript">
- $(document).ready(function () {
- var winH = $(window).height();
- var winW = $(window).width();
- $('#mask').css({ 'width': winW, 'height': winH, "opacity": 0.8 }).hide();
- $("#window").css({ "top": winH / 2 - $("#window").height() / 2, "left": winW / 2 - $("#window").width() / 2 }).hide().corner("cc:#000").children("div").css({ "opacity": 0.8 });
- });
- function showWindow(apmSID, roleSID, layerSID) {
- $("#apmid").val(apmSID);
- $("#layid").val(layerSID);
- $("#roleid").val(roleSID);
- $('#mask').show(500);
- $("#window").show(800);
- $.ajax({
- type: "post",
- url: "../service/GetAllowRulesHandler.ashx",
- data: $.param({ APMSID: apmSID, RoLESID: roleSID, LAYSID: layerSID }),
- success: function (response) {
- var result = $.parseJSON(response);
- if (result.ErrorCode == 0) {
- $("#paramList > tbody > tr").remove();
- $(result.Data).each(function () {
- $("#paramList > tbody:last").append("<tr sid='" + this.SID + "'><td><img src='../images/icons/delete.png' alt='??' onclick=\"deleteData('" + this.SID + "');\" /></td><td>" + ($("#paramList > tbody > tr").size() + 1) + "</td><td>" + this.ParameterName + "</td></tr>");
- });
- return;
- }
- alert("??????????????!\n" + result.ErrorText);
- }
- });
- }
- function hideWindow() {
- $("#mask").hide(800);
- $("#window").hide(500);
- return false;
- }
- function isNonEmpty() {
- if ($(".paramTextBox[value!='']").size() == 0) {
- alert("Please input more than one parameter...");
- return false;
- }
- $.ajax({
- type: "post",
- url: "../service/SaveAllowRulesHandler.ashx",
- data: $.param({ APMSID: $("#apmid").val(), ROLESID: $("#roleid").val(), LAYSID: $("#layid").val(), PARAMNAME: $(".paramTextBox").val() }),
- success: function (response) {
- var result = $.parseJSON(response);
- if (result.ErrorCode == 0) {
- var role = result.Data;
- $("#paramList > tbody:last").append("<tr sid='" + role.SID + "'><td><img src='../images/icons/delete.png' alt='??' onclick=\"deleteData('" + role.SID + "');\" /></td><td>" + ($("#paramList > tbody > tr").size() + 1) + "</td><td>" + role.ParameterName + "</td></tr>");
- $(".paramTextBox").val("");
- return;
- }
- alert("??????????????!\n" + result.ErrorText);
- }
- });
- return false;
- }
- function deleteData(sid) {
- $.ajax({
- type: "post",
- url: "../service/DelAllowRulesHandler.ashx",
- data: $.param({ SID: sid }),
- success: function (response) {
- var result = $.parseJSON(response);
- if (result.ErrorCode != 0) {
- alert("??????????????!\n" + result.ErrorText);
- return;
- }
- $("#paramList > tbody > tr[sid=" + sid + "]").remove();
- }
- });
- }
- </script>
Server 端:
Code
Snippet
- <%@ WebHandler Language="C#" Class="GetAllowRulesHandler" %>
- using System;
- using System.Web;
- using System.Web.SessionState;
- using System.Web.Script.Serialization;
- using Com.Trg.R2R.Model;
- using Com.Trg.R2R.UCO;
- public class GetAllowRulesHandler : IHttpHandler, IRequiresSessionState {
- public void ProcessRequest (HttpContext context) {
- string result = string.Empty;
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- try
- {
- AccessRule rule = new AccessRule();
- AllowRuleUCO uco = new AllowRuleUCO();
- rule.ApmSID = context.Request.Form["APMSID"];
- rule.RolSID = context.Request.Form["ROLESID"];
- rule.LaySID = context.Request.Form["LAYSID"];
- AllowRule[] rules = uco.GetRules(rule);
- result = serializer.Serialize(new { ErrorCode = 0, ErrorText = string.Empty, Data = rules });
- }
- catch (Exception ex)
- {
- result = serializer.Serialize(new { ErrorCode = 99999, ErrorText = ex.Message });
- }
- context.Response.ContentType = "text/plain";
- context.Response.Write(result);
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
- }
留言
張貼留言