Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 5th, 2005, 05:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default Odd attributes on Web- and Html Controls

Setting the ID and the CssClass on a dynamically add CheckBox web control renders the following HTML code...
Code:
<span class="myClass"><input id="_ctl0_myID" type="checkbox" name="_ctl0:myID" />
First of all I am surprised that the class is put on a span and not on the input. Why is that?

Moreover, setting the ID explicitly from the code-behind renders odd prefixes on both the id and name. What I wanted was to apply an ID, which I could count on when designing the style sheet, and then I would like the prefix to be gone. What is the solution to this?

When trying the same with the HtmlInputCheckBox (with the ID), it renders the same prefixes. Pretty odd, but there must be a natural explanation. Something about Viewstate?

Thanks, Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old November 6th, 2005, 02:32 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Are you adding the checkbox to a datagrid or datarepeater?

 
Old November 6th, 2005, 06:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, doing a very simple example renders the same span around the CheckBox, but the IDs are as I would expect. Simple setup...
Code:
<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
     <head>
         <
        <script runat="server">
            public void Page_Load(Object oSender, EventArgs eArg)
            {
                CheckBox box1 = new CheckBox();
                box1.ID = "myID2";
                box1.CssClass = "someclass";

                HtmlInputText box2 = new HtmlInputText(); 
                box2.ID = "myID1";

                test.Controls.Add(box1);
                test.Controls.Add(box2);
            }
        </script>
    </head>
    <body>
        <form id="test" method="post" action="default.aspx" runat="server"/>
    </body>
</html>
The span is still odd, but the IDs are fine. It renders (aligned nicer though)...
Code:
<form name="test" method="post" action="test.aspx" id="test">
    <input type="hidden" name="__VIEWSTATE" value="dDw5NTgyNDg3OTY7O2w8bXlJRDI7Pj4zqu8pPRyb4gWRfNvIozI77yvCaQ==" />
    <span class="someclass">
        <input id="myID2" type="checkbox" name="myID2" />
    </span>
    <input name="myID1" id="myID1" type="text" />
</form>
The problem now seems like it has to do with my structure. Basically I have a user control (.ascx), which are loaded dynamically, and in this there is a Label as a placeholder.

In code I make an instance of a class, which inherrits HtmlForm. The CheckBox in the first post was added to this class using the Controls collection of the base class HtmlForm. The class which inherrits HtmlForm is then added to the placeholder. This seems to work fine; i.e. login UI etc. works fine; i.e. except from the odd labeling and CSS classes.

I am not using DataGrid or DataAdapter. Simply adding dynamically instantiated CheckBox controls to an instance of a HtmlForm, and then adding it to the page.

Thanks, Jacob.
 
Old November 6th, 2005, 07:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, this is pretty strange. Never seen anything like this. I though that it could have something to do with uniquely identifying the elements, and therefore I tried to add this line...
Code:
this.Name = "myForm";
... in the HtmlForm class, and then suddenly all _ctl0_ is replaced with test. I would at least expect _ctl0_ to be replaced by myForm.

Moreover, it is not only the elements within the form which has been prefixed. It is also the other elements, which I have included in the original .ascx control. Thats strange.

Jacob.
 
Old November 6th, 2005, 07:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Alright, found the answer and not so strange after all.

The problem was that the ID of the .ascx control in the first post was nothing and in the third post I had changed it to... well yes, test.

I guess that since it is a dynamically loaded user control, the framework wants to be sure that the IDs of the elements do not collide with others also dynamically loaded, and therefore it prefixes the controls with the ID of the control itself. If not specified with _ctl0.

Thanks you brough me in the right direction.
Jacob.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rather odd web-project oldmainframehack Javascript How-To 0 August 8th, 2007 09:03 AM
Using xml elements as html attributes chipmaster XSLT 4 June 26th, 2007 10:22 AM
need html attribute based on xml attributes charles95621 XSLT 1 May 24th, 2007 05:21 PM
Use of HTML controls in a web page anujrathi ASP.NET 1.0 and 1.1 Basics 1 June 19th, 2006 07:51 PM
HTML controls in web page anujrathi ASP.NET 1.0 and 1.1 Professional 1 June 18th, 2006 10:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.