Pre-Populating Data

Apr 7, 2010 at 4:25 PM
I thought something similar to question.InputHtmlAttributes.Add("Value", "xxx") ---- not the capital in Value ------ would pre-populate a textbox but it causes problems when you then change the value on screen and resubmit. It reloads the page with the original value not the new one. Looking at the Html generated, it looks like the problem is because there are two attributes "Value" and "value". The problem is that when I use question.InputHtmlAttributes.Add("value", "xxx") ----- note no capital for value --------- the pre-population does not work. Do you know how to get around this? Thanks
Apr 7, 2010 at 4:27 PM

I will experiment and let you know what to do.

Apr 7, 2010 at 4:52 PM

Ok, so the InputHtmlAttributes property isn't the place to set the value. When the a textbox or textarea field gets rendered, the RenderHTML method sets the value attribute (or InnerHTML in the case of the textarea) to the string that is stored in the Value property.

The Value property was marked as internal, so that's why you couldn't set it. There is no harm in changing it to public in the source. So, in the source code in Fields/Abstract/TextField.cs, change the defintion for the Value property to be public:

public string Value { get; set; }

Then in your calling code:

            var name = new TextBox
                Key = "name",
                ResponseTitle = "Name",
                Prompt = "Enter your full name:",
                DisplayOrder = 20,
                Required = true,
                RequiredMessage = "Your full name is required",
                Value = "CHONG"
This would set the default value of the textbox to "Chong" and allow the user to change it.

If you don't want to change the source yourself, here is the binary:

Apr 7, 2010 at 4:58 PM
You beauty, worked a treat.
Sep 23, 2010 at 12:20 PM

I also wanted to point out that, if you are using a version of the library where the Value property isn't exposed as a public member, you can also use reflection to set it:

inputField.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(inputField, "some value", null); 

Jul 14, 2011 at 3:03 PM

hi this is exatly what i needed. Also let me take the opportunity here to thank you for this great project.  We have forms stored in a table, and with little effort i can show the form for the user to fill in but also for the administrator (by using reflection) to modify it.

Jul 14, 2011 at 5:42 PM

Glad that the project has helped you and others. I wish that I had time to enhance the feature set.