我想知道是否有更简单的方法来格式化表单视图中的标签,我使用的代码就是这个,它在事件表单视图数据绑定内:
protected void FormView2_DataBound(object sender, EventArgs e)
{
if (FormView2.CurrentMode == FormViewMode.Edit)
{
Label DAT_Label1 = (Label)FormView2.FindControl("DAT_Label1");
if (DAT_Label1 != null)
{
DateTime date = Convert.ToDateTime(DAT_Label1.Text);
DAT_Label1.Text = string.Format("{0:dd/MM/yyyy}", date);
}
}
}
标签控件中是否没有可以帮助进行格式化的属性?
答案 0 :(得分:2)
标准Short Date format specifier比您的代码略微冗长,但确实具有使用用户格式样式的优势,因此在英国显示dd / mm / yyyy或mm / dd / yyyy如果区域设置设置为美国
protected void FormView2_DataBound(object sender, EventArgs e)
{
if (FormView2.CurrentMode == FormViewMode.Edit)
{
Label DAT_Label1 = (Label)FormView2.FindControl("DAT_Label1");
if (DAT_Label1 != null)
{
DateTime date = Convert.ToDateTime(DAT_Label1.Text);
DAT_Label1.Text = string.Format("{0:d}", date);
}
}
}