checkboxes dynamicaly

G

Guest

When creating checkboxes dynamically, I assign the ID property as follows :
chkBox.ID ="myID", when runing the ASPX page I see a name attribute in the
input
*************************************
<input id="30R" type="checkbox" name="30R5149"........
**************************************
The object checkbox does not have "name" property. The 1st question, is it
possible to give ita name different fromthe id,if yes how ?
2nd question, I want to give the same name for several checkboxes in order
to, later, using javascript to loop this group of checkboxes. Just 10
mlinutes ago I posted another question related this issue

Thanks
 
W

Walter Wang [MSFT]

Hi,

To answer your question, we need to understand the relationship between
server-side ID, ClientID and client-side id, name attributes.

At server-side, we can only change the ID property, the ClientID is
readonly, and it will not necessary equal to the ID property. This is
because we can put child controls inside a container which implements
INamingContainer interface which is a marker interface. ASP.NET will
generate the ClientID based on which container the control belongs, so that
you only need to make sure the ID is unique in the same container, and
ASP.NET will generate ClientID that is unique in the whole page.

For client-side, the name attribute is only valid on the <form> and form
elements (<input>, <textarea>, and <select>). It is used to specify the
name to associate with the name/value pair that is submitted on a form post.

For example:

<input type=checkbox name=foo value=1>

If this checkbox is checked, it will submit foo=1. In the dom you can
reference form elements from the form.elements collection by specifying the
name as the index. If it's not unique, the collection returns an array of
elements rather than the element:

document.getElementsByName(nameValue)

Note: it always returns an array even if only one element is found.

Client-side id attribute is from the xml world, and is a unique id for any
node, not just form elements, and unlike the name attribute it is valid on
any html node. To find the element by id:

document.getElementById(idValue)

As for ASP.NET generated html, the id and name (if this is one of the three
tags that can submit data) attributes will always equal to server-side
ClientID, which is guaranteeded to be unique on the page. Also, the name
attribute is required to postback the data to server-side code so that
ASP.NET will know which server-side property is changed.

In a short, actually you can't control which name attribute at client-side
to user, it depends on your server-side ID property and which container the
control is in. If you change the name attribute and server-side code
doesn't have the knowledge of this, the postback data will be lost since
server-side code cann't map the postback data to correct properties.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Sorry my previous reply is not completely right.

At server-side we have three properties: ID, ClientID, UniqueID; the
ClientID and UniqueID is based on ID and generated by three parts:
* the container's according property (if the control is in a container)
* the ID
* the separator: ClientIDSeparator or IdSeparator:

#Control.ClientIDSeparator Property (System.Web.UI)
http://msdn2.microsoft.com/en-us/library/system.web.ui.control.clientidsepar
ator.aspx

#Control.IdSeparator Property (System.Web.UI)
http://msdn2.microsoft.com/en-us/library/system.web.ui.control.idseparator.a
spx

At client-side, the id attribute will use ClientID; the name attribute will
use UniqueID.

Note the separators are new in .NET 2.0, in .NET 1.1, the logic is the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top