【问题标题】:How to get the ID from DropDownList?如何从 DropDownList 中获取 ID?
【发布时间】: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


【解决方案1】:

只是根据您提供的信息猜测,但应该是这一行:

ddlTrailerLoc.SelectedValue.ToString();

其实是:

TrailerID = ddlTrailerLoc.SelectedValue.ToString();

否则无法从设置 TrailerID 的代码中确定。

【讨论】:

  • 是的,我已经尝试过了,但它会导致错误Input string was not in a correct format.,因为它将作为字符串的拖车位置设置为 int 的 id。 TrailerID 从 DropDownList 变为“测试”,但它应该是一个数字
  • 那么为什么它应该是一个数字时却是“测试”呢?如何填充 DropDownList?
  • 我认为(如上述用户所述)您需要检查 DropDownList 的人口。您通常期望 ListItem 的“文本”属性是“测试”,而不是值(正如您所说,期望是整数)。
  • @Hysteria86 我已将用于填充 DropDownList 的代码放入我的问题中
  • 您似乎对 DropDownList 项的值和文本字段都使用了“描述”。我假设值“描述”是“测试”。对于 ddlTrailerLoc.DataValueField,您应该使用数据集中返回唯一整数值​​(TrailerID 或类似内容)的字段之一。
【解决方案2】:
  protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlTrailerLoc=sender as DropDownList;
string value = "".
            if (!String.IsNullOrEmpty(ddlTrailerLoc.SelectedValue.ToString())
            {
                value = ddlTrailerLoc.SelectedValue.ToString();

                Trailer.UpdateTrailer(int.Parse(TrailerID), Company.Current.CompanyID,txtTrailerReg.Text,ddlTrailerLocation.Text);
            }
        }

然后在你需要使用它的时候给它赋值。就像 TrailerID。我认为这就是您要实现的目标。如果是这样,您可以将 TrailerID 分配给 ddlTrailerLoc.SelectedValue.ToString();然后不需要声明字符串值。在这种情况下:

protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlTrailerLoc=sender as DropDownList;
if (!String.IsNullOrEmpty(ddlTrailerLoc.SelectedValue.ToString())
{
TrailerID = ddlTrailerLoc.SelectedValue.ToString();
Trailer.UpdateTrailer(int.Parse(TrailerID), Company.Current.CompanyID,txtTrailerReg.Text,ddlTrailerLocation.Text);
}
}

【讨论】:

  • 设置TrailerID = ddlTrailerLoc.SelectedValue.ToString(); 会导致错误输入字符串格式不正确。因为它将作为字符串的拖车位置设置为 int 的 id。 TrailerID 从 DropDownList 变为“测试”,但它应该是一个数字
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多