【发布时间】:2015-06-10 09:28:58
【问题描述】:
当我从 DropDownList 中选择不同的值时,我正在尝试保存所做的更改。但是我收到了这个错误
值不能为空。参数名称:字符串[ArgumentNullException: 值不能为空。参数名称:字符串]
System.Number.StringToNumber(String str, NumberStyles 选项, NumberBuffer& 数字, NumberFormatInfo 信息, Boolean parseDecimal) +10722118 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +145 System.Int32.Parse(String s) +23
我认为问题在于它没有在 DropDownList 中找到值的 ID,因此它在更新语句 Trailer.UpdateTrailer(int.Parse(TrailerID), 中捕获了这一行
protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlTrailerLoc=sender as DropDownList;
if (ddlTrailerLoc != null)
{
ddlTrailerLoc.SelectedValue.ToString();
Trailer.UpdateTrailer(int.Parse(TrailerID), Company.Current.CompanyID,txtTrailerReg.Text,ddlTrailerLocation.Text);
}
}
如何获取从 DropDownList 中选择的值的 ID?
这是填充 DropDownList 的代码
protected void PopulateDDLs(DropDownList ddlTrailerLoc)
{
DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
if (dsTrailerLocation.Tables[0].Rows.Count > 0)
{
ddlTrailerLoc.DataSource = dsTrailerLocation;
ddlTrailerLoc.DataValueField = "Description";
ddlTrailerLoc.DataTextField = "Description";
ddlTrailerLoc.DataBind();
}
else
{
ddlTrailerLoc.Items.Insert(0, new ListItem("No Locations Entered", "0"));
}
}
我知道这个ddlTrailerLoc.DataValueField = "Description"; 应该设置为 TrailerID 或其他东西,但它只有在它是描述时才有效。更改此设置会使 DropDownList 显示错误的值
【问题讨论】:
-
从您的示例中不清楚 TrailerID 变量的初始化位置。您可能需要在事件处理程序中显式初始化它,如下所示: TrailerID=((Trailer)ddlTrailerLoc.SelectedValue).TrailerID,其中 Trailer 是 DropDownList 项目的类
-
设置 DropDownList 值和文本字段如下 ddlTrailerLoc.DataValueField = "Trailer_ID"; // 你想要的 id 字段 ddlTrailerLoc.DataTextField = "Trailer_Name";
标签: c# asp.net database drop-down-menu selectedindexchanged