Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is it possible to dynamically add controls to a webpart without having a postback for example with AJAX/Java Script/jQuery/Partial Postback within SharePoint? I'm searching for some kind of possibility/best practice.

The controls I'm talking about are dropdownlists/labels/textboxes.

Extra details: I'm developing a webpart for SP2010 (farm solution).

share|improve this question
    
Have you tried using <asp:UpdatePanel>? –  Daniel Ziga Jan 4 '12 at 12:34
    
So basically if I understand this UpdatePanel I can add any control to the panel with a trigger that is standing outside of the UpdatePanel? –  OG_Mike Jan 4 '12 at 12:57

2 Answers 2

up vote 3 down vote accepted

A webpart is a just a kind of asp.net control, nothing specific to SharePoint considering what you want to achieve.You can use javascript\jquery\ajax to add controls dynamically.

With javascript\jquery, you can add HTML controls.Check this : http://techbrij.com/445/add-dynamic-form-elements-textbox-button-radio-checkbox

With Ajax Updatepanel, you can add server side controls. Check this : http://stackoverflow.com/questions/553073/adding-controls-dynamically-to-an-updatepanel-in-asp-net-ajax

share|improve this answer
    
Ok so I managed to get the UpdatePanel kind of working. But I'm facing another problem here. I want to the controls to be added to my UpdatePanel after a button click event. This works fine although the previously entered data/values in the previously added controls go away after the click event. How can I fix this, do I need to trigger a postback event? If yes I tried to add a PostBackTrigger but I'm not able to have it working. –  OG_Mike Jan 5 '12 at 12:57

You can use jquery Ajax pretty easily and either have the response return some more html which you can add to an element on the page or build elements based on data returned.

The update panel is a simple way to implement ajax in ASP.NET but may be (depends on req) more difficult to add controls dynamically (you may end up using JS anyway.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.