服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - Asp.Net类型转换类(通用类)代码分享

Asp.Net类型转换类(通用类)代码分享

2019-12-30 13:08mrr ASP.NET教程

本文给大家分享两段代码,一段代码关于asp.net类型转换通用类的代码,第二段关于asp.net 页面数据验证通用类的实例代码,代码简单易懂,非常不错,感兴趣的朋友参考下吧

废话不多说了,直接给大家贴代码了,具体代码如下所述:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/// <summary>
 /// 类型转换类
 /// 处理数据库获取字段为空的情况
 /// </summary>
 public static class DBConvert
 {
  #region------------------ToInt32类型转换------------------
  /// <summary>
  /// 读取数据库中字符串并转换成Int32
  /// 为空时返回0
  /// </summary>
  /// <param name="obj">object类型的值</param>
  /// <returns>Int32类型</returns>
  public static int ToInt32(object obj)
  {
   int result = 0;
   if (IsInt(Convert.ToString(obj)))
   {
    result = Convert.ToInt32(obj);
   }
   else if (obj != null && obj is Enum) //处理非null值类型时(或者枚举)
   {
    result = ((IConvertible)obj).ToInt32(null);
   }
   return result;
  }
  /// <summary>
  /// 读取数据库中字符串并转换成Int32
  /// 为空时返回0
  /// </summary>
  /// <param name="str">string类型的值</param>
  /// <returns>Int32类型</returns>
  public static int ToInt32(string str)
  {
   int result = 0;
   if (IsInt(str))
   {
    result = Convert.ToInt32(str);
   }
   return result;
  }
  /// <summary>
  /// 判断一个字符串是否属于Int类型
  /// 如果是的返回true,如果不是返回false
  /// </summary>
  /// <param name="str">string类型的值</param>
  /// <returns>true:是Int的字符串(即可以转换成Int类型),false:不是Int类型的字符串</returns>
  public static bool IsInt(string str)
  {
   bool result = false;
   if (str != "" && str!=null)
   {
    Regex reg = new Regex("^[0-9]*$");
    if (reg.IsMatch(str))
    {
     result = true;
    }
   }
   return result;
  }
  #endregion
  #region------------------ToString类型转换------------------
  /// <summary>
  /// 读取数据库中字符串并转换成string
  /// </summary>
  /// <param name="obj">object类型的值</param>
  /// <returns>string类型</returns>
  public static string ToString(object obj)
  {
   string result = "";
   if (obj != null)
   {
    result = Convert.ToString(obj);
   }
   return result;
  }
  #endregion
  #region------------------ToDouble类型转换------------------
  /// <summary>
  /// 判断一个字符串是否属于Double类型(包括负浮点型)
  /// 如果是的返回true,如果不是返回false
  /// </summary>
  /// <param name="str">string类型的值</param>
  /// <returns>true:是Double的字符串(即可以转换成Double类型),false:不是Double类型的字符串</returns>
  public static bool IsDouble(string str)
  {
   bool result = false;
   if (str != "" && str != null)
   {
    Regex reg = new Regex(@"^(-?\d+)(\.\d+)?$");
    if (reg.IsMatch(str))
    {
     result = true;
    }
   }
   return result;
  }
  /// <summary>
  /// 读取数据库中字符串并转换成Int32
  /// 为空时返回0
  /// </summary>
  /// <param name="obj">object类型的值</param>
  /// <returns>Int32类型</returns>
  public static double ToDouble(object obj)
  {
   double result = 0.0;
   if (IsDouble(Convert.ToString(obj)))
   {
    result = Convert.ToDouble(obj);
   }
   else if (obj != null && obj is Enum) //处理枚举
   {
    result = ((IConvertible)obj).ToDouble(null);
   }
   return result;
  }
  /// <summary>
  /// 读取数据库中字符串并转换成Int32
  /// 为空时返回0
  /// </summary>
  /// <param name="str">string类型的值</param>
  /// <returns>Int32类型</returns>
  public static double ToDouble(string str)
  {
   double result = 0.0;
   if (IsDouble(str))
   {
    result = Convert.ToDouble(str);
   }
   return result;
  }
  #endregion
  #region------------------ToDateTime类型转换------------------
  /// <summary>
  /// 判断时间格式是否是时间类型
  /// 如23:10
  /// </summary>
  /// <param name="str">要判断的字符串</param>
  /// <returns>true:是时间类型的字符串(即可以转换成时间类型),false:不是时间类型的字符串</returns>
  public static bool isDateTime(string str)
  {
   bool result = false;
   if (str != "" && str != null)
   {
    Regex reg = new Regex("(([01]\\d)|(2[0-3])):[0-5]\\d");
    if (reg.IsMatch(str))
    {
     result = true;
    }
   }
   return result;
  }
  #endregion
 }
}
//"^\d+(\.\d+)?$"  //非负浮点数(正浮点数 + 0)
//"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数
//"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮点数(负浮点数 + 0)
//"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数
//"^(-?\d+)(\.\d+)?$"  //浮点数

好了,Asp.Net类型转换类(通用类)代码分享到此结束。

下面看下ASP.NET页面数据验证通用类的实例代码。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
public class PageValidate
{
private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
private static Regex RegNumber = new Regex("^[0-9]+$");
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
public PageValidate()
{
}
//数字字符串检查#region 数字字符串检查
public static bool IsPhone(string inputData)
{
Match m = RegPhone.Match(inputData);
return m.Success;
}
/**//// <summary>
/// 检查Request查询字符串的键值,是否是数字,最大长度限制
/// </summary>
/// <param name="req">Request</param>
/// <param name="inputKey">Request的键值</param>
/// <param name="maxLen">最大长度</param>
/// <returns>返回Request查询字符串</returns>
public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
{
string retVal = string.Empty;
if(inputKey != null && inputKey != string.Empty)
{
retVal = req.QueryString[inputKey];
if(null == retVal)
retVal = req.Form[inputKey];
if(null != retVal)
{
retVal = SqlText(retVal, maxLen);
if(!IsNumber(retVal))
retVal = string.Empty;
}
}
if(retVal == null)
retVal = string.Empty;
return retVal;
}
/**//// <summary>
/// 是否数字字符串
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumber(string inputData)
{
Match m = RegNumber.Match(inputData);
return m.Success;
}
/**//// <summary>
/// 是否数字字符串 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumberSign(string inputData)
{
Match m = RegNumberSign.Match(inputData);
return m.Success;
}
/**//// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimal(string inputData)
{
Match m = RegDecimal.Match(inputData);
return m.Success;
}
/**//// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimalSign(string inputData)
{
Match m = RegDecimalSign.Match(inputData);
return m.Success;
}
#endregion
//中文检测#region 中文检测
/**//// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
}
#endregion
//邮件地址#region 邮件地址
/**//// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsEmail(string inputData)
{
Match m = RegEmail.Match(inputData);
return m.Success;
}
#endregion
//其他#region 其他
/**//// <summary>
/// 检查字符串最大长度,返回指定长度的串
/// </summary>
/// <param name="sqlInput">输入字符串</param>
/// <param name="maxLength">最大长度</param>
/// <returns></returns>
public static string SqlText(string sqlInput, int maxLength)
{
if(sqlInput != null && sqlInput != string.Empty)
{
sqlInput = sqlInput.Trim();
if(sqlInput.Length > maxLength)//按最大长度截取字符串
sqlInput = sqlInput.Substring(0, maxLength);
}
return sqlInput;
}
/**//// <summary>
/// 字符串编码
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static string HtmlEncode(string inputData)
{
return HttpUtility.HtmlEncode(inputData);
}
/**//// <summary>
/// 设置Label显示Encode的字符串
/// </summary>
/// <param name="lbl"></param>
/// <param name="txtInput"></param>
public static void SetLabel(Label lbl, string txtInput)
{
lbl.Text = HtmlEncode(txtInput);
}
public static void SetLabel(Label lbl, object inputObj)
{
SetLabel(lbl, inputObj.ToString());
}
//字符串清理
public static string InputText(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 '<':
retVal.Append("<");
break;
case '>':
retVal.Append(">");
break;
default:
retVal.Append(inputString[i]);
break;
}
}
retVal.Replace("'", " ");// 替换单引号
}
return retVal.ToString();
}
/**//// <summary>
/// 转换成 HTML code
/// </summary>
/// <param name="str">string</param>
/// <returns>string</returns>
public static string Encode(string str)
{
str = str.Replace("&","&");
str = str.Replace("'","''");
str = str.Replace("\"",""");
str = str.Replace(" "," ");
str = str.Replace("<","<");
str = str.Replace(">",">");
str = str.Replace("\n","<br>");
return str;
}
/**//// <summary>
///解析html成 普通文本
/// </summary>
/// <param name="str">string</param>
/// <returns>string</returns>
public static string Decode(string str)
{
str = str.Replace("<br>","\n");
str = str.Replace(">",">");
str = str.Replace("<","<");
str = str.Replace(" "," ");
str = str.Replace(""","\"");
return str;
}
public static string SqlTextClear(string sqlText)
{
if (sqlText == null)
{
return null;
}
if (sqlText == "")
{
return "";
}
sqlText = sqlText.Replace(",", "");//去除,
sqlText = sqlText.Replace("<", "");//去除<
sqlText = sqlText.Replace(">", "");//去除>
sqlText = sqlText.Replace("--", "");//去除--
sqlText = sqlText.Replace("'", "");//去除'
sqlText = sqlText.Replace("\"", "");//去除"
sqlText = sqlText.Replace("=", "");//去除=
sqlText = sqlText.Replace("%", "");//去除%
sqlText = sqlText.Replace(" ", "");//去除空格
return sqlText;
}
#endregion
//是否由特定字符组成#region 是否由特定字符组成
public static bool isContainSameChar(string strInput)
{
string charInput = string.Empty;
if (!string.IsNullOrEmpty(strInput))
{
charInput = strInput.Substring(0, 1);
}
return isContainSameChar(strInput, charInput, strInput.Length);
}
public static bool isContainSameChar(string strInput, string charInput, int lenInput)
{
if (string.IsNullOrEmpty(charInput))
{
return false;
}
else
{
Regex RegNumber = new Regex(string.Format("^([{0}])+$", charInput));
//Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput));
Match m = RegNumber.Match(strInput);
return m.Success;
}
}
#endregion
//检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查#region 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
/**//// <summary>
/// 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
/// </summary>
public static bool isContainSpecChar(string strInput)
{
string[] list = new string[] { "123456", "654321" };
bool result = new bool();
for (int i = 0; i < list.Length; i++)
{
if (strInput == list[i])
{
result = true;
break;
}
}
return result;
}
#endregion
}

延伸 · 阅读

精彩推荐