像这样填充的列表框:

if (ds != null)
{
    ListPreviousRecords.Items.Clear();

    ListPreviousRecords.DataSource = ds;
    ListPreviousRecords.DataTextField = "Date";
    ListPreviousRecords.DataValueField = "ID";
    ListPreviousRecords.DataBind();
}

获取选定的值:
protected void ListPreviousRecords_OnSelectedIndexChanged(object sender, EventArgs e)
{
    if(ListPreviousRecords.SelectedItem.Value != "")
    {
        int mySelectedValue = int.Parse(ListPreviousRecords.SelectedItem.Value);// throwing null exception
        loadPreviousDetails(mySelectedValue);
    }
}

最佳答案

您可以添加此代码以确保输入非空值

if(!string.IsNullOrEmpty(ListPreviousRecords.SelectedItem.Value ))
{
...
}

并确保在您的控件上设置了 AutoPostBack="true"
链接:http://msdn.microsoft.com/fr-fr/library/system.string.isnullorempty.aspx

关于c# - 列表框 Selected.value 抛出空异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15497199/

10-13 05:53