首先,我先Web控件新建了一张表单,包括用户名和密码,注意这里不是HTML控件,
然后用两个判断是否为空的验证控件分别对这两个文本框进行验证是否未填,
我要问的是:
我这种情况,只要一点击提交按钮,就直接会提交表单,在提交表单之后才会去验证是否未填,我是想:点击“提交”之后先让它验证,验证全部通过后,再执行验证通过后的表单。就是这个意思:if (验证控件有一个不能通过)
{
不能提交
else if (验证控件全部通过)
{
全部填之后,再提交表单,执行相应操作
}
}
然后用两个判断是否为空的验证控件分别对这两个文本框进行验证是否未填,
我要问的是:
我这种情况,只要一点击提交按钮,就直接会提交表单,在提交表单之后才会去验证是否未填,我是想:点击“提交”之后先让它验证,验证全部通过后,再执行验证通过后的表单。就是这个意思:if (验证控件有一个不能通过)
{
不能提交
else if (验证控件全部通过)
{
全部填之后,再提交表单,执行相应操作
}
}
验证控件本身就是客户端验证..是你理解错了..
同意。
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 64px" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 104px" runat="server"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 320px; POSITION: absolute; TOP: 160px" runat="server"Text="登录"></asp:Button>
<asp:Button id="Button2" style="Z-INDEX: 104; LEFT: 376px; POSITION: absolute; TOP: 160px" runat="server"Text="取消" CausesValidation="False"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 192px; POSITION: absolute; TOP: 64px" runat="server">用户名</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 200px; POSITION: absolute; TOP: 112px" runat="server">密码</asp:Label>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 107; LEFT: 424px; POSITION: absolute; TOP: 64px" runat="server" ErrorMessage="用户名不能为空" ControlToValidate="TextBox1">*</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator id="RequiredFieldValidator2" style="Z-INDEX: 108; LEFT: 424px; POSITION: absolute; TOP: 112px" runat="server" ErrorMessage="密码不能为空" ControlToValidate="TextBox2">*</asp:RequiredFieldValidator>
<asp:ValidationSummary id="ValidationSummary1" style="Z-INDEX: 109; LEFT: 208px; POSITION: absolute; TOP: 248px" runat="server"></asp:ValidationSummary>
</form>
<html>
<head>
<script>
function checkform()
{var username=document.login.username.value;
var password=document.login.password.value;
if(username.length==0)
{alert("请输入用户名!");
this.login.username.focus();
return false;
}else if(password.length==0)
{alert("请输入您的密码!");
this.login.password.focus();
return false;
}
return true;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>标题</title>
</head>
<body>
<form name="login" method="post" action="dologin.jsp" target=_self onsubmit="return checkform();">……………………<td><input type="text" name="username"></td>
<td><input type="password" name="password"></td>……………………</body>
</html>这是一个登录页面验证输入的用户名和密码不能为空的部分代码。
myControl.Attribute.Add("onchange","alert('呵呵')");
欢迎大家加入!
if (!IsPostPack){
}
来判断页面有没有提交,提交就插入数据库,可是,若用户未填写完整这张表单,点提交,虽然验证控件是产生验证效果了,但好像我写在这句里面的SQL插入语句也生效了?
{
response.write("<script>window.alert('..不能为空')</script>")
return;
}
设置验证控件的EnableClientScript=true 就行了!!!
设置验证控件的EnableClientScript=true 就行了!!!
设置验证控件的EnableClientScript=true 就行了!!!
if (!IsPostPack){
}
=================================================================这句话有些矛盾..IsPostPack加在页面Page_Load中..判断是否回传,避免在回传的时候再次操作数据..而你的提交按钮..不需要添加IsPostPack..如果有疑问..请在实际操作后再发表问题..实践后就可以看出问题所在了..对于插入数据的工作在Button_Click中加入代码就好了.