How to get DataAnnotations working in asp.net 4.5 WebForms?

一个人想着一个人 提交于 2019-12-05 23:06:39

It surely seems that data annotations with model binding in Asp.Net 4.5 webforms does not work 100%. As a work around I had to resort to formatting the display on each page's markup like this:

<asp:Literal runat="server" ID="dueDate" Text='<%#Eval("DueDate", "{0:yyyy/MM/dd}") %>'></asp:Literal>

Nasty, but projects have due dates and need to be completed. Lets hope Microsoft sorts this bug out in the next release.

I agree that some of the data annotation functionality in WebForms does not work like in MVC.

I've been using a DynamicField control in place of text boxes for this issue:

<asp:TextBox ID="dueDate" runat="server" text="<%#: BindItem.DueDate %>"></asp:TextBox>

becomes

<asp:DynamicControl ID="dueDate" runat="server" DataField="DueDate" Mode="Edit" />

...at least in the EditTemplate, so similarly change to Mode="ReadOnly" in the ItemTemplate. I was only able to test this with EF6, as I'm posting a few years later. It's unfortunate though that it doesn't work for the good ole' TextBox control, making working with a base of existing aspx files less joyful without editing them all.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!