views:

486

answers:

4

I'm creating a SharePoint web part in C# that is using an UpdatePanel for some AJAX magic. Everything is working fine, but I'd like to know how to lay out my controls visually (without using SharePoint Designer). I just have two dropdownlists, some labels, a button, and a textbox. I am creating them within the overridden CreateChildControls. Thanks!

+1  A: 

there are a couple of ways you can lay them out. you can add a Table object and add rows, cells, etc. and add your controls to the cells.

Alternately, you can override the RenderContents method and output HTML directly to the write that is passed in as a parameter. If you do this method (its probably less work and more efficient then using the Table objects), you should use a StringBuilder to build your HTML then output the results to the writer. This method should gain you some performance.

Sadly, there is no visual WYSIWIG editor for this method.

Muad'Dib
Thanks for your reply. I decided to go ahead with using the Table class, as it will be quick, and I'm not worried much about browser standards compliance with SharePoint (considering that the default portal page currently throws 277 errors through the W3C Validator).
Geo Ego
A: 

Unfortunately, there is no visual designer for web parts that are created programmatically.

You can use a user control and the SmartPart web part from codeplex to gain advantage of the visual designer for .ascx user controls.

Kyle Trauberman
+2  A: 

add a container panel around your controls and give it a class. Add the panel to the UpdatePanel's container. Add all other controls to the new Panel's Controls.

You can now use css to do your styling, using the container panel's CssClass as reference.

in code:

protected override CreateChildControls()
{
  // .. creation of updatepanel, say upd1
  Panel container = new Panel{CssClass = "webpartContainer"};
  upd1.ContentTemplateContainer.Controls.Add(container);

  container.Controls.Add(dropdown1); // etc. etc.
}

The Css:

.webpartContainer
{
  /* if needed add some style here also */
}

.webpartContainer select
{
  /* add style */
}

.webpartContainer .specificClass
{
  /* give controls a class of their own in CreateChildControls 
  if controls of the same type need different styling
  (i.e. you have more than 1 select that need to look different)  */
}
Colin
Thanks very much for your answer. I really like the elegance of this solution and the fact that it avoids tables (my background is more web design than development, so tables = ick), but in this case I want to avoid the additional panel. I did upvote this, though, because I like it better as a solution overall; I just felt that the other way would work better for me in this particular situation.
Geo Ego
Good to see a developer who can see part of the value of the C in CSS :-D. Using 1 container as start element to base styles on, cascading / specifying from there, is unfortunately not a widely used practice among developers. God I loathe inline style attributes.... :-D.
Colin
It's too bad that these practices aren't more widespread. I think that the root of the problem is that developer/designer rift. You don't find many developers who mind inline style whatsoever, and designers don't generally have a deep enough back-end understanding to solve these kinds of problems properly. Like I said, I love this solution, and I'm definitely going to work with it so that I can incorporate it in the future, specifically when I am not working with SharePoint. Thanks again!
Geo Ego
A: 

You can use ASCX files in web parts.. just load it from your webpart class in CreateChildControls like so:

var control = Page.LoadControl("/_CONTROLTEMPLATES/yourproject/your.ascx");
Controls.Add(control);

This way you can use the normal way with Visual Studio to layout your webpart. Much nicer than building HTML in code which is a pain to say the least.

(this is also much better than using SmartPart which causes issues with the trustlevel and deployment)

ArjanP