获取gridview里的dropdownlist的值时"未将对象引用设置到对象的实例"

kaqia2003 2010-11-30 11:15:01
我在gridview里增加了两列 一列是一个RadioButtonList,另一列是一个DropDownList

pageload里写了
if (!IsPostBack)
{}

在获取gridview的选取行的值时, RadioButtonList的值可以获取,但是DropDownList的值是"未将对象引用设置到对象的实例"

为什么取不到DropDownList的值啊.
...全文
462 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaqia2003 2010-11-30
  • 打赏
  • 举报
回复
我把取值的那段改成了
DropDownList DDownList1 = (DropDownList)list.SelectedRow.Cells[7].FindControl("DropDownList");

ssbd = DDownList1.Text.ToString();


还是一样的为空
kaqia2003 2010-11-30
  • 打赏
  • 举报
回复
自己再顶!
kaqia2003 2010-11-30
  • 打赏
  • 举报
回复
建了一个名为list的griview控件
有list_RowDataBound1和list_SelectedIndexChanged两件事件
protected void list_RowDataBound1(object sender, GridViewRowEventArgs e)
{
e.Row.Height = 30;
DataRowView drvSfhg;
string strHg;
int i;
if (e.Row.RowType == DataControlRowType.DataRow)
{
//外观样式
if (e.Row.RowIndex % 2 == 0)
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
else
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"#EFF3F7\"");
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#D1DDF1\"");
//编号
e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();

drvSfhg = (DataRowView)e.Row.DataItem;
strHg = drvSfhg.Row["sfhg"].ToString();

if (strHg == "1")
{
e.Row.Cells[4].Text = "是";
e.Row.Cells[4].ForeColor = Color.Red;
}
else
{
e.Row.Cells[4].Text = "否";
}



RadioButtonList rblidea = (RadioButtonList)e.Row.FindControl("rblidea");
HiddenField hditem = (HiddenField)e.Row.FindControl("hditem");
DropDownList DDownList1 = (DropDownList)e.Row.FindControl("DDownList1");

if (hditem.Value == "1")
{
rblidea.Items[0].Selected = true;

}
else
{
rblidea.Items[1].Selected = true;
}


//生成标段选择列表
for (i = 1; i <= bdsm; i++)
{
DDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));

}
}
}

-------------------------------------------------------------------------
protected void list_SelectedIndexChanged(object sender, EventArgs e)
{
string zbggbm, qybm, ssbd, sftbr;
zbggbm = Request.QueryString["sgzbbm"].ToString();
GridViewRow row = list.SelectedRow;
qybm = row.Cells[3].Text.Trim();

RadioButtonList rblidea = (RadioButtonList)row.FindControl("rblidea");
if (rblidea.Items[0].Selected == true)
{
sftbr = "1";
}
else
{
sftbr = "0";
}


DropDownList DDownList1 = (DropDownList)row.FindControl("DropDownList");
ssbd = DDownList1.Text.ToString();
}
free_live_zyx 2010-11-30
  • 打赏
  • 举报
回复
建议楼主把代码粘全
l171147904 2010-11-30
  • 打赏
  • 举报
回复
建议用 JS去取值!

获取 GRIDVIEW 控件, GRIDVIEW
document.getElementById("GRIDVIEW1").ROW 去取值
lovexiu0924 2010-11-30
  • 打赏
  • 举报
回复
你那样找肯定是错的 这样去找GridView1.SelectedRow.FindControl("XX");
ltcszk 2010-11-30
  • 打赏
  • 举报
回复
row是什么
l171147904 2010-11-30
  • 打赏
  • 举报
回复
取不到是对的!!!! 下拉的ID已经被 GRIDVIEW改变了!!你看HTML代码就知道了!

既然ROW能取的到,就能ROW.ITEM 取到 下拉所在的 列! 然后再转换 DropDownList
zcldeson 2010-11-30
  • 打赏
  • 举报
回复
调试看看SQL取到 对象集合了么..如果没有SQL除了问题,如果有,就是给值的时候出了问题
lunber 2010-11-30
  • 打赏
  • 举报
回复
ssbd = DDownList1.Text.ToString();
guolunfeng 2010-11-30
  • 打赏
  • 举报
回复
你循环的哪个i的值应该是空的,你response出来看一下吧,应该是没有值的
kaqia2003 2010-11-30
  • 打赏
  • 举报
回复
RadioButtonList的值是可以获取的,倒是DropDownList的值没办法获取.
xiehuijianlove 2010-11-30
  • 打赏
  • 举报
回复
RadioButtonList rblidea = (RadioButtonList)row[i].FindControl("rblidea");
kaqia2003 2010-11-30
  • 打赏
  • 举报
回复
protected void list_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList rblidea = (RadioButtonList)row.FindControl("rblidea");
if (rblidea.Items[0].Selected == true)
{
sftbr = "1";
}
else
{
sftbr = "0";
}


DropDownList DDownList1 = (DropDownList)row.FindControl("DropDownList");
ssbd = DDownList1.SelectedItem.Text.ToString();

}

写在gridview的SelectedIndexChanged事件里
kaqia2003 2010-11-30
  • 打赏
  • 举报
回复
获取DropDownList值的语句
DropDownList DDownList1 = (DropDownList)row.FindControl("DropDownList");
ssbd = DDownList1.SelectedItem.Text.ToString();
zhang_13245768 2010-11-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kaqia2003 的回复:]
获取DropDownList值的语句
DropDownList DDownList1 = (DropDownList)row.FindControl("DropDownList");
ssbd = DDownList1.SelectedItem.ToString();[/Quote]
这句写在哪里的
林少1024 2010-11-30
  • 打赏
  • 举报
回复
DropDownList生成语句
for (i = 1; i <= bdsm; i++)
{

DDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
写在哪....
kaqia2003 2010-11-30
  • 打赏
  • 举报
回复
DropDownList生成语句
for (i = 1; i <= bdsm; i++)
{

DDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}

获取DropDownList值的语句
DropDownList DDownList1 = (DropDownList)row.FindControl("DropDownList");
ssbd = DDownList1.SelectedItem.ToString();
lunber 2010-11-30
  • 打赏
  • 举报
回复
没有代码不知道错在哪
四维空间151 2010-11-30
  • 打赏
  • 举报
回复
我之前也是这个错误,但是后来我自己解决了,是取值的问题,对DropDownList来说,他是一个很特别的类,他的实例不是在gridview里面,有时间找到我的给你看看,只要对他先填充数据。
加载更多回复(3)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧