Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this with a native components from ASP.Net:</p> <p>In your ASP page:</p> <pre><code>&lt;asp:TextBox ID="TextBox1" runat="server"/&gt; &lt;asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="ValidateTextBox1" ForeColor="Red" SetFocusOnError="true"&gt;&lt;/asp:CustomValidator&gt; </code></pre> <p>In the code behind:</p> <pre><code> protected void ValidateTextBox1(object source, ServerValidateEventArgs args) { if (TextBox1.Text == string.Empty) { args.IsValid = false; TextBox1.Style.Add("border", "solid 1px red"); CustomValidator1.Text = "required"; } else args.IsValid = true; } </code></pre> <p><strong>PS.: On press button, the RequiredFieldValidator is activated before the CustomValidator. If there are any on the same page, you have to turn it off or replace it with CustomValidator.</strong> </p> <p>OR</p> <p>You can do with a CSS and Javascript.</p> <p>CSS Class:</p> <pre><code>&lt;style type="text/css"&gt; body { font-family:Arial; font-size:10pt; } .ErrorControl { background-color: #FBE3E4; border: solid 1px Red; } &lt;/style&gt; </code></pre> <p>JavaScript function:</p> <pre><code>&lt;script type="text/javascript"&gt; function WebForm_OnSubmit() { if (typeof (ValidatorOnSubmit) == "function" &amp;&amp; ValidatorOnSubmit() == false) { for (var i in Page_Validators) { try { var control = document.getElementById(Page_Validators[i].controltovalidate); if (!Page_Validators[i].isvalid) { control.className = "ErrorControl"; } else { control.className = ""; } } catch (e) { } } return false; } return true; } &lt;/script&gt; </code></pre> <p>Now, the validators ASP.net:</p> <pre><code>&lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="Required"&gt;&lt;/asp:RequiredFieldValidator&gt; </code></pre> <p>OR</p> <pre><code>&lt;asp:TextBox ID="TextBox2" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Required" ControlToValidate="TextBox2" ValidateEmptyText="true" ClientValidationFunction="Validate"&gt;&lt;/asp:CustomValidator&gt; &lt;script type="text/javascript"&gt; function Validate(sender, args) { if (document.getElementById(sender.controltovalidate).value != "") { args.IsValid = true; } else { args.IsValid = false; } } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload