How to install Page.IsValid in ASP.Net

When a property of the IsValid class is read-only, how can I set it using my own validation method?

So far, all I have managed to do is set this property by calling Page.Validate() .

How can I write my own functionality that will change the IsValid property IsValid like Page.Validate() ?

+4
source share
5 answers

You do not install IsValid directly; instead, you call the Validate () method of the Page object. If you have your own verification methods, you need to use the CustomValidator object and set this function in the server-side verification property.

  <asp:CustomValidator ID="YourValidator" runat="server" SetFocusOnError="true" ControlToValidate="YourControl" ClientValidationFunction="YOUR_JAVASCRIPT_FUNCTION" OnServerValidate="YOUR_SERVER_VALIDATION_FUNCTION" Text="*" /> 
+10
source

The IsValid property is read-only because it is intended for use with servers and client-side validators, such as RequiredFieldValidator and RegularExpressionValidator . This is read-only because you cannot force the page to be valid programmatically. "Valid" in this context means that all validators on the page are evaluated as true.

+2
source

I know this is old, but I needed to do something similar, basically forcing the IsValid property by mistake (don't ask why). Here is what I basically did (what you see here is my proof of concept):

Added this to the .aspx page:

 <asp:TextBox ID="txtDummy" runat="server" Visible="false" /> <asp:RangeValidator ID="rvDummy" ControlToValidate="txtDummy" runat="server" MinimumValue="1" MaximumValue="2" /> 

And then I added this to the code behind:

 bool makeMyPageInvalid = true; if (makeMyPageInvalid) txtDummy.Text = "0"; Page.Validate(); if (Page.IsValid) ScriptManager.RegisterStartupScript(Page, Page.GetType(), "test", "alert('valid');", true); else ScriptManager.RegisterStartupScript(Page, Page.GetType(), "test", "alert('not valid');", true); 

You can see that this only allows you to force a page to check for an invalid state. You can use any validator or reason to establish this. Hope this helps someone!

+2
source

If you like to use some kind of JavaScript, you can do it on the client side by changing the Page_IsValid variable as follows:

 function pageLoad() { Page_IsValid = false; } 

I only use this if someone presses the submit button without entering data. Then I can display the warning as follows:

 function valid() { if (!Page_IsValid) { alert("Some Questions Remain Unanswered and are Marked with a Red Asterisc. ( * )"); } 

(at first I thought: β€œWho would have submitted a form without data”, but sooner than later, I realized that this was happening)

+1
source

This is a really old question, but it appeared in the search, so I decided to add my answer to it. First create an extension method in one of your helper classes.

 public static IEnumerable<T> GetAllControlsOfType<T>(this Control parent) where T : Control { var result = new List<T>(); foreach (Control control in parent.Controls) { if (control is T) { result.Add((T)control); } if (control.HasControls()) { result.AddRange(control.GetAllControlsOfType<T>()); } } return result; } 

Now, in your code for the file, loop over each validator on the page that is not checked.

 foreach (var validator in Page.GetAllControlsOfType<BaseValidator>().Where(w => !w.IsValid)) { validator.IsValid = true; } 
0
source

Source: https://habr.com/ru/post/1286552/


All Articles