BPM中字段查重记录:
这也算是一个C#调用Ajax的示例吧,如果是异步加载的话async: false去掉就可以了。
需求:比如现在要录入一些信息,但是,有一个字段不能重复,BPM表都是自己生成的,所以我们需要自己在保存的时候进行验证。在保存的时候获取到要判断的字段查看是不是复核要求,数据库有没有当前字段,如果有的话,就提示错误信息,让用户重新输入。
建好表单之后,只需增加JavaScript代码:
1 <script type="text/javascript"> 2 $.MvcSheet.Ready(function () { 3 $("input[data-datafield='cardNumber']").change(function () { 4 setTimeout(function () { 5 ClearerrorMessagecardNumber(); 6 }, 1000); 7 }); 8 }); 9 10 $.MvcSheet.Validate = function () { 11 if ($.MvcSheetUI.SheetInfo.ActivityCode == "Activity2") { 12 if (this.Action == "Submit") { 13 var result = true; 14 result = CheckCardNumber(); 15 return result; 16 } 17 } 18 } 19 function CheckCardNumber() { 20 var varcardnumber = $.MvcSheetUI.GetControlValue("cardNumber"); 21 ClearerrorMessagecardNumber(); 22 var result = true; 23 var ajaxUrl = "/portal/Ajax/CardNumberRepeatHandler.ashx"; 24 $.ajax({ 25 async: false, 26 type: "post", 27 url: ajaxUrl, 28 data: { "cardNumber": varcardnumber, "ObjectID": $.MvcSheetUI.SheetInfo.BizObjectID }, 29 success: function (e) { 30 if (e == "0") { 31 var spanhtml = "<label id="errorMessagecardNumber" class="InvalidText">"; 32 var html = ""; 33 html += "证件号码重复"; 34 spanhtml += html + "</label>"; 35 $("input[data-datafield='cardNumber']").removeClass("inputMouseEnter"); 36 $("input[data-datafield='cardNumber']").addClass("inputError inputMouseOut") 37 $("input[data-datafield='cardNumber']").after(spanhtml); 38 console.log("12212"); 39 result = false; 40 } 41 } 42 }); 43 return result; 44 } 45 46 function ClearerrorMessagecardNumber() { 47 if ($("#errorMessagecardNumber").length >= 1) { 48 $("#errorMessagecardNumber").remove(); 49 $("input[data-datafield='cardNumber']").removeClass("inputError inputMouseOut"); 50 $("input[data-datafield='cardNumber']").addClass("inputMouseEnter"); 51 } 52 } 53 </script>
这里是调用Ajax强制同步完成的,不然的话程序会先进行了保存,然后才会提示错误,这样达不到需求的效果,那么,Ajax代码是这样的:
<%@ WebHandler Language="C#" Class="OThinker.H3.Portal.Ajax.CardNumberRepeatHandler" %> using System; using System.Web; using OThinker.H3.BizBus; using System.Data; using System.Collections.Generic; using System.IO; using OThinker.H3.Apps; using System.Linq; using OThinker.H3.WorkflowTemplate; using OThinker.H3.Acl; using System.Text; namespace OThinker.H3.Portal.Ajax { public class CardNumberRepeatHandler : HttpHandlerBase { public override void DoAction(HttpContext context) { context.Response.ContentType = "text/plain"; string strobjectid = this.Request.Form["ObjectID"] ?? ""; string strcardnumber = this.Request.Form["cardNumber"] ?? ""; string result = string.Empty; if (!string.IsNullOrWhiteSpace(strobjectid)) { string strsql = "select 1 from I_isRepeat where objectid!='" + strobjectid + "' and cardNumber='" + strcardnumber + "'"; DataTable dt = OThinker.H3.WorkSheet.AppUtility.Engine.EngineConfig.CommandFactory.CreateCommand().ExecuteDataTable(strsql); if (dt!=null && dt.Rows.Count>0) { result = "0"; } else { result = "1"; } } context.Response.Clear(); context.Response.Write(result); context.Response.End(); } } }