“DropDownList1”有一个无效 SelectedValue,因为它不在项目列表中。

I_was_a_novice I_was_a_novice 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
2个表table1,table2
table1列:id,name
table2列:id,name,sort1 //table1的name=table2的sort1

DropDownList1绑定table1
GridView1绑定table2
点修改,DropDownList1显示相应的 一级分类


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection(ConnectionString);
string sql = "select * from table1";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);

DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();

string sql2 = "select * from table2";
SqlDataAdapter da2 = new SqlDataAdapter(sql2, conn);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
GridView1.DataSource = ds2;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = int.Parse(e.CommandArgument.ToString());
int id = int.Parse(this.GridView1.DataKeys[index].Value.ToString());
if (e.CommandName == "Edit")
{
SqlConnection conn = new SqlConnection(ConnectionString);
string sql = "select * from table2 where id=" + id + "";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.SelectedValue = ds.Tables[0].Rows[0]["sort1"].ToString();
//Response.Write(ds.Tables[0].Rows[0]["sort1"].ToString());
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{

}

错误:

这是为什么啊
...全文

相关推荐

DropDownList等列表类控件的SelectedValue值的安全绑定

在ASP.NET 2.0 网站页面的开发过程中,经常需要把DropDownList等列表类控件的SelectedValue值设置为一个从数据库或其他地方读取出来的值。 最简单的办法就是直接进行指定: DropDownList1.SelectedValue = "中国"; 但有的时候如果DropDownList1中没有"中国"这一项的话,赋值就会出现异常,这是我们所不愿意看到的,那么有没有一种安全的绑定方法来避免出现这样的情况呢?当然是有的,看完我传的资料,相信一定能使你有所收获,我也是网上搜集的呢~~

dwwe1988df

DropDownList绑定选择数据报错提示异常解决方案

抛出的异常信息:异常详细信息: System.ArgumentOutOfRangeException: “ddlTotalCostDiscount ”有一个无效 SelectedValue,因为它不在项目列表中。 原先给DropDownList这样赋值: this.ddlTotalCostDiscount.SelectedValue = obj.TotalCostDiscount.ToString(); 改成这样赋值: ddlTotalCostDiscount.SelectedIndex = ddlTotalCostDiscount.Items.IndexOf(ddlTotalCostDisc

weixin_38548434

ASP dropdownlist缺少值错误

'DropDownList'的SelectedValue无效,因为它在项目列表中不存在。

weixin_38622962

三个DropDownList自动生成年月日

三个DropDownList自动生成年月日,代码复制即可使用

mynight

asp.net中DropDownList控件的二级联动

asp.net(C#)中DropDownList控件的二级联动,两种方法,第一种为普通方法,第二种为从数据库绑定DropDownList的。

我本良人
评论 9
收藏 已收藏
分享
返回顶部
I_was_a_novice
.NET社区 62,275 成员 668,968 内容