急:如何实现先在客户端做数据检查,数据正确后才提交到服务器?
调用的语句如下:
<asp:Button ID= "ReceiveBtn " runat= "server " OnClient= "ReceiveBtn_Click " OnClientClick= "ReceiveTask() " Text= "接收任务 "/>
function ReceiveTask()
{
var status = document.all( "status ").value;
if (status.toString()!= "未接收 ")
{
alert( "请选择一条状态为[未接收]的任务! ");
return;
}
document.all( "txtTaskBaseId ").value = document.all( "TaskBaseId ").value; }
主要是想先通过本地的JS函数ReceiveTask对数据是否填写做一下简单的检查,正常的话,才提交到服务器,如果不正常则不提交。现在这样写的话,无论数据是否正确,都会提交到服务器。请问应该如何做?
------解决方案-------------------- <asp:Button ID= "ReceiveBtn " runat= "server " OnClient= "ReceiveBtn_Click " OnClientClick= "return ReceiveTask()); " Text= "接收任务 "/
function ReceiveTask()
{
var status = document.all( "status ").value;
if (status.toString()!= "未接收 ")
{
alert( "请选择一条状态为[未接收]的任务! ");
return false;
}
document.all( "txtTaskBaseId ").value = document.all( "TaskBaseId ").value;
return true;
}
不论客户端是否检验,服务器端都要检验。
------解决方案--------------------上面的方法就行,
另外也可以使用。net的验证控件,我感觉那个更方便
------解决方案--------------------好像不能,除非两个按钮
------解决方案--------------------用验证控件或者js返回false
------解决方案-------------------- <asp:Button ID= "ReceiveBtn " runat= "server " Text= "接收任务 "/>
<script type= "text/javascript ">
function ReceiveTask()
{
var status = document.all( "status ").value;
if (status.toString()!= "未接收 ")
{
alert( "请选择一条状态为[未接收]的任务! ");
return false;
}
document.all( "txtTaskBaseId ").value = document.all( "TaskBaseId ").value;
return true;
}
</script>
后台
ReceiveBtn.Attributes.Add( "onclick ", "return ReceiveTask(); ");