ListBox中的DateTime C#Asp.Net

时间:2022-01-17 11:18:28

I would like to fill a listBox with DateTime. So firstly, I've my code in ASP.NET

我想用DateTime填充listBox。首先,我在ASP.NET中使用了我的代码

<table>
    <tr>
        <td>&nbsp;
            <asp:ListBox 
                ID="ListBoxDate" 
                runat="server" 
                AutoPostBack="True"
                OnSelectedIndexChanged="ListBoxDate_SelectedIndexChanged" >
            </asp:ListBox>
        </td>
    </tr>
</table>

This is in C# the method

这是C#的方法

protected void Page_Load(object sender, EventArgs e)
{
    ListBoxDate.DataSource = GetAllDate(); /* it returns a list of objects Date (which is a class with an attribute DateTime) */
    ListBoxDate.DataTextField = "_Date";
    ListBoxDate.DataValueField = "_Date";
    ListBoxDate.DataBind();
}

And the method ListBoxDate_SelectedIndexChanged when I click on the event :

单击事件时方法ListBoxDate_SelectedIndexChanged:

protected void ListBoxDate_SelectedIndexChanged(object sender, EventArgs e)
{
    Date date= new Date()
    {
        _Date = ListBoxDate.SelectedValue,
    };
}

And when I click on my date, I just recover an empty string and not a DateTime or a string containing the date. (the listbox is OK when it displays on my browser)

当我点击我的日期时,我只是恢复一个空字符串而不是DateTime或包含日期的字符串。 (当我的浏览器显示列表框时,列表框正常)

I did the same principle with other attributes wich are string and it was OK. The problem is really the use of DateTime.

我对其他属性做了同样的原则,这是属于字符串的,它没关系。问题实际上是使用DateTime。

So, how can I recover the Date in after clicking on the listBox ?

那么,如何在点击listBox后恢复日期呢?

1 个解决方案

#1


2  

In Page_Load, you have to check if is not a postback before databinding the ListBox. Otherwise, the ListBox will be repopulated before entering ListBoxDate_SelectedIndexChanged and the SelectedValue value will be lost:

在Page_Load中,您必须在数据绑定ListBox之前检查是否不是回发。否则,在输入ListBoxDate_SelectedIndexChanged之前将重新填充ListBox,并且SelectedValue值将丢失:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack) 
    {
       ListBoxDate.DataSource = GetAllDate(); /* it returns a list of objects Date   (which is a class with an attribute DateTime) */
       ListBoxDate.DataTextField = "_Date";
       ListBoxDate.DataValueField = "_Date";
       ListBoxDate.DataBind();
    }
}

Have in mind that the SelectedValue property is a String, you'll have to parse its value to obtain a DateTime. You can using DateTime.Parse() for that.

请记住,SelectedValue属性是一个String,您必须解析其值以获取DateTime。您可以使用DateTime.Parse()。

#1


2  

In Page_Load, you have to check if is not a postback before databinding the ListBox. Otherwise, the ListBox will be repopulated before entering ListBoxDate_SelectedIndexChanged and the SelectedValue value will be lost:

在Page_Load中,您必须在数据绑定ListBox之前检查是否不是回发。否则,在输入ListBoxDate_SelectedIndexChanged之前将重新填充ListBox,并且SelectedValue值将丢失:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack) 
    {
       ListBoxDate.DataSource = GetAllDate(); /* it returns a list of objects Date   (which is a class with an attribute DateTime) */
       ListBoxDate.DataTextField = "_Date";
       ListBoxDate.DataValueField = "_Date";
       ListBoxDate.DataBind();
    }
}

Have in mind that the SelectedValue property is a String, you'll have to parse its value to obtain a DateTime. You can using DateTime.Parse() for that.

请记住,SelectedValue属性是一个String,您必须解析其值以获取DateTime。您可以使用DateTime.Parse()。