簡體   English   中英

如何從 DropDownList 獲取 ID?

[英]How to get the ID from DropDownList?

當我從 DropDownList 中選擇不同的值時,我試圖保存所做的更改。 但我收到此錯誤

值不能為空。 參數名稱:字符串 [ArgumentNullException:值不能為空。 參數名稱:字符串]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, 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 顯示錯誤的值

只是猜測您提供的信息,但應該是這一行:

ddlTrailerLoc.SelectedValue.ToString();

實際上是:

TrailerID = ddlTrailerLoc.SelectedValue.ToString();

否則無法從設置 TrailerID 的代碼中確定。

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

然后在您需要使用它時為其賦值。 像預告片ID。 我認為這就是您想要實現的目標。 如果是這樣,您可以將 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);
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM