gpt4 book ai didi

c# - 列表框 Selected.value 抛出 null 异常

转载 作者:行者123 更新时间:2023-11-30 14:13:01 24 4
gpt4 key购买 nike

像这样填充的列表框:

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 抛出 null 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15497199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com