需要时如何突出显示文本框边框为红色? [英] How do I highlight a textbox border red when it is required?
问题描述
如果出现验证错误,我在必填字段验证器控件上使用什么属性来使文本框变为红色?
What property do I use on the required field validator control to make the textbox red if there is a validation error?
这是我的代码:
<asp:Label ID="lblFirstName" runat="server" AssociatedControlID="txtFirstName" Text="First Name:" CssClass="reg-labels" />
<br />
<asp:TextBox ID="txtFirstName" runat="server" CausesValidation="true" MaxLength="60" CssClass="standard_width"/>
<asp:RequiredFieldValidator ControlToValidate="txtFirstName" runat="server" ID="valFirstName" ValidationGroup="grpRegistration" ErrorMessage="First Name is required." Text="*" />
推荐答案
ASP.Net Web 表单在内部使用位于 aspnet_client{0}{1}
文件夹的 Javascript 框架来处理验证等.它们基本上是从属性ClientScriptsLocation
ASP.Net web forms internally uses a Javascript frameworka located at aspnet_client{0}{1}
folder to handle the validation, etc. They are basically determined from the property ClientScriptsLocation
尝试覆盖默认框架函数,将其保留在您的页面中,包括设置 control_to_validate 颜色的附加行
Try overriding the default framework function by keeping it in your page includes additional line to set the control_to_validate color
document.getElmentById(val.controltovalidate).style.border='1px solid red';
<asp:TextBox ID="txtFirstName" runat="server" CausesValidation="true" MaxLength="60"
CssClass="standard_width" />
<asp:RequiredFieldValidator ControlToValidate="txtFirstName" runat="server" ID="valFirstName" ValidationGroup="grpRegistration" ErrorMessage="First Name is required." Text="*" />
<asp:Button Text="Super" ID="btnSubmit" CausesValidation="true" runat="server" />
JS
<script type="text/javascript">
function ValidatorUpdateDisplay(val) {
if (typeof (val.display) == "string") {
if (val.display == "None") {
return;
}
if (val.display == "Dynamic") {
val.style.display = val.isvalid ? "none" : "inline";
return;
}
}
val.style.visibility = val.isvalid ? "hidden" : "visible";
if (val.isvalid) {
document.getElementById(val.controltovalidate).style.border = '1px solid #333';
}
else {
document.getElementById(val.controltovalidate).style.border = '1px solid red';
}
}
</script>
这篇关于需要时如何突出显示文本框边框为红色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!