开发者

Default DateTime Parameter in EntityDataSource

I have a ASP.Net DetailsView, bound to an EntityDataSource control. My EntitySet has a DateTime Property that I want to set to a default value of DateTime.Now I don't want to display this property on the DetailsView. I figure I could use an InsertParameter in my EntityDataSource, but don't know the syntax, or whether this is possible:

        <InsertParameters>
            <asp:Parameter DbType="DateTime2" DefaultValue="" /&开发者_如何转开发gt;
        </InsertParameters>

Am I taking the right approach?


Well, it doesn't look like it's possible to do this in HTML, but you can do it easy in code behind. Simply attach an Inserting event handler to your entity data source, and then set the DateTime parameter to the defaults you require:

    protected void edsNewsItem_Inserting(object sender, EntityDataSourceChangingEventArgs e)
    {
        var newsItem = e.Entity as NewsItem;
        if (newsItem != null)
        {
            newsItem.Date = DateTime.Now;
            newsItem.LastUpdated = DateTime.Now;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜