My First MSDN Article

September 27, 2004 | Uncategorized | 11 Comments

A while back I posted some code that handles form binding of objects. Since then, I recoded the whole thing and wrote an article about it for MSDN.  Here it is: http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/aspformbinding.asp

11 responses to “My First MSDN Article”

  1. Unresolvable says:

    Any example code on how to use the form binding objects?

    Many thanks

    Cliff

  2. Unresolvable says:

    Cliff,

    Were there no examples in the download?

    JD

  3. Unresolvable says:

    It Would be awesome with some Sample Code. :)

  4. Unresolvable says:

    Do you have sample to bind DataRow to Controls ?

  5. Unresolvable says:

    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

  6. Unresolvable says:

    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

  7. Unresolvable says:

    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;

    }

    }

  8. Unresolvable says:

    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…

  9. Unresolvable says:

    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!

  10. Unresolvable says:

    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.

  11. Unresolvable says:

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

Leave a Reply

Hi, I'm John Dyer. In my day job, I build websites and create online seminary software for a seminary in Dallas. I also like to release open source tools including a pretty popular HTML5 video player and build tools that help people find best bible commentaries and do bible study. And just for fun, I also wrote a book on the theology of technology and media.

Fork me on GitHub

Social Widgets powered by AB-WebLog.com.