views:

88

answers:

2

Currently I have a webform that has a series of links. What I'd like to be able to do is add a webcontrol's content when one of the links is clicked. Is this kind of thing even possible?

If not, what's the best strategy for loading a set of controls (one textbox and one dropdown with values from one link, two textboxes and a checkbox from another link, etc.). I'd need to be able get the values of each of these controls on postback.

Ideally, I'd like to be able to add that new content to an acordian control, most likely the jQuery UI acordian. So each clickable link would add new content to an acordian control.

What's everybody's thoughts on this?

A: 

Use ViewManager.RenderView demonstrated here:

http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx

Return the contents in a webservice and read with ajax.

Update

Roman Army, I see what you are saying. Chris, are you planning on posting back the data from the new content that was added? If so, this will not work.

Your best bet is to use the ASP.NET Ajax framework and the UpdatePanel.

Raj Kaimal
The limitation of that approach is that it doesn't update the ViewState so ASP.NET may rebel a little when Chris tries to submit data. You'll notice the demo is about rendering content, not submitting it back. Since then Microsoft has put a bit of work into client side templating.
R0MANARMY
**I'd need to be able get the values of each of these controls on postback.** - I'm gonna go with yeah, he's gonna need to post them back =).
R0MANARMY
yes, i would need access to the data in each of the new controls to persist to the database. I haven't tried it yet, but I'm assuming I should be able to do something like Request["idOfDynamicControl"] to get the data server-side.
Chris Conway
@Chris Conway: When you say "control" I assume you mean primitive html inputs, not asp.net user controls, right? If so then yes, they should be in the request collection.
R0MANARMY
yeah, i mean just any html control, not the asp.net control. i like the idea of the serialized json in a hidden input field and then retrieving and parsing on the server side. next week i'll give that a try and mark the question as answered when successful. Thanks!
Chris Conway
+2  A: 

Assuming this is indeed ASP.NET WebForms and you do plan to submit (fairly arbitrary) data using a postback, your best bet would probably be to use some sort of client side or server side templating as Raj mentioned.

Then before the form is submitted, you could use something similar to the approach described here (Serialize form to JSON with jQuery), save the dynamically created data into a hidden field, and use a JSON parsing library server side to parse the data and turn it into meaningful objects you can work with.


If it's at all an option, this kind of stuff is trivial to do using ASP.NET MVC. You could (fairly easily) splice it into an existing WebForms project and have the two live side by side. The main downside of it is that it will be a bit of a franken-project and people maintaining the project in the future would have some family unfriendly things to say about your design decision.

R0MANARMY
yeah, i would love to use mvc for this and you're right in that it would be simple to implement. unfortunately the constraint you mentioned is spot on in that we have a support team that will need to make heads and tails out of this when i'm done.thanks for the tip!
Chris Conway