[ASP.NET][C#][DropDownList][Debug]錯誤訊息「不可以在 DropDownList 中選取多個項目。」

  • 49
  • 0

.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sCustId = Convert.ToString(Request.QueryString["c"] ?? "");

            ////這樣寫會發生錯誤。錯誤訊息「不可以在 DropDownList 中選取多個項目。」
            //foreach (ListItem item in ddlCustomer.Items)
            //{
            //    if (item.Value == sCustID)
            //    {
            //        item.Selected = true;
            //        break;
            //    }
            //}

            //這樣寫才對
            for (int i = 0; i < ddlCustomer.Items.Count; i++)
            {
                if (ddlCustomer.Items[i].Value == sCustId)
                {
                    ddlCustomer.SelectedIndex = i;
                    break;
                }
            }
        }
    }