在 ASP.NET 中,DetailsView 控件下,可以绑定控件,比如:
<asp:TextBox ID="_name" runat="server" Text='<%#Eval("Name")%>'>
后端可用 _detailsView.FindControl("_name").Text; 进行取值。
但是 CheckBox 则不可:
<asp:CheckBox ID="_published" runat="server" Checked='<%#Eval("Published")%>'>
如上:不论 Published 是否为 nullable,当 DetailsView 为 Insert 模式,就会出错:未将对象引用设置到对象的实例。
解决办法
将 Eval 换成 Bind。