offline页面开发常用方法及页面控件验证,对一些CheckBoxList操作进行封装,新人可以直接使用该代码。
1、返回上一页网址
/// <summary> /// Description: /// 返回上一页网址 /// Author : 付义方 /// Create Date: 2014-02-09 /// </summary> /// <returns>跳转Url</returns> public string ToRedirect() { //没有来路地址 string RedirectUrl = "WebIndex.aspx"; if (Request.UrlReferrer != null) { //如果能获取来路地址 RedirectUrl = Request.UrlReferrer.ToString(); } return RedirectUrl; }
2、根据字符串,自动勾选CheckBoxList对应项
/// <summary> /// Description: /// 根据字符串,自动勾选CheckBoxList对应项 /// Author : 付义方 /// Create Date: 2014-02-09 /// </summary> /// <param name="str">字符串,格式要求为“A,B,C”</param> /// <param name="checkBoxList">CheckBoxList控件</param> public void FillCheckBoxList(string str, CheckBoxList checkBoxList) { string[] items = str.Split(','); //遍历items foreach (string item in items) { //如果值相等,则选中该项 foreach (ListItem listItem in checkBoxList.Items) { if (item == listItem.Value) { listItem.Selected = true; } else { continue; } } } }
3、得到CheckBoxList选中值字符串
/// <summary> /// Description: /// 得到CheckBoxList值字符串 /// Author : 付义方 /// Create Date: 2014-02-09 /// </summary> /// <returns>字符串,格式为“A,B,C”</returns> public string GetChekVal(CheckBoxList _CheckBoxList) { string ChekVal = string.Empty; for (int i = 0; i < _CheckBoxList.Items.Count; i++) { if (_CheckBoxList.Items[i].Selected == true) { ChekVal += _CheckBoxList.Items[i].Value + ","; } } ChekVal = ChekVal.TrimEnd(','); return ChekVal; }
4、Jquery CheckBoxList选中值验证
//验证CheckBoxList必选 var str = 0; $("input[id^=<%=ChkToRangeList.ClientID %>]").each(function (i, val) { if ($(i)[0].type == "checkbox") { if ($(i)[0].checked) { str += 1; } } }); if (str == 0) { alert("请选择显示设备!"); return false; } //验证RadioButtonList必选 var str = 0; $("input[id^=<%=RdisFilterList.ClientID %>]").each(function (i, val) { if ($(i)[0].type == "radio") { if ($(i)[0].checked) { str += 1; } } }); if (str == 0) { alert("请选是否过滤!"); return false; }
5、验证网址
//验证网址 function checkUrl(url) { var strRegex = new RegExp("((^http)|(^https)|(^ftp)):\/\/(\\w)+\.(\\w)+"); var re = new RegExp(strRegex); if (re.exec(url)) { return true; } else { return false; } }
6、验证正整数字
//验证正整数字 function validateNumber(obj) { var reg = /^\d+$/; if (obj.length == 0) { return true; } if (!reg.test(obj)) { return false; } else { return true; } }
7、得到Repeater全选值
/// <summary> /// 得到Repeater选择值 GetAllCheckBoxList /// </summary> /// <param name="_Repeater"></param> /// <returns></returns> private List<string> GetAllCheckBoxList(Repeater _Repeater) { List<string> list = new List<string>(); for (int i = 0; i < _Repeater.Items.Count; i++) { CheckBox CB = (CheckBox)_Repeater.Items[i].FindControl("ckbIndex"); HiddenField _HiddenFieldVal = (HiddenField)_Repeater.Items[i].FindControl("hf_JobinfotaskId"); if (CB != null && _HiddenFieldVal != null) { if (CB.Checked == true) //判断该复选框是否被选中 { list.Add(_HiddenFieldVal.Value); } } } return list; }