3

填充列表框如下:

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);
    }
}
4

3 回答 3

6

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

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

并确保AutoPostBack="true"在您的控制下设置

链接:http: //msdn.microsoft.com/fr-fr/library/system.string.isnullorempty.aspx

于 2013-03-19T10:43:08.667 回答
1

改变:

 if(ListPreviousRecords.SelectedItem.Value != "")

到:

  if (!string.IsNullOrEmpty(ListPreviousRecords.SelectedItem))
于 2013-03-19T10:44:18.080 回答
0

使用ListPreviousRecords.SelectedValue.

if (!string.IsNullOrWhiteSpace(ListPreviousRecords.SelectedValue)) {
    // ...
}
于 2013-03-19T10:43:29.733 回答