我通过一览画面,选择一个复选框,点击编辑按钮,总是程序出错,提示:“ “ddlType” 有一个无效 SelectedValue,因为它不在项目列表中。参数名: value”,调试发现dropdownlist的ddlType.SelectedValue = model.Type; 查出来的总是ID,插入数据时插入的也是ID,请问高手怎么解决!

解决方案 »

  1.   

     
    ddlLogsGroup.DataSource = BlogLogic.SearchUserGroupNameByUserId(userId);
         ddlLogsGroup.DataTextField = "AriticleGroupName"; 是不是你的下拉列表绑定value的时候绑定错了
         ddlLogsGroup.DataValueField = "AriticleGroupId"; //一般这里是id,上面那个是Name
         ddlLogsGroup.DataBind();
      

  2.   

    你应该把代码贴出来  看不到你的属性设置   应该是你的SelectedValue和SelectedItem问题
      

  3.   

    用seletedItem属性
    不用selectedValue
      

  4.   

            /// <summary>
            /// 绑定类型下拉框
            /// </summary>
            protected void BindType()
            { 
                DataSet ds = new AutoWebSite.Code.BLL.Class().GetList("");
                this.ddlType.DataTextField = "name";
                this.ddlType.DataValueField = "id";
                this.ddlType.DataSource = ds;
                this.ddlType.DataBind();
            }页面代码:
     <tr class="alt2">
                            <td class="text_right" width="130">
                               类型:</td>
                            <td width="*" class="style1">
                                <asp:DropDownList ID="ddlType" runat="server" CssClass="formInput" 
                                    EnableTheming="True" >
                                    
                                </asp:DropDownList>
                                <br>
                                <span class="form_clue"></span>
                            </td>
                        </tr>转到编辑代码:
       /// <summary>
            /// 编辑方法
            /// </summary>
            /// <param name="id"></param>
            protected void EditBind(string id)
            {
                AutoWebSite.Code.Model.ImgList model = new AutoWebSite.Code.BLL.ImgList().GetModel(id);
                this.HIdstring.Value = model.Id;
                this.txtTitle.Text = model.Title;
                this.ddlType.SelectedValue = model.Type;            string imgImg = model.Img;
                if ((imgImg == null) || (imgImg.Length == 0))
                {
                    this.imgpreviewImg.Visible = false;
                }
                else
                {
                    this.imgpreviewImg.Visible = true;
                    this.imgpreviewImg.ImageUrl = AppConfig.UploadFilePath + AppConfig.Upimgqz + imgImg;
                }
                model = null;
            }
    注:走到这里就出错!
      

  5.   

     this.ddlType。item[ this.ddlType.SelectedValue ].Text
      

  6.   

    如果你确定 model.Type 这个有值   那可是就是 你加载的时候出了问题   先绑定ddlType 然后this.ddlType.SelectedValue = model.Type
      

  7.   

    ddlType.SelectedValue = model.Type;
    替换成
    ddlType.SelectedIndex = ddlType.Items.IndexOf(ddlType.Items.FindByValue(model.Type));
      

  8.   

    this.ddlType.SelectedValue = model.Type;没有SelectedText呀?
      

  9.   


    this.ddlType.SelectedValue = model.Type;这句话有没有报错?
      

  10.   


    this.ddlType.SelectedValue = model.Type;这玩意儿是不能赋值的吧 
      

  11.   

    应该是你加载的时候出了问题 先绑定ddlType 然后this.ddlType.SelectedValue = model.Type  很有可能是这个原因  我需要看到所有的C#代码才知道
      

  12.   

    我现在问你一下,我要插入数据库代码又该如何写呢?
      string type = this.ddlType.SelectedValue;
      model.Type = type;
    怎么改朋友!
      

  13.   


    我现在问你一下,我要插入数据库代码又该如何写呢?
      string type = this.ddlType.SelectedValue;
      model.Type = type;
    怎么改朋友!
      

  14.   


    我现在问你一下,我要插入数据库代码又该如何写呢?
      string type = this.ddlType.SelectedValue;
      model.Type = type;
    怎么改朋友!
      

  15.   


    /// <summary>
      /// 绑定类型下拉框
      /// </summary>
      protected void BindType()
      {  
      DataSet ds = new AutoWebSite.Code.BLL.Class().GetList("");
    this.ddlType.DataSource = ds;
      this.ddlType.DataTextField = "name";
    this.ddlType.DataValueField = "id"; 
      this.ddlType.DataBind();
      }
    这样?
      

  16.   

      你的写法是没有错的,错误的原因可能是加载的顺序输了问题   dropdownlist  这个东西一定要先加载 只需要页面初始化加载一次就行了,其他的地方应该是没有问题的 或许你把代码发给我看看,我再告诉你怎么改
      

  17.   

     DataSet ds = new AutoWebSite.Code.BLL.Class().GetList("");
      this.ddlType.DataTextField = "name";
    this.ddlType.DataValueField = "id"; this.ddlType.DataSource = ds;
      this.ddlType.DataBind();
    你这种写法应该是错误的
     DataSet ds = new AutoWebSite.Code.BLL.Class().GetList("");
    this.ddlType.DataSource = ds;
     this.ddlType.DataTextField = "name";
    this.ddlType.DataValueField = "id";  
     this.ddlType.DataBind();  应该是这样写的......
      

  18.   

    string type = this.ddlType.SelectedItem.Value;
    model.Type = type;
      

  19.   


    我说的那个问题解决了,可是现在出现我插入数据时候插入的是ID,不是下拉框选中的值,怎么解决。
    代码如下:        /// <summary>
            /// 保存事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnSave_Click(object sender, EventArgs e)
            {
                string msg = "";
                AutoWebSite.Code.Model.ImgList model = new AutoWebSite.Code.Model.ImgList();
                string Title = this.txtTitle.Text;
                model.Title = Title;
                string imgImg = EImage.UpLoadFile(this.imguploadImg.PostedFile.FileName, this.imguploadImg.PostedFile.InputStream, true)[0];
                string oldimgImg = this.imgpreviewImg.ImageUrl;
                imgImg = (imgImg == null) ? oldimgImg.Substring(oldimgImg.LastIndexOf("/") + 1).Replace(AppConfig.Upimgqz, "") : imgImg;
                model.Img = imgImg;
                //string Url = this.txtUrl.Text;
                //model.Url = Url;
                //string Num = this.txtNum.Text;
                //if (Num.Length > 0)
                //{
                //    model.Num = Convert.ToInt32(Num);
                //}
                //ddlType.SelectedIndex = ddlType.Items.IndexOf(ddlType.Items.FindByValue(model.Type));
                string type = this.ddlType.SelectedValue;
                model.Type = type;
                AutoWebSite.Code.BLL.ImgList bll = new AutoWebSite.Code.BLL.ImgList();
                string hid = this.HIdstring.Value;
                try
                {
                    try
                    {
                        if (hid.Length == 0)
                        {
                            model.Id = EUtil.GetGuid();
                            bll.Add(model);
                        }
                        else
                        {
                            model.Id = hid;
                            bll.Update(model);
                        }
                        this.CleanText();
                        msg = "保存成功!!";
                        base.ShowTip(msg, msg);
                    }
                    catch (Exception ex)
                    {
                        msg = "保存失败!!";
                        string detils = EString.CreateError(ex).Replace("\r", "").Replace("\n", "");
                        base.ShowTip(msg, detils);
                    }
                }
                finally
                {
                    this.GridBind();
                    if (hid.Length > 0)
                    {
                        this.SetVisible(1);
                    }
                    else
                    {
                        Button btn = sender as Button;
                        if (btn.ID.Equals("btnSaveReturn"))
                        {
                            this.SetVisible(1);
                        }
                        else if (btn.ID.Equals("btnSave"))
                        {
                            this.SetVisible(2);
                        }
                    }
                }
            }       
      

  20.   

    string type = this.ddlType.SelectedValue  这个取的不是你选中那一行的ID吗?  还是随便选中哪一个都是固定的ID 先试试,我才知道答案...
      

  21.   

    model.Type=dropdownlist.SelectedItem.Text   不知道你是不是要的是这个  试试看吧,不对在说
      

  22.   

    先看下你model.Type是不是string类型的,是的话就像楼上那样
    model.Type=dropdownlist.SelectedItem.Text 
    不是的话你就先改下
      

  23.   

    model.Type=dropdownlist.SelectedItem.Text 
    谢谢代码!