奇怪的RequiredFieldValidator验证控件问题????

来源:百度知道 编辑:UC知道 时间:2024/07/06 19:35:13
我建立了一个Textbox控件Textbox1,将一个RequiredFieldValidator控件的ControlToValidator属性指向Textbox1,然后建立一个button1按钮,它的click方法如下:

protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid == true)
{
Response.Write(string.Format("<script>alert('验证通过!');</script>"));

}
else
{
Response.Write( string.Format("<script>alert('请在本文框中输入信息');</script>"));
}
}

这样一个简单的验证,居然也出了问题,当我不在Textbox1中输入数据而直接点击button1的时候(此时IsValid 应该是false),怎么不弹出‘请在本文框中输入信息'的对话框呢?

protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid == true)
{
Response.redirect("http://www.baidu.com");
}
}

你这样看看能不能到达百度。。。。

o(∩_∩)o...,当然错了,是你的这句有问题Page.IsValid == true
应该是
if(RequiredFieldValidator1.IsValid)
这样就没有问题了。你可以试试。

呵呵,根本不需要这样的.你直接一个提交,如果那个没有输入的话,直接就提示错误信息.