views:

34

answers:

1

Hi All,

I am trying to create a form that is made up of controls with values that represent an entity with multiple child entities.

The form will represent a product with multiple properties where the user will then be able to create options with multiple properties which in turn be able to create multiple option-items with multiple properties.

My question is what is the best approach? Can i use ajax to avoid postbacks and having to rewrite the controls to the page? If i dynamically add the controls in the form of table rows or grid rows will the data/control values be available in the code-behind when i submit?

This is an age old question.. the last time i had to do this was .Net 2.0, pre-ajax (for me) and i was forced to recreate all the controls on each post back. thanks!

A: 

If I'm understanding your question correctly, you want to dynamically change form elements depending on some kind of selection criteria without refreshing the page? Javascript can do this for you. If your elements rely on data from somewhere, such as options for a select menu, then yes you will have to use an ajax request to populate those. If they are static options, then pure javascript will work for you. And though the source won't indicate your generated elements, they will indeed be available for a submit. I hope this helps.

edl
if i generate controls with js, how can i then access them in code-behind?
bill
Sorry, I'm not familiar with the term "code-behind." But if you're asking how to select the data from your controls, there's a number of ways. A quick google search comes up with this thread:http://www.codingforums.com/showthread.php?t=103655Using "setAttribute" you can set the id attr so your server-side scripts can access it. Or use "value()" to get the data and use an ajax post.
edl
code-behind is a .Net term.. and this question is specific to .Net as the issue is with being able to grab control values from controls using ajax. I'm not sure if the viewstate on the page is properly updated or not so these controls (values) are available on post..
bill
sorry that should read:he issue is with being able to grab control values from controls that were added using ajax.
bill
Grabbing control values should be no problem, even if they were added using Javascript. For example, I can do something like this:document.write("<div id="added"></div>");and then:document.getElementById("added").innerHTML = "HELLO";is valid. The same goes for form elements such as textfields and select menus. Hope this helps.
edl
Yes i understand you can get the values using js. How you get them in "code-bhind" c# is the question? Are those controls available on POST in the file handling the request.
bill
Ah. Ok, I get it. Sorry for not spending more time to understand the question. My answer to that is "I don't think so." For example, if I were to use js to createElement("select"), that control will not be available on POST. if that select menu already existed on the page, and you were to use js to dynamically add the options to that select, those values would be available. So it depends on what kind of controls you need.
edl