Vit*_*ova 5 c# asp.net formview
我正在尝试使用FormView
并希望验证服务器端的一些数据.我正在尝试使用该属性,EnableModelValidation
但MSDN对此非常不完整.
我看到了一个aproach(但关于动态数据,而不仅仅是ObjectDataSource),你抛出一个ValidationException
它,如果你有一个它将处理ValidationSummary
.不幸的是,它与黄色错误页面崩溃而不是显示摘要.
以下是我的课程:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
和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" />
Run Code Online (Sandbox Code Playgroud)
我试图在我提到的文章中添加其他控件,但它没有做任何事情:
<asp:DynamicValidator ErrorMessage="Error" runat="server"
ControlToValidate="ObjectDataSource1" />
Run Code Online (Sandbox Code Playgroud)
我希望有人可以帮助我使用它,EnableModelValidation
因为我没有找到关于它的官方文档.
归档时间: |
|
查看次数: |
960 次 |
最近记录: |