说一个今早我用ASP.NET写东西的时候遇到的小bug

Requiredfieldvalidator

Requiredfieldvalidator控件,我在用它写表单验证提交后台的时候,一跑起来,美滋滋的抛出了一堆错。。。let’s see~

“Requiredfieldvalidator用于验证TextBox是否有输入”
“Requiredfieldvalidator用于验证TextBox是否有输入”

So the Thrown error(所以抛出错误):

“Requiredfieldvalidator用于验证TextBox是否有输入”
“Requiredfieldvalidator用于验证TextBox是否有输入”

咱直接说解决方案,字多了太无聊

解决方案一

1
2
3
在aspx页面的Page_Load方法中添加代码:

UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;

解决方案二

1
2

把文件AspNet.ScriptManager.jQuery.dll添加到项目的引用中。虽然可以消除错误,但是会导致验证控件不起作用。

解决方案三

1
2
3
4
5
6
7
修改Web.config文件如下:
<configuration>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode"value="None" />
</appSettings>
...
</configuration>

解决方案四

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
修改全局应用程序类Global.asax
1. 添加程序集引用:using System.Web.UI;

2. 在类Global的Application_Start方法中添加代码:
ScriptManager.ScriptResourceMapping.AddDefinition("jquery",
new ScriptResourceDefinition
{
Path = "~/scripts/jquery-1.7.2.min.js",
DebugPath = "~/scripts/jquery-1.7.2.min.js",
CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.min.js",
CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.js"
}
);

3. 在解决方案项目目录下新建scripts文件夹,放入jquery文件

举例使用解决方案一

“在使用到Requiredfieldvalidator控件的aspx页面的Page_Load加入如图代码”
“在使用到Requiredfieldvalidator控件的aspx页面的Page_Load加入如图代码”

然后美滋滋跑起来~

“页面就活活跑起来了”
“页面就活活跑起来了”

有个朋友问到了文字转拼音的方法,我get到了

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
public static string ChineseToFullPinYin(string chinese)
{
// 匹配中文字符
Regex regex = new Regex("^[\u4e00-\u9fa5]$");
byte[] array = new byte[2];
string pyString = "";
int chrAsc = 0;
int i1 = 0;
int i2 = 0;
char[] noWChar = chinese.ToCharArray();

for (int j = 0; j < noWChar.Length; j++)
{
// 中文字符
if (regex.IsMatch(noWChar[j].ToString()))
{
array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
i1 = (short)(array[0]);
i2 = (short)(array[1]);
chrAsc = i1 * 256 + i2 - 65536;
if (chrAsc > 0 && chrAsc < 160)
{
pyString += noWChar[j];
}
else
{
// 修正部分文字
if (chrAsc == -9254)
pyString += "Zhen";
else if (chrAsc == -12080)
{
pyString += "Hang";
}
else
{
for (int i = (pyValue.Length - 1); i >= 0; i--)
{
if (pyValue[i] <= chrAsc)
{
pyString += pyName[i];
break;
}
}
}
}
}
// 非中文字符
else
{
pyString += noWChar[j].ToString();
}
}
return pyString;
}