Thursday, March 22, 2012

HTML Control Name and Id modified of by .Net

Hello...

Can someone help me with this little problem?

I have a Master Page. And with it I did create a new aspx page.

Inside theasp:content of this new page I put a form and some code like this one:

<label for="Offices" runat="server">Office: <select id="Offices" runat="server" /></label>

In the PageLoad of the page I did fill the select. All is OK at moment.

The problem is that .Net changed the name and id of the Select.

The HTML generated is here:

<label for="Oficinas">Oficina:
<select name="ctl00$contenido$Oficinas" id="ctl00_contenido_Oficinas">
<option value="MAIN">MAIN</option>
<option value="OTHER">OTHER</option>
</select>
</label>

I want that .net change the attributeforof thelabeltoo. That automatically add the textctl00_contenido_to it.

How can i do that?

Thanks!

That's because you have <Label... not <asp:Label...

<Label isn't a valid anything......and it's not an Asp.Net control


Curt,

Actually, it shouldn't have to be an ASP Label control. You can add the runat="server" to the HTML label control.

gporras,

you need to add an ID property to the label. Then, you can manipulate the for property in your code behind. I would remove the for="Oficinas" from your markup, and then do something like this in your OnLoad event:

OfficesLabel.Attributes.Add("for",OfficesSelectControl.ClientId);

The trick is the "ClientId" property. The reason you are seeing "ct100_contenido_Oficinas", is because the framework has to generate a unique id for each control, so it does this by prefixing your control's id with the id(s) of the naming container(s). This id will then be stored in the "ClientId" property.

Cheers!

Matt


true... but then what's the point? There are FEW cases where you are better off trying to add server attributes to client controls.

And I'm still not seeing why the OP would even want this though... i have a feeling we're missing something...


Thanks Matt...I did as you told me and it works...

In tha page:

<label runat="server" id="CitiesLabel">

City: <select id="Cities" runat="server" />

</label>

And in the PageLoad:

CitiesLabel.Attributes.Add ( "for", Cities.ClientID ) ;

But I think .Net is working bad here... I told you about 1 label and select.. But in reality I have 15 labels in my page, and now i have to add 15 lines of code by a change that .Net did alone.

.Net should update my labels too... Not me...

Thanks anyway!!!


why are you using <label and not <asp:Label ?

If you need serverside intereaction you really should use the server controls


I tried the <asp:Label but it produces a <span> html label.. Not a <label> what is that I need!

The <asp:Label... don't produces the html <label...


Curt,

Using the attribute runat="server", he has made the <label> a "server control." Perhaps he doesn't like that an asp:Label renders as a <span> and doesn't feel like writing a control adapter.

Matt


Matt-dot-net:

Using the attribute runat="server", he has made the <label> a "server control." Perhaps he doesn't like that an asp:Label renders as a <span> and doesn't feel like writing a control adapter.

In the basest technical terms yes... but its about the same as putting a porche body on your pinto... sure it will pass as one but it's what's under it that matters.

Either way... if that's what he wants to do and it works for him then c'est la vie... it's his choice, he'll have to support it down the road.


Curt_C:

In the basest technical terms yes... but its about the same as putting a porche body on your pinto... sure it will pass as one but it'swhat's under it that matters.

Ok, Curt, I'll bite.

Would you please explain "what's under it" that I am missing?

Thanks,

Matt


predefined events for starters, not to mention they are inherited from different bases. All the html controls are lumped together into one really...at least when you add a runat="server" to them. You dont get the differentiated properties/methods, you dont get anything built in. You'd have to manually do everything.

While it's not bad when doing simple things it will really cause havok later when you start to doing anything more with the controls (from a serverside call).


Thanks, Curt! Generally speaking, I see your point. However, I have to disagree with your opinion that the OP will have problems down the road with this simple label. What server-side events could you possibly want to handle on a label? I'm somewhat of a minimalist and it seems "light-weght" to use something from the HtmlControls namespace as opposed to the WebControls. Anyway, thanks for the explanation and I'll relinquish this thread nowSmile

Matt


actually...I dont think that <label runat="server" is any "lighter weight" then an <asp:Label runat="server" since you now made it a server control, just an HtmlGeneric one instead of a proper Label one...

Like I said though, for simple things it is probably fine... it's just a bad habit to get into.

No comments:

Post a Comment