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

null 値が入力されないようにするために、このコードを追加できます。

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 に答える