1

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).

2
  • Have you tried using <asp:UpdatePanel>?
    – user2536
    Commented Jan 4, 2012 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
    Commented Jan 4, 2012 at 12:57

2 Answers 2

3

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 : https://stackoverflow.com/questions/553073/adding-controls-dynamically-to-an-updatepanel-in-asp-net-ajax

1
  • 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
    Commented Jan 5, 2012 at 12:57
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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