My First MSDN Article

11 thoughts on “My First MSDN Article

  1. Hey,

    Wow! This is incredibly useful! Your article was posted on ASP.NET, and I checked it out. Saved me hundreds of lines of code. However, both your article and your code on MSDN have some bugs:

    In the 2nd code block on the article:

    FormBinding.BindObjectToControls(document);

    should read…

    FormBinding.BindObjectToControls(document, Page);

    Also, in the downloadable code, the FindAndGetControlProperty method is messed up. It’s also messed up in the article. The article version is mostly correct, except:

    if (controlPropertiesArray.Name == "Text" &&

    controlPropertiesArray.PropertyType == typeof(String)) {

    should be

    if (controlProperty.Name == "Text" &&

    controlProperty.PropertyType == typeof(String)) {

    Also, I took the liberty of creating a VB.net version (using one of the online conversion tools). I’d be happy to post it or email it if you are interested.

    Thanks again,

    Jason

  2. The line of code is actually more messed up than that. Both the expression in the IF statement and the loading of the form data into the object are incorrect. It should be:

    private static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)

    {

    // iterate through control properties

    //

    foreach (PropertyInfo controlProperty in controlPropertiesArray) {

    // check for matching name and type

    //

    if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)

    {

    // set the control’s property to the business object property value

    //

    controlProperty.SetValue(control, Convert.ChangeType( objProperty.GetValue(obj, null), type) , null);

    return true;

    }

    }

    return false;

    }

    I’m going to have a look at it as I’ve done something similar for the Data Access Layer using Reflection. Something I did was to use the Cache to store the PropertyInfo[] on the first call to the page, thereby reducing the performance hit due to Reflection.

    Where do you think I should post my resulting code?

    Matthew

  3. Sorry, I cut the wrong method ;-(

    private static bool FindAndGetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)

    {

    // iterate through control properties

    //

    foreach (PropertyInfo controlProperty in controlPropertiesArray)

    {

    // check for matching name and type

    //

    if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)

    {

    // set the control’s property to the business object property value

    //

    try

    {

    objProperty.SetValue(obj, Convert.ChangeType( controlProperty.GetValue(control, null), objProperty.PropertyType) , null);

    return true;

    }

    catch (){

    // the data from the form control could not be converted to objProperty.PropertyType

    //

    return false;

    }

    }

    }

    return false;

    }

    }

  4. Thanks for the comments guys. Sorry the MSDN code was wrong. I’ll post some updated code soon. I added support for Enum properties in the ListControl area…

  5. Thanks for the article, John!

    I’ve tryed the approach you described with business objects defined in web service. There is an issue caused by the way wsdl generates proxy classes: all the original properties will be serialized into fileds.

    Thus, i have a shy suggestion that using FieldInfo[] instead of PropertyInfo[] would be more common solution.

    Thank you again!

  6. Great Article. This is way neat! Thanks to Matthew for also providing a fix – I was looking around all day trying to figure out why I could bind an object to the controls, but not bind the controls back to the object.

  7. Nice article. I guess the code could also be extended to use custom property attributes for setting up validation controls etc.

Comments are closed.