asp.net 中我想编辑dropdownlist选中值,可是SelectedValue总是ID值,怎么回事? 我通过一览画面,选择一个复选框,点击编辑按钮,总是程序出错,提示:“ “ddlType” 有一个无效 SelectedValue,因为它不在项目列表中。参数名: value”,调试发现dropdownlist的ddlType.SelectedValue = model.Type; 查出来的总是ID,插入数据时插入的也是ID,请问高手怎么解决! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ddlLogsGroup.DataSource = BlogLogic.SearchUserGroupNameByUserId(userId); ddlLogsGroup.DataTextField = "AriticleGroupName"; 是不是你的下拉列表绑定value的时候绑定错了 ddlLogsGroup.DataValueField = "AriticleGroupId"; //一般这里是id,上面那个是Name ddlLogsGroup.DataBind(); 你应该把代码贴出来 看不到你的属性设置 应该是你的SelectedValue和SelectedItem问题 用seletedItem属性不用selectedValue /// <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; }注:走到这里就出错! this.ddlType。item[ this.ddlType.SelectedValue ].Text 如果你确定 model.Type 这个有值 那可是就是 你加载的时候出了问题 先绑定ddlType 然后this.ddlType.SelectedValue = model.Type ddlType.SelectedValue = model.Type;替换成ddlType.SelectedIndex = ddlType.Items.IndexOf(ddlType.Items.FindByValue(model.Type)); this.ddlType.SelectedValue = model.Type;没有SelectedText呀? this.ddlType.SelectedValue = model.Type;这句话有没有报错? this.ddlType.SelectedValue = model.Type;这玩意儿是不能赋值的吧 应该是你加载的时候出了问题 先绑定ddlType 然后this.ddlType.SelectedValue = model.Type 很有可能是这个原因 我需要看到所有的C#代码才知道 我现在问你一下,我要插入数据库代码又该如何写呢? string type = this.ddlType.SelectedValue; model.Type = type;怎么改朋友! 我现在问你一下,我要插入数据库代码又该如何写呢? string type = this.ddlType.SelectedValue; model.Type = type;怎么改朋友! 我现在问你一下,我要插入数据库代码又该如何写呢? string type = this.ddlType.SelectedValue; model.Type = type;怎么改朋友! /// <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(); }这样? 你的写法是没有错的,错误的原因可能是加载的顺序输了问题 dropdownlist 这个东西一定要先加载 只需要页面初始化加载一次就行了,其他的地方应该是没有问题的 或许你把代码发给我看看,我再告诉你怎么改 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(); 应该是这样写的...... string type = this.ddlType.SelectedItem.Value;model.Type = type; 我说的那个问题解决了,可是现在出现我插入数据时候插入的是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); } } } } string type = this.ddlType.SelectedValue 这个取的不是你选中那一行的ID吗? 还是随便选中哪一个都是固定的ID 先试试,我才知道答案... model.Type=dropdownlist.SelectedItem.Text 不知道你是不是要的是这个 试试看吧,不对在说 先看下你model.Type是不是string类型的,是的话就像楼上那样model.Type=dropdownlist.SelectedItem.Text 不是的话你就先改下 model.Type=dropdownlist.SelectedItem.Text 谢谢代码! **********缓存和XML********** 多层模式,一般怎么测试 一个分析器错误怎么回事 DataList 怎么设置才出数据??? url参数为空判断? ASP.NET操作Word的的问题: 哪里错了? listbox的问题。。为什么selected总是取不到值呢? ASP.NET环境出问题了,求教高手 同志们,散分了,现在还有29分。 IbatisNet 把表名作为参数 MVC中的返回html 为何报 “/”应用程序中的服务器错误。路径中具有非法字符
ddlLogsGroup.DataSource = BlogLogic.SearchUserGroupNameByUserId(userId);
ddlLogsGroup.DataTextField = "AriticleGroupName"; 是不是你的下拉列表绑定value的时候绑定错了
ddlLogsGroup.DataValueField = "AriticleGroupId"; //一般这里是id,上面那个是Name
ddlLogsGroup.DataBind();
不用selectedValue
/// 绑定类型下拉框
/// </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;
}
注:走到这里就出错!
替换成
ddlType.SelectedIndex = ddlType.Items.IndexOf(ddlType.Items.FindByValue(model.Type));
this.ddlType.SelectedValue = model.Type;这句话有没有报错?
this.ddlType.SelectedValue = model.Type;这玩意儿是不能赋值的吧
string type = this.ddlType.SelectedValue;
model.Type = type;
怎么改朋友!
我现在问你一下,我要插入数据库代码又该如何写呢?
string type = this.ddlType.SelectedValue;
model.Type = type;怎么改朋友!
我现在问你一下,我要插入数据库代码又该如何写呢?
string type = this.ddlType.SelectedValue;
model.Type = type;怎么改朋友!
/// <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();
}
这样?
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(); 应该是这样写的......
model.Type = type;
我说的那个问题解决了,可是现在出现我插入数据时候插入的是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);
}
}
}
}
model.Type=dropdownlist.SelectedItem.Text
不是的话你就先改下
谢谢代码!