0

If you have multiple controls in your webpart how do you format them 'nicely' so they don't all appear in one horizontal line?

So you would have something like

  • CompanyName:
  • First Name:
  • Last Name:
  • Post code:
  • Email Address:

Is it going to have to be a table?

4 Answers 4

2

There is an article on A List Apart which gives an example of how to format a form without using tables

http://www.alistapart.com/articles/prettyaccessibleforms

There is also a discussion on Stack Overflow which give a few more examples. One point that was made that I tend to agree with is:

"If you don't use tables you need to know the width of your labels upfront. This can often be a problem for multi-language sites (i18n).

With tables, they stretch to fit labels of differing sizes. CSS alone can't do that yet in a well-supported way." -> https://stackoverflow.com/questions/591539/forms-can-they-be-done-without-tables

1

The same way you format any HTML inputs on any HTML page. The webpart simply renders the HTML as you define in the render event. Use tables, divs, css styles, like any other page.

1
  • Override the RenderWebPart event to output any html you desire, e.g.: protected override void RenderWebPart(HtmlTextWriter output) { output.write("<div class='emaildiv'>"); _lblEmail.RenderControl(output); _txtEmail.RenderControl(output); output.write("</div>"); }
    – Anonymous
    May 21, 2010 at 16:43
1

Simplest way is to add an additional LiteralControl like this in CreateChildControls:

LiteralControl lit = new LiteralControl("<br />");
this.Controls.Add(lit);

Put an instance like this between all your controls, but there are so many ways to achieve this and all of that is beyond the scope of this site as it is standard ASP.Net Web Control development.

-1

You can use Table as well as Div, SharePoint in general uses tables for most of it's built in webparts as they display the same no matter the browser.

Just be careful on how many tables you use, and nested tables are extremely bad also due to rendering times of the page.

Selecting the right container for the right situation is what should be done.

4
  • how would you use a DIV within code?
    – Rob
    May 21, 2010 at 12:11
  • 2
    I would not recommend this approach as you're hard coding tables for layout which is bad practice. Useful to know that a Panel control renders as a div if you need a div in your web part. May 21, 2010 at 18:47
  • 2
    Avoid using tables, use divs or ul/li and CSS formatting - it allows for better formatting. May 22, 2010 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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