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

When i use Controls they just appear in that order they are added, which can be a little bit difficult because sometimes there can be a lot of code between each Controls added.

I like visual webpart's but i do not think it is the best way to do webparts.

How can i design a webpart when using CreateChildControls?

share|improve this question
    
I would suggest you to extensively use Css classes in the controls you are building, and use some containers (divs) to facilitate the element selectors. –  Steve B Jan 21 '13 at 14:12

1 Answer 1

it depends on what your trying todo, unfortunatly c# is procedural so it goes down the tree and adds as it goes down.

What you can do is the brunt of the code first and adding the controls at the last stage.

To have controls before others or/and inbatween would require if functions, global varibles and gloabal objects. This will allow you to reuse the same object multiple times and to add them when you want.

How to set visual webparts to load one after - another sharepoint 2010

http://msdn.microsoft.com/en-us/library/dd583168(v=office.11).aspx

http://msdn.microsoft.com/en-us/library/dd583168(v=office.11).aspx#wssinsideoutch19_addinglayoutcontrolsandcontent

its all about thinking about the logic and applying to suit your needs :),

a if function within a forloop is what i use to recreate controls with unique id's and assign the text by sql DB table or from a list. So within the if function i display the title, outside of the if function is its subheaddings, the check im making with the if function is the title name is not the same ;) the adding of the control is in both the if and out of the if within the forloop so it would look like this:

Title Subtitle Subtitle

Title Subtitle Subtitle Subtitle Subtitle

Title Subtitle Subtitle

another trick if you dont like that method that i also use is to add through code some divs that wrap the code, within the divs is the controls. The divs obviously have their own id / class so you can rearange the order by css what is just as simple :)

this.Controls.Add(new LiteralControl("<div class='Div1'>"));

//do your control adding here

this.Controls.Add(new LiteralControl("</div>"));

hope it helps :)

share|improve this answer
1  
Correction here mate. The fact that web parts gets loaded in a specific way has got nothing to do with c#. ASP.Net and Sharepoint to some extent decides when and where to load controls. Anyways, to get to your answer, you can control the way controls are loaded by using the page LoadControl method. –  Fox Jan 21 '13 at 13:02
    
? i use this method at work and is one method to use! I use this method in enterprise with over 300 clients using the webpart so i dont understand how its not correct? No as i have given the first link that contains my answer already outlines that and the op said he doesnt want load control! go look up on msdn about webparts... there are pros and cons for both types of webpart and i do use both types depending on the needs/requirements –  ali Sharepoint Jan 21 '13 at 13:20
    
also like to add that he is talking about 1 webpart and the controls within them, he is also talking about the createchildcontrols and not avout asp.net and sharepoint specifically and how it renders the webpart itself but more on the order it goes through code. ;) –  ali Sharepoint Jan 21 '13 at 13:32

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.