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) );
}
}
}
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) );
}
}
}
解决方案 »
- 怎样使asp.net 自带的日历控件显示指定的日期
- 关于在内容页中使用kindeditor
- 关于一控件的问题,谢谢!
- 在网站上怎么做一个翻书一样的效果?
- 后天要面视了,各位给小弟一点意见啊!
- 关于图象问题
- sqlserver 和 .net 做的网站多少连接算正常我们的网站怎么一会大一会小的
- 发布的问题,兄弟们来看~~~
- DataGrid排序出错,有谁能帮我???
- 如何把datagird里面的hyperlinkcolumn进行urlencode,把boundcolumn进行htmlencode?
- 如何实现在ASP。NET页面的BUTTON按钮C#代码,使它象WINFORM一样弹出对话窗(messagebox.show("title","message",buttonsytle,buttonIcon)
- 表格中的图片怎么实现自动换行?
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 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