2012-11-09 36 views
0

我曾經嘗試這樣做 -如何找到在FormView控件項模板控制

if (FormView1.CurrentMode == FormViewMode.ReadOnly) 
{ 
DropDownList ddlexpense = (DropDownList)FormView1.Row.FindControl("ddle"); 
} 

上FormView1數據綁定,初始化,ItemCreated和一個單獨的功能,但我總是得到同樣的錯誤 - System.NullReferenceException:對象未設置爲對象實例的引用

回答

0
var cnt = FindControl(formViewTemplate.Row, "controlName"); 
      var htmlControl = cnt as HtmlControl; 
      if (htmlControl != null) 
      {//---------------------} 
private Control FindControl(Control parent, string id) 
    { 
     foreach (Control child in parent.Controls) 
     { 
      string childId = string.Empty; 
      if (child.ID != null) 
      { 
       childId = child.ID; 
      } 
      if (childId.ToLower() == id.ToLower()) 
      { 
       return child; 
      } 
      if (child.HasControls()) 
      { 
       Control response = FindControl(child, id); 
       if (response != null) 
        return response; 
      } 
     } 

     return null; 
    }