msdn上有篇文章<<使用反射将业务对象绑定到 ASP.NET 窗体控件>>我用作者提供的代码不成功,不知何解?Document document =  Documents.GetDocument(Request.QueryString["DocumentID"]);FormBinding.BindObjectToControls(document,Page);
public void BindObjectToControls(object obj, Control container) 
{
if (obj == null) return;

// Get the properties of the business object
//
Type objType = obj.GetType();
PropertyInfo[] objPropertiesArray = objType.GetProperties(); foreach (PropertyInfo objProperty in objPropertiesArray) 
{ Control control = container.FindControl(objProperty.Name);

// handle ListControls (DropDownList, CheckBoxList, RadioButtonList)
//
if (control is ListControl) 
{
ListControl listControl = (ListControl) control;
string propertyValue = objProperty.GetValue(obj, null).ToString();
ListItem listItem = listControl.Items.FindByValue(propertyValue);
if (listItem != null) listItem.Selected = true;


else 
{
// get the properties of the control
//
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties(); // test for common properties
//
bool success = false;
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool) );

if (!success) 
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime) ); if (!success) 
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String) );

if (!success) 
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String) );

}
}
}

解决方案 »

  1.   

    using System.Reflection; //引用这个命名空间
      

  2.   

    “/”应用程序中的服务器错误。
    --------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。  堆栈跟踪: 
    [NullReferenceException: 未将对象引用设置到对象的实例。]
       GX3Y.Web.Fuctions.BindObjectToControls(Object obj, Control container) +215
       GX3Y.Web.test.ly.WebForm1.Page_Load(Object sender, EventArgs e) +57
       System.Web.UI.Control.OnLoad(EventArgs e) +67
       System.Web.UI.Control.LoadRecursive() +35
       System.Web.UI.Page.ProcessRequestMain() +750