asp.net GridView控件绑定的日期格式不更新工作 [英] asp.net gridview bind dateformat not working with update

查看:251
本文介绍了asp.net GridView控件绑定的日期格式不更新工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TemplateField列其中显示了从一个DataSource日期时间的GridView。

I have a GridView with a TemplateField column which shows a DateTime from a DataSource.

<Columns>
    <asp:CommandField ShowEditButton="True" />

    <asp:TemplateField HeaderText="Start Date">
        <EditItemTemplate>
            <asp:TextBox ID="txtDateStart" runat="server" 
                Text='<%# Bind("dtDateStart", "{0:dd/MM/yyyy}") %>'</asp:TextBox>

        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" 
                Text='<%# Bind("dtDateStart", "{0:dd/MM/yyyy}") %>'></asp:Label>
         </ItemTemplate>

    </asp:TemplateField>
</Columns>

显示在正确的日期格式的作品,因为它应该。请注意,格式日之后开始MONTH

Displaying the date in the correct format works as it should. Note that the format starts with DAY followed by MONTH.

当我切换到编辑模式,在文本框中日期更改为 '31 -01-2013'并preSS GridView的更新链接我得到一个错误:
无法转换参数值从'System.String'dtDateStart'到'System.DateTime的
由GridView的不是我自己的code产生的错误。
碰巧我的DataSource UpdateMethod调用之前。

When I switch to edit mode, change the date in the TextBox to '31-01-2013' and press the GridView's update-link i get an error: Cannot convert value of parameter 'dtDateStart' from 'System.String' to 'System.DateTime' The error is generated by the GridView not my own code. It happens before the UpdateMethod of my DataSource is called.

当我键入 '01 -31-2012'数据被正确处理,值被更新到数据库中。

When i type '01-31-2012' the data is processed correctly and the value is updated into the database.

不知怎的,当日期显示它使用格式为: DD-MM-YYYY (就像我需要它)
但是,当它读取新的价值形式,它使用文本框的 MM-DD-YYYY

Somehow when the date is displayed it uses format dd-MM-yyyy (just as I need it to) But when it reads the new value form the TextBox it uses MM-dd-yyyy

有人可以帮帮我吗?

推荐答案

感谢您的回复。
我不知道在网站内区域性设置。我被假设与日期格式证明绑定功能就足够了。

thanks for your reply. I do not know about culture settings within the website. I was assuming that proving the Bind-function with a dateformat would be sufficient.

现在我已经实现了以下解决方法,这是对别人希望对大家有用:

By now I have implemented the following workaround, which is hopefully useful for others:

我添加一个附加属性上我的课是字符串类型。此属性我只用于绑定到GridView。

I have added an additional property to my class which is of type string. This property I only use for binding to the gridview.

// Original Date property //
public DateTime dtDateStart { get; set; }

// Additional Date property of type string // 
public string sDateStart
{
    get
    {
        return dtDateStart.ToString("dd/MM/yyyy");
    }
    set
    {
        dtDateStart = DateTime.ParseExact(value, "dd/MM/yyyy", null);
    }
}

我不知道这是否应该标记为答案。但也许其他人可以使用这个作为一个快速的解决方法为好。

I am not sure if this should be marked as the answer. But maybe others can use this as a quick workaround as well.

这篇关于asp.net GridView控件绑定的日期格式不更新工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆