日期:2014-05-18  浏览次数:20426 次

gridview模板列数据绑定问题:“未将对象引用设置到对象的实例。”
我的目标是把gridview模板列中DropDownList1选中的值赋给Label3。   DropDownList1数据绑定成功了。代码如下~~~            
  //所在区域的绑定;
                if   (e.Row.RowType   ==   DataControlRowType.DataRow)
                {
                        foreach   (GridViewRow   row   in   GridView1.Rows)
                        {
      Label   lb   =   (Label)row.FindControl( "Label3 ");
      DropDownList   ddl   =   (DropDownList)row.FindControl( "DropDownList1 ");
      lb.Text   =   ddl.SelectedItem.Text;
                        }
                }

错误提示:lb.Text   =   ddl.SelectedItem.Text;------:“未将对象引用设置到对象的实例。”

------解决方案--------------------
Label lb = (Label)GridView1.FindControl( "Label3 ");
DropDownList ddl = (DropDownList)GridView1.FindControl( "DropDownList1 ");
lb.Text = ddl.SelectedItem.Text;
这样行不 瞎说的

------解决方案--------------------

foreach (GridViewRow i in GridView1.Rows)
{
DropDownList ddl = (DropDownList)GridView1.Rows[i.RowIndex].FindControl( "DropDownList1 ");
Label lb = (Label)GridView1.Rows[i.RowIndex].FindControl( "Label3 ");
lb.Text = ddl.SelectedItem.Text;
}
看看
------解决方案--------------------
到底是那个对象是null呢?是ddl.SelectedItem么?

你是在哪个事件里做的,为什么还有在里面做循环??


if (e.Row.RowType == DataControlRowType.DataRow)
{

Label lb = (Label)e.Row.FindControl( "Label3 ");
DropDownList ddl = (DropDownList)e.Row.FindControl( "DropDownList1 ");
lb.Text = ddl.SelectedItem.Text;

}

------解决方案--------------------
用e.Row.FindControl