I'm trying to use FormView
and want to validate some data in the server side. I'm trying to use the property EnableModelValidation
but MSDN is very incomplete for this.
I saw an aproach (but about Dynamic Data instead of just ObjectDataSource) where you throw a ValidationException
and it will handle if you have a ValidationSummary
. Unfortunately it crashes with the yellow error page instead of showing the summary.
Following is my class:
namespace FormViewTest
{
[DataObject]
public class Person
{
private int age;
[DataObjectField(false)]
public int Age
{
get { return age; }
set
{
if (value < 0)
{
throw new ValidationException("Invalid age");
}
age = value;
}
}
public void Insert(Person p)
{ }
public Person Get()
{
return new Person();
}
}
}
and the aspx:
<asp:FormView runat="server"
DataSourceID="ObjectDataSource1"
DefaultMode="Insert"
EnableModelValidation="true">
<InsertItemTemplate>
Age:
<asp:TextBox ID="AgeTextBox" runat="server"
Text='<%# Bind("Age") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CommandName="Insert"
Text="Insert" />
</InsertItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="FormViewTest.Person"
InsertMethod="Insert" SelectMethod="Get"
TypeName="FormViewTest.Person"></asp:ObjectDataSource>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
I've tried to add other control as said in the article I mention but it did nothing:
<asp:DynamicValidator ErrorMessage="Error" runat="server"
ControlToValidate="ObjectDataSource1" />
I hope someone can help me to use this EnableModelValidation
since there is no official documentation I could find about it.
Enabling that property is not going to do anything unless you are using DynamicData version of FormView See this for a better explanation on using ASP.NET Dynamic Data Scaffolding along with some useful links
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With