[C#.NET][ASP.NET] WebForm's ObjectDataSource has DateTime Property
續上篇,http://www.dotblogs.com.tw/yc421206/archive/2014/11/13/147293.aspx
按下更新或是新增,跳出下圖:
綁定的欄位有日期型態,在CUD模式有用到日期欄位的話,必須要設定 UpdateParameters | InsertParameters | DeleteParameters
public class Employee { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } public DateTime? Birthday { get; set; } }
SelectParameters 代表查詢參數,不需要設定,使用方式參考以下連結
http://www.dotblogs.com.tw/yc421206/archive/2014/11/23/147393.aspx
以本篇的範例而言只需要設定,UpdateParameters & InsertParameters
前端寫法
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="Simple.ObjectDataSourceBinding.EmployeeDataAccess" DataObjectTypeName="Simple.ObjectDataSourceBinding.Employee" OldValuesParameterFormatString="original_{0}" SelectMethod="GetEmployees" DeleteMethod="Delete" InsertMethod="Insert" UpdateMethod="Update"> <UpdateParameters> <asp:Parameter Name="Birthday" Type="DateTime" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="Birthday" Type="DateTime" /> </InsertParameters> </asp:ObjectDataSource>
後端寫法
protected void Page_Load(object sender, EventArgs e) { this.ObjectDataSource1.InsertParameters.Add(new Parameter("Birthday", TypeCode.DateTime)); this.ObjectDataSource1.UpdateParameters.Add(new Parameter("Birthday", TypeCode.DateTime)); }
兩者擇一即可。
本文出自:http://www.dotblogs.com.tw/yc421206/archive/2014/11/24/147407.aspx
範例下載:https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourceBinding/
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET