Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you map HTML <label> to asp.net form controls 1

Status
Not open for further replies.

jupiter8

Programmer
Nov 22, 2002
72
GB
I am adding form elements into a placeholder and as such asp.net appends info to the ID of the form control as shown below.

<input name="ctl00$ContentPlaceHolder1$txt_3" type="text" value="some text" id="ctl00_ContentPlaceHolder1_txt_3" />

so when I add my HTML label, it isn't going to work because its only got the ID that I assigned to the textbox and not the additional info that asp.net adds

<label for='txt_3'>my text box</label>

To illustrate this further, if I add a checkobox control asp.net renders the label automatically and gets it right

<input id="ctl00_ContentPlaceHolder1_chk_2" type="checkbox" name="ctl00$ContentPlaceHolder1$chk_2" checked="checked" />

<label for="ctl00_ContentPlaceHolder1_chk_2">Checkbox label</label>

I need the labels because accessibility is essential for the project in hand.
 
Sorry just going back to the issue at hand, do you think that it is possible to change the order of the controls in the controls collection so that the label is rendered first.

I'm not bad with style sheets, but its tricky to do anything too adventurous layout wise when you are trying to reposition controls. I have something that works but can't help but wonder if there is a better way.
 
Well, yea. If you put in the code behind, then it doesnt matter what comes first or not because its kind of, for lack of a better word, compiled before its pushed to the browser.

so do this

--aspx--
<asp:textbox id=txtYo runat=server />
<label id=mylabel runat=server>Click Here</label>

--vb--
sub page_load(.....)
mylabel.attributes.add("for",txtYo.clientid)
end sub
 
Blimey I thought you had it there for a minute, but its slightly more complex because the form is being generated from a database so I don't know what form elements there will be and therefore need to do it all from the code behind.

So I tried doing what you have done here and creating a lable control and add the attribute which works, with one expection. The ASP.NET label controls renders as a span tag, if there was a control for the HTML <label> we'd be in business.
 
nooo....dont create an <asp:label, just do a <label tag and run that at the server. In addition to all the <asp: stuff, .net has controls for all the original html stuff, such as htmltablecell,htmltablerow, etc etc

generated from a database...going into a repeater and you need to do the label assignment there??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top