Rendered id attributeshttp://forums.asp.net/t/1020202.aspx/1?Rendered+id+attributesThu, 24 Aug 2006 20:23:38 -040010202021379137http://forums.asp.net/p/1020202/1379137.aspx/1?Rendered+id+attributesRendered id attributes I pasted an HTML form that was styled using CSS into a web form, with the intention of changing the tags to server controls so I could take advantage of the validation controls.<br> <br> As soon as I changed the form and one of the input tags to server controls and refreshed the browser that was displaying my page in good order my presentation collapsed. Some quick viewing of the source code identified the problem; my original id attributes that were being styled had been changed to some obscure value by .net's rendering of the controls.<br> <br> Surely there must be a simply way to prevent this but I cannot find one.<br> <br> Can anyone help?<br> <br> Bill Wallace<br> 2006-08-23T21:50:54-04:001379199http://forums.asp.net/p/1020202/1379199.aspx/1?Re+Rendered+id+attributesRe: Rendered id attributes <p>This is an interesting topic, Bill. Before diving into it, let's first establish that this isn't an issue that is caused by the use of adapters.&nbsp; If I understand what you described, you ran into a problem when you added runat=&quot;server&quot; to a &lt;form&gt; and an &lt;input&gt; tag.&nbsp; The ID that you had placed on those tags did not correspond to the ID produced in the new HTML produced by the server and sent to your browser.&nbsp; Right?&nbsp; And because the ID changed, the CSS rules that were using ID-based selectors no longer applied so the presentation was wrong.</p> <p>Do I have the situation spelled out correctly?</p> <p>If so, then I can say that the problem comes from the fact that, fundamentally, the ASP.NET framework normally takes charge of the ID value in the HTML that it renders.&nbsp; You, as the author, typically don't have the ability to directly set that HTML attribute.</p> <p>... unless you customize your site with something like an adapter that would&nbsp;adapt the &lt;input runat=&quot;server&quot; /&gt; tag such that it produced an ID value on the rendered client-side &lt;input&gt; tag that was more to your liking.</p> <p>Currently, the CSS Friendly ASP.NET 2.0 Control Adapter kit (<a href="http://www.asp.net/cssadapters">http://www.asp.net/cssadapters</a>) isn't offering a sample adapter for &lt;input runat=&quot;server&quot; /&gt; but you probably could experiment with making one for yourself. Actually, I would recommend that you adapt the System.Web.UI.WebControls.TextBox class and then use &lt;asp:TextBox runat=&quot;server&quot; /&gt; in places where you were thinking of using &lt;input runat=&quot;server&quot; /&gt;.&nbsp; If you do that you could write your own TextBox adapter and add it to your .browser file.</p> <p>Your adapter could then set the ID attribute in the HTML that it renders.</p> <p>Frankly, I don't know of any other way to directly gain control over the ID attribute.</p> <p>You might consider a totally different approach, though.&nbsp; In these situations it is sometimes much (MUCH) easier to simply alter your CSS rules so their selectors use classes rather than IDs.&nbsp; For example, you might have this today:</p> <p>#foobar { background: red; }</p> <p>This might correspond today to markup like this:</p> <p>&lt;input type=&quot;text&quot; id=&quot;foobar&quot; /&gt;</p> <p>If you want to switch this over to being a server-side &lt;input&gt; tag then you could do this:</p> <p>.foobar { background: red; }</p> <p>And alter the markup to be:</p> <p>&lt;asp:TextBox id=&quot;foobar&quot; CssClass=&quot;foobar&quot; runat=&quot;server&quot; /&gt;</p> <p>This TextBox should end up rendering an &lt;input class=&quot;foobar&quot; id=&quot;something_foobar&quot; /&gt;</p> <p>It doesn't matter with the ID is on this client-side &lt;input&gt; tag because your CSS isn't ID-based any longer.&nbsp; You've already changed your CSS rule's selector to refer to classes... which you can control in ASP.NET by using the CssClass attribute on any class (control) derived from WebControl.</p> <p>&nbsp;</p> 2006-08-23T23:18:29-04:001379815http://forums.asp.net/p/1020202/1379815.aspx/1?Re+Rendered+id+attributesRe: Rendered id attributes Thanks Russ you are spot on with the problem which leaves me amazed that we have no control over rendered IDs.<br> <br> I thought of changing the id to class but although I could do this for most IDs I do use some for hooks to snippets of unobtrusive JavaScript I use, which would break these processes.<br> <br> Talking of which, members of this forum have all seen the light regarding the separation of presentation from structure using CSS. Great! However, unobtrusive JavaScript does the same for behaviour as CSS does for presentation. The rendering of behaviour used by server controls produces loads of in-line and embedded JavaScript, especially ATLAS controls. We have a long way to go to overcome this but perhaps a simular approach as yours is possible.<br> <br> Anyways, I thought I would leave my form as is (Totally standards and unobtrusive), action an aspx to do the processing, and would you believe it, I cannot find a way to simply read a form field by name. Are we going backwards?<br> <br> The best I can do is run a loop to find out the index to each field and then access the fields I need by the index, so my interegation code might be:<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = 0 To Request.Form.Count - 1<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; itemName = Request.Form.AllKeys(i)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; itemValue = Request.Form.GetValues(i)(0)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r = r &amp; i &amp; &quot;.&quot; &amp; itemName &amp; &quot; : &quot; &amp; itemValue &amp; &quot;&lt;br /&gt;&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br> &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; lblOut.Text = r<br> and after copying and deleting the results for reference I could use code like:<br> <br> email = Request.Form.GetValues(10)(0)<br> <br> Once again, there must be an easier way!!?<br> <br> Bill<br> 2006-08-24T14:56:43-04:001379953http://forums.asp.net/p/1020202/1379953.aspx/1?Re+Rendered+id+attributesRe: Rendered id attributes <p>Form elements can be access by name using the name as the key like so:</p> <p>email = Request.Form[&quot;someName&quot;]</p> <p>where someName is the value of the NAME attribute on the client-side &lt;input&gt; tag in your form.</p> <p>Remember that you don't have to use Request.Form information to obtain the value of server-side controls like &lt;asp:TextBox runat=&quot;server&quot; id=&quot;foobar&quot; /&gt; that you have used in your form instead of client-side &lt;input&gt; tags.&nbsp; With server-side controls you simply use a syntax like:</p> <p>email = foobar.Text</p> <p>Hope this helps.</p> 2006-08-24T16:47:43-04:001380060http://forums.asp.net/p/1020202/1380060.aspx/1?Re+Rendered+id+attributesRe: Rendered id attributes That's the code I thought I should use but:<br> <br> email = Request.Form[&quot;email&quot;]<br> <br> produces this error:<br> <br> <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">BC30311: Value of type 'System.Collections.Specialized.NameValueCollection' cannot be converted to 'String'.<br> <br> Bill<br> <br> <br> </font> 2006-08-24T18:50:30-04:001380117http://forums.asp.net/p/1020202/1380117.aspx/1?Re+Rendered+id+attributesRe: Rendered id attributes <p>Sorry, I had a syntax error.&nbsp; This is VB.NET, right?&nbsp; So it should be:</p> <p>email = Request.Form(&quot;email&quot;)</p> <p>Don't use square brackets.&nbsp; Use parentheses.</p> 2006-08-24T19:41:30-04:001380157http://forums.asp.net/p/1020202/1380157.aspx/1?Re+Rendered+id+attributesRe: Rendered id attributes Cheers Russ<br> <br> I hang my head in shame<br> <br> Bill<br> 2006-08-24T20:23:38-04:00