与自定义表达式的两种方式绑定?

发布于 2024-11-05 23:42:47 字数 663 浏览 4 评论 0原文

我在我的 asp.net 项目中使用 gridview 来查看和修改数据库中的一些记录。数据库有两列:start_date 和 end_date。创建新记录时,这些列包含 null,但稍后可以使用 gridview update 命令修改它们。

在 gridview 中,我有两个模板字段(名称为 start_date 和 end_date),我在其中放置了两个日历控件。单击 gridview 的更新链接后,由于绑定到日历的空值,它总是返回错误。我已经使用这个辅助函数来解决它:

protected DateTime ReplaceNull(Object param)
{
    if (param.Equals(DBNull.Value))
    {
        return DateTime.Now;
    }
    else
    {
        return Convert.ToDateTime(param);
    }
}

并在日历控件的 SelectedDate 中使用了这两个自定义表达式:

ReplaceNull(Eval("start_date"))
ReplaceNull(Eval("end_date"))

问题是在选择日期时绑定日历的双向数据不会更新数据库表。有什么解决方法吗?或者,更好的解决方案将不胜感激。

I am using a gridview in my asp.net project to view and modify some records from the database. The database has two columns: start_date and end_date. When a new record is created these columns contains null, but they can be modified later using the gridview update command.

In gridview I have two template fields (having names start_date and end_date) in which I have placed two calendar controls. Upon clicking an update link of gridview it always returns an error because of the null value binding to the calendar. I have used this helper function to solve it:

protected DateTime ReplaceNull(Object param)
{
    if (param.Equals(DBNull.Value))
    {
        return DateTime.Now;
    }
    else
    {
        return Convert.ToDateTime(param);
    }
}

and used these two custom expressions in calendar control's SelectedDate:

ReplaceNull(Eval("start_date"))
ReplaceNull(Eval("end_date"))

The problem is that two-way data binding the calendars upon selecting a date does not update the database table. Are there any workarounds? Or alternatively, a better solution would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2024-11-12 23:42:47

我不知道为什么在插入新记录时让它们为空,但我认为有很多方法可以解决这个问题。

其中之一:在GridviewRowDataBound事件中

 protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               if (e.Row.Cells[1] == null) //the index of the start_date
                 {
                    e.Row.Cells[1].Text = DateTime.Now.ToString(); // in your case you will  make the selected date of the calender(through casting to calender) with the value you need.
                 }

            }
        }

或者:你可以捕获异常,你在 的更新按钮。

通过try and catch 块

I don't know why you let them null when insert a new record , but many ways you can solve this problem i think .

one of them : in the RowDataBound event of the Gridview

 protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               if (e.Row.Cells[1] == null) //the index of the start_date
                 {
                    e.Row.Cells[1].Text = DateTime.Now.ToString(); // in your case you will  make the selected date of the calender(through casting to calender) with the value you need.
                 }

            }
        }

Or: you can catch the exception , you meet in your Update button through

try and catch block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文