两个DataList嵌套。有写绑定事件,在绑定事件里要访问里面的DataList里的控件Label(比如名称为:lblzhiwei).我为什么访问不到lblzhiwei。老是提示“未将对象引用设置到对象的实例”。
代码:protected void dtlParent_ItemDataBound44(object sender, DataListItemEventArgs e)
    {        int id = Convert.ToInt32(this.dtlzuixinzp.DataKeys[e.Item.ItemIndex].ToString());
        if (id != 0)
        {
            //string sql = "select zhiwei from company_zhiwei where  userid=" + id + "";
            string sql = "select distinct userid,danwei from company_zhiwei where  hangyeid=" + id + "";
            DataSet ds = Operation.GetDataSet(sql);
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
              
                DataList repBody = (DataList)e.Item.FindControl("repBody");                repBody.DataSource = ds;
                repBody.DataKeyField = "userid";
                repBody.DataBind();
                string idd = "";                if (ds.Tables[0].Rows.Count != 0)
                {
                   
                        for (int j = 0; j < repBody.Items.Count; j++)
                        {                           Label zhiwei = (Label)((DataList)e.Item.FindControl("repBody")).FindControl("lblzhiwei");
                           zhiwei.Text = "";
                        
                           for (int h = 0; h < ds.Tables[0].Rows.Count; h++)
                           {
                               string sql88 = "select zhiwei from company_zhiwei where userid=" + ds.Tables[0].Rows[h]["userid"].ToString() + "";
                               DataSet ds88 = Operation.GetDataSet(sql88);
                               if (ds88.Tables[0].Rows.Count != 0)
                               {
                                   zhiwei.Text += ds88.Tables[0].Rows[h]["zhiwei"].ToString() + "</br>";
                               }
                           }
                                                                    }
                   
                }
               
            }
                       
        }
    }

解决方案 »

  1.   

     Label zhiwei = (Label)((DataList)e.Item.FindControl("repBody")).FindControl("lblzhiwei"); 
    => Label zhiwei = repBody.Items[i].FindControl("lblzhiwei") as Label;if(zhiwei!=null)
    {
       //do sth
    }
      

  2.   

        Label zhiwei = (Label)((DataList)e.Item.FindControl("repBody")).FindControl("lblzhiwei"); 
    肯定找不到啊。Datalist中哪能直接找到控件啊。
    你应该给)((DataList)e.Item.FindControl("repBody"))传一个rowdatabound事件,在repBody这个datalist的rowdatabound事件的e.item.FindControl("lblzhiwei")可以找着
      

  3.   

    protected void dataList1_ItemDataBound(object sender, DataListItemEventArgs e) 
        { 
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
            { 
                string id = dataList.DataKeys[e.Item.ItemIndex].ToString(); 
                DataList  dl = (DataList)e.Item.FindControl("dataList2") 
                Label lbl= (Label)dl.Items[i].FindControl("lbl"); 
                
                string s= lbl.Text.ToString();         } 
        }