views:

306

answers:

6

OKay, I'm from a PHP background, but I've just been tasked with developing some custom Web Parts in SharePoint. I've figured out how to create and deploy a basic "Hello world" web part in VB. Okay so far.

What I'm stuck on is a really basic, stupid point - how the hell do I lay out things in a VB web part?

For an example, here's a label and a textbox:

protected overrides sub createchildcontrols()
mybase.createchildcontrols

dim mylabel as new label
dim mytextbox as new textbox

mylabel.text ="My label text"
mytextbox.text ="My textbox"

me.controls.add(mylabel)
me.controls.add(mytextbox)

How would I, for example, get mylabel and my textbox to appear on different lines, rather than running one after the other as they do now? In PHP I'd just wrap them in some top break them onto differnt lines, but how do I do it here?

+2  A: 

There are a number of ways to go about it. The easiest, if you really just want the controls to appear on different lines would be to add an ASP.net LiteralControl with a BR tag between them.

Aside from that, you can always use the ASP.net formatting controls, like Table to break your controls into sections for output.

Additionaly, everything that derives from WebControl has an Attribues and CssClass property for setting formatting based on style-sheets you can use.

The last method, and the most customizable, but hardest to maintain and change, would be to override the webpart's Render method and generate your HTML completely by hand in the WebPart.

Alternately, you could scrap this altogether, and employ the SmartPart to develop ASP.net user controls for use inside of SharePoint, giving you the options to use the Visual Studio designer tools to layout your controls on the form.

OedipusPrime
For now, you've saved my sanity just with the LiteralControl option. It's enough for me to immediately produce the needed web part.In the longer-term, as there are more complex Web Parts to build, I'll be trying out the SmartPart option.
SonniesEdge
+1  A: 

You should override the Render() method. By default this method just renders all the child controls you have added in the CreateChildControls() method, but overriding it lets you write additional HTML elements around the controls.

I usually code in C#, but I think the following example should work in VB:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
  writer.Write("<h1>Custom webpart rendering</h1>")
  me.mylabel.RenderControl(writer)
  writer.Write("<br />")
  me.myTextbox.RenderControl(writer)
End Sub

Give it a try...

Thomas Favrbo
+1  A: 

I've been developing web parts for an ASP.NET site using the standard web user control model, which gives you access to the VS designer and means your UI can be standard HTML. ASP.NET then wraps the UserControl into a GenericWebPart at runtime to host it in a WebParts site.

I know that Sharepoint doesn't support this model out of the box but I've just found this which might help you...

http://weblogs.asp.net/jan/archive/2006/12/02/announcing-the-return-of-the-smartpart.aspx

Marc
A: 

Smart Part (or a variation of it) is the easiest way to go. Why mess with rendering direct html when you can develop a user control more easily?

Plus if you are not an expert in VB, having Visual Studio Designer will help with creating user controls

Tundey
A: 

i blogged about this very topic. Easily build a rich UI for a web part without using SmartPart

Jason
A: 

Thanks for all the responses. I've gone with EvilGoatBobs solution as the most immediately easy to implement.

This is my first time on StackOverflow and your helpful answers have made it a really good introduction to the site! :)

SonniesEdge