.NET - 使用 jQuery 跟 ASP.NET 泛型處理常式(ASHX) 實作 AJAX


    需在原本的頁面上加入附加的功能,那做法不外乎加入 Window.Open 的方式來實作附加功能上的 UI ,不然就是使用圖層的觀念來實作在原本的頁面中,那在此介紹使用圖層的部份來達到這個功能.

那要達到此功能,所選取的方案為泛型處理常式(ASHX)做為 Server 端提供資料的 Service,那 Client 端使用 jQuery 實作 AJAX 來呼叫 Server 端存取資料~

Client 端:

Code Snippet
  1. <script type="text/javascript" language="javascript">
  2. $(document).ready(function () {
  3. var winH = $(window).height();
  4. var winW = $(window).width();
  5. $('#mask').css({ 'width': winW, 'height': winH, "opacity": 0.8 }).hide();
  6. $("#window").css({ "top": winH / 2 - $("#window").height() / 2, "left": winW / 2 - $("#window").width() / 2 }).hide().corner("cc:#000").children("div").css({ "opacity": 0.8 });
  7. });
  8. function showWindow(apmSID, roleSID, layerSID) {
  9. $("#apmid").val(apmSID);
  10. $("#layid").val(layerSID);
  11. $("#roleid").val(roleSID);
  12. $('#mask').show(500);
  13. $("#window").show(800);
  14. $.ajax({
  15. type: "post",
  16. url: "../service/GetAllowRulesHandler.ashx",
  17. data: $.param({ APMSID: apmSID, RoLESID: roleSID, LAYSID: layerSID }),
  18. success: function (response) {
  19. var result = $.parseJSON(response);
  20. if (result.ErrorCode == 0) {
  21. $("#paramList > tbody > tr").remove();
  22. $(result.Data).each(function () {
  23. $("#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>");
  24. });
  25. return;
  26. }
  27. alert("??????????????!\n" + result.ErrorText);
  28. }
  29. });
  30. }
  31. function hideWindow() {
  32. $("#mask").hide(800);
  33. $("#window").hide(500);
  34. return false;
  35. }
  36. function isNonEmpty() {
  37. if ($(".paramTextBox[value!='']").size() == 0) {
  38. alert("Please input more than one parameter...");
  39. return false;
  40. }
  41. $.ajax({
  42. type: "post",
  43. url: "../service/SaveAllowRulesHandler.ashx",
  44. data: $.param({ APMSID: $("#apmid").val(), ROLESID: $("#roleid").val(), LAYSID: $("#layid").val(), PARAMNAME: $(".paramTextBox").val() }),
  45. success: function (response) {
  46. var result = $.parseJSON(response);
  47. if (result.ErrorCode == 0) {
  48. var role = result.Data;
  49. $("#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>");
  50. $(".paramTextBox").val("");
  51. return;
  52. }
  53. alert("??????????????!\n" + result.ErrorText);
  54. }
  55. });
  56. return false;
  57. }
  58. function deleteData(sid) {
  59. $.ajax({
  60. type: "post",
  61. url: "../service/DelAllowRulesHandler.ashx",
  62. data: $.param({ SID: sid }),
  63. success: function (response) {
  64. var result = $.parseJSON(response);
  65. if (result.ErrorCode != 0) {
  66. alert("??????????????!\n" + result.ErrorText);
  67. return;
  68. }
  69. $("#paramList > tbody > tr[sid=" + sid + "]").remove();
  70. }
  71. });
  72. }
  73. </script>


Server 端:
Code Snippet
  1. <%@ WebHandler Language="C#" Class="GetAllowRulesHandler" %>
  2. using System;
  3. using System.Web;
  4. using System.Web.SessionState;
  5. using System.Web.Script.Serialization;
  6. using Com.Trg.R2R.Model;
  7. using Com.Trg.R2R.UCO;
  8. public class GetAllowRulesHandler : IHttpHandler, IRequiresSessionState {
  9. public void ProcessRequest (HttpContext context) {
  10. string result = string.Empty;
  11. JavaScriptSerializer serializer = new JavaScriptSerializer();
  12. try
  13. {
  14. AccessRule rule = new AccessRule();
  15. AllowRuleUCO uco = new AllowRuleUCO();
  16. rule.ApmSID = context.Request.Form["APMSID"];
  17. rule.RolSID = context.Request.Form["ROLESID"];
  18. rule.LaySID = context.Request.Form["LAYSID"];
  19. AllowRule[] rules = uco.GetRules(rule);
  20. result = serializer.Serialize(new { ErrorCode = 0, ErrorText = string.Empty, Data = rules });
  21. }
  22. catch (Exception ex)
  23. {
  24. result = serializer.Serialize(new { ErrorCode = 99999, ErrorText = ex.Message });
  25. }
  26. context.Response.ContentType = "text/plain";
  27. context.Response.Write(result);
  28. }
  29. public bool IsReusable {
  30. get {
  31. return false;
  32. }
  33. }
  34. }

留言

這個網誌中的熱門文章

WPF - 深入 Style

C# – M$ Chart Control 自定 ToolTip 的顯示

Vue.js - 基礎介紹教學