Issue with generating webcontrol dynamically...

S

scsharma

We have 508 compliance issues where every <input> tag should have associated
label. The label have to speicfy the control that it is associated to, in
this case the checkbox. This is done by setting the associatedControlID
property of label.
I am using data grid and i want to have a checkbox as one of the column.
For that i am creating a template class and in InstantiateIn(Control
container) function
i am dynamically creating a check box and adding that check box to control
collection.
Since the checkbox generates <input> tag I would like to have corresponding
label so that
i can set the associatedControlID property of the label to checkbox. But
when final
HTML is generated instead of <label> tag I am seeing <span> tag.
What am i doing wrong? How can i generate the <label> tag. Please help.

Here is the code:

public void InstantiateIn(Control container)
{
CheckBox cbx = new CheckBox();
Label lbl = new Label();
cbx.DataBinding +=new EventHandler(cbx_DataBinding);
lbl.DataBinding+=new EventHandler(lbl_DataBinding);
if (staticText !="")
lbl.AssociatedControlID = cbx.ID;
container.Controls.Add(lbl);
container.Controls.Add(cbx);
}

Thanks
sharmasu
 
S

Steven Cheng[MSFT]

Hi sharmasu,

Thanks for your posting. Regarding on this issue, I've also found another
former thread of yours in this newsgroup:

titled: Generate <Label> tag for input controls...

I've posted my reply there and I've pasted my reply here for your
convenience, if you have any further questions, please feel free to post
here:

====================================
Hi SCSharma,

Thanks for your posting. From your description, you're developing a certain
DataBound control's Template in which you'll dynamically create a checkbox
and a Label control and assign the checkbox.ID to the Label control's
AssociatedControlID property so as to output 508 compliant <label> , but
you found the output is always <span> rather than the expected <label> yes?

Based on the code snippet you provided,
====================
public void InstantiateIn(Control container)
{
CheckBox cbx = new CheckBox();
Label lbl = new Label();
cbx.DataBinding +=new EventHandler(cbx_DataBinding);
lbl.DataBinding+=new EventHandler(lbl_DataBinding);
if (staticText !="")
lbl.AssociatedControlID = cbx.ID;
container.Controls.Add(lbl);
container.Controls.Add(cbx);
}
====================

I think the problem is likely due to the ID of the cbx , as for a
dynamicaly created control, if we don't explicitly assign ID to it, it
will remain null(undefined) until it is added into a certain control's
Controls collection, (at that time, its "AutoGenerateID" method will be
called). So, in the code you used, when you assign id to the
lbl.AssociatedControlID , the cbx.ID is undefined, so this means the lbl
hasn't been associated with any control. To correct this , we have two
means:

1. Explicitly set the ID property after we construct the dynamic control

2. Access the new constructed control's ID after it has been added into a
certain parent control's Controls collection.

HTH.

Thanks & Regards,

Steven Cheng
Microsoft Online Support

==========================================================

Thanks & Regards,

Thanks & Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top