How to control ASP.NET controls location in the page

What is the best way to control the web controls position in ASP.NET page?

Back in the early days in pure HTML I use to do it with Table.

Is this still the best way?

Thank you in advance,

Roy.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Tables or divs, same as any other HTML element. If you’re comfortable with table-based layout and the person for whom you’re making the web page doesn’t mind, use tables. Otherwise, use CSS, Divs, and all of the positioning stuff that goes along with that.

Method 2

Officially, tables are dead. Prefer CSS posistioning.
WC3 Schools has some nice material on how to use. They have an in-page editor so you can put in code and try it out before you use it in your page(s).

<myControl style="postion:absolute;left:80px;top:80px" />

Method 3

The modern approach is CSS positioning. Table v CSS is the type of question that everyone has an opinion on. You should research some of the pros and cons and come to your own conclusions.

To get you started:

If you’re interested in CSS, I’ve found Stylin’ With CSS to be useful.

Method 4

You can place a web control anywhere in the asp.net page inside of regular HTML.

If you are adding the web control from the code behind the best way to get it placed is with a place holder.

HTML side could be

<div>
    <asp:PlaceHolder runat="server" id="PlaceHolder1" />
</div>

And in the code behind

Control control = //Load or create the control
PlaceHolder1.Controls.Add(control);

Method 5

Purely speaking about HTML you have divs you can use for laying out the page. You should always prefer them instead of HTML tables and then do the layout by using CSS. This also from an accessibility point of view, since screen readers have some problems with tables.

The same holds ASP.net web controls. You can place them inside a div which is then positioned and styled with CSS accordingly. At runtime you can attach them to containers or placeholders like the Panel control or Placeholder control. Actually the Panel control renders as HTML div later. Many of these controls have the CssClass property which again lets you specify some CSS class(es).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x