Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 5th, 2005, 05:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
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).
  #2 (permalink)  
Old November 6th, 2005, 02:32 AM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Are you adding the checkbox to a datagrid or datarepeater?

  #3 (permalink)  
Old November 6th, 2005, 06:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
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.
  #4 (permalink)  
Old November 6th, 2005, 07:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
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.
  #5 (permalink)  
Old November 6th, 2005, 07:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:39 PM.


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