public string myrePlaceChar(string inputString, int maxLength) { StringBuilder retVal = new StringBuilder(); // 检查是否为空 if ((inputString != null) && (inputString != String.Empty)) { inputString = inputString.Trim(); //检查长度 if (inputString.Length > maxLength) inputString = inputString.Substring(0, maxLength); //替换危险字符 for (int i = 0; i < inputString.Length; i++) { switch (inputString[i]) { case '"': retVal.Append("""); break; case '<': //wangmeihong 版印次中包含这个符号 转义 retVal.Append("<"); break; case '>': retVal.Append(">"); break; default: retVal.Append(inputString[i]); break; } } retVal.Replace("'", "");// 替换单引号 转义 } return retVal.ToString(); }
public void rePlaceChar(int ctrl, Control frmPage) { //Controls frmCtrl; switch (ctrl) { //遍历界面控件 case 1: foreach (Control frmCtrl in frmPage.Controls) { if (frmCtrl is TextBox) { ((TextBox)frmCtrl).Text = myrePlaceChar(((TextBox)frmCtrl).Text, ((TextBox)frmCtrl).Text.Length); }
else if (frmCtrl.HasControls())
rePlaceChar(frmCtrl);//递归遍历 } break; default: break; } }
//使用方法,在提交前 rePlaceChar(1,this);