如何在属性中设置日期时间默认值? [英] How to set datetime default value in a property?

查看:125
本文介绍了如何在属性中设置日期时间默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码在自定义属性中设置默认值DateTime.Now:

I am trying to set default value DateTime.Now in my custom Property using the following code:

<DefaultValue(GetType(DateTime), "Now")>


但是,它设置了一个空白值.请帮帮我.
预先感谢.


But, it sets a blank value. Please, help me.
Thanks in advance.

推荐答案

而不是使用"Now",而应使用类似DateTime.Now.ToString的东西.
Rather than use "Now", you should use something like DateTime.Now.ToString.


DefaultValueAttribute实际上并不设置属性值,而只是通知编辑器已在代码中设置了什么默认值.希望该示例可以阐明这个概念.

The DefaultValueAttribute does not actually set the property value but just informs the editor what default value has been set in code. Hopefully the example will clarify the concept.

Imports System.ComponentModel

Public Class MyClass
    Private Const InitialValue As Int32 = 37
    Private pvalue As Int32

    Public Sub New()
        pvalue = InitialValue
    End Sub

    <DefaultValue(InitialValue)> _
    Public Property MyProperty() As Int32
        Get
            Return pvalue
        End Get
        Set
            pvalue = value
        End Set
    End Property
End Class



另外请注意,属性指定了一个常量值,因此不可能说默认值就是变量DateTime.Now.

艾伦



Additionally note that an attribute specifies a constant value so it is not possible to say that a default value is the very variable DateTime.Now.

Alan


朋友,

假设您的文本框ID为


< asp:TextBox ID ="txtDates" runat ="server"></asp:TextBox>

在页面加载中调用以下代码将获得您想要的内容



txtDates.Text = System.DateTime.Now.ToString("dd/MM/yyyy");


您可以根据需要更改格式,我使用的是dd/MM/yyyy
Hi fiend,

Suppose your textbox id is


<asp:TextBox ID="txtDates" runat="server"></asp:TextBox>

call the below code in Page Load will get what u want



txtDates.Text = System.DateTime.Now.ToString("dd/MM/yyyy");


The Format you can change according to your need I am using dd/MM/yyyy


这篇关于如何在属性中设置日期时间默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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