Issue with generating webcontrol dynamically...

Discussion in 'ASP .Net Web Controls' started by scsharma, Feb 3, 2005.

  1. scsharma

    scsharma Guest

    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
     
    scsharma, Feb 3, 2005
    #1
    1. Advertising

  2. 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.)
     
    Steven Cheng[MSFT], Feb 4, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    742
  2. Luis Ramírez.

    Can a webcontrol (webcontrol.dll) have a configuration file?

    Luis Ramírez., Jul 5, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    208
    Luis Ramírez
    Jul 6, 2004
  3. Kasabaarde Sumta

    WebControl inside of WebControl and attaching events

    Kasabaarde Sumta, Dec 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    203
    Kasabaarde Sumta
    Dec 13, 2004
  4. Fabricio

    Custom webcontrol that references another webcontrol

    Fabricio, Sep 21, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    191
    Fabricio
    Sep 21, 2005
  5. Peter Morris [Air Software Ltd]

    WebControl with non-WebControl property

    Peter Morris [Air Software Ltd], Dec 7, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    142
    Peter Morris [Air Software Ltd]
    Dec 7, 2004
Loading...

Share This Page