tags:

views:

302

answers:

3

I need a help on this following aspx code

aspx Code:

<asp:Label ID ="lblName" runat ="server" Text ="Name"></asp:Label>
<asp:TextBox ID ="txtName" runat ="server"></asp:TextBox>

Consider this is my aspx page content. I am going to populate the values for textbox only after the postback from server. but the label also posting to the server(runat="server") eventhough its not neccessary. shall i write my code like this to save time from server with less load.

Corrected Code:

<label id ="lblNames">Name</label>
<asp:TextBox ID ="txtName" runat ="server"></asp:TextBox>

Only my server control will send to server for postback and not my html control which a static value.

Please suggest me whether this is the correct way of coding.

+3  A: 

If you take the runat='server' out of the <label> element then it won't be parsed as a server control. If you're not going to do anything with lblNames from the server then it is perfectly okay to leave it out.

Mark Cidade
A: 

If you're not doing anything with the label server-side, then just use a <span>. It'll end up as the same html at the browser.

Joel Coehoorn
Not true. asp.net label controls render as html elements.
DancesWithBamboo
html <label> elements that is.
DancesWithBamboo
I'll have to check, but I know from experience that at least it _used_ to be a <span>
Joel Coehoorn
A: 

.net label controls are rendered as html label elements and do not get posted back to the server. Labels just don't post back. The server control allows you to manipulate the properties of the control in code however which is very useful.

There is nothing wrong with using html tags as well in your aspx/ascx page though if you don't need any programmatic control of the element.

DancesWithBamboo