Control ID

Discussion in 'ASP .Net Web Controls' started by MLibby, Dec 1, 2004.

  1. MLibby

    MLibby Guest

    My custom control's ID is automatically generated at design time when
    dragging the control onto the webform, however it is not generated when the
    control is programmatically created in the code behind. Is there a way for a
    control to automatically generate a unique id if one has not been assigned to
    it?

    For example...
    private void Page_Load(object sender, System.EventArgs e)
    {
    MyControl control = new MyControl();
    // the control.ID is not assigned. Is there a way for the control
    // to automatically generate the ID if not assigned???
    Form1.Controls.Add(control);
    }
    ....

    Mike



    Mike
    --
    mcp, mcse, mcsd, mcad.net, mcsd.net
    MLibby, Dec 1, 2004
    #1
    1. Advertising

  2. MLibby

    Viksi Guest

    Your control needs to implement the INamingContainer interface. The interface
    has no requirements, it just tells .NET to assign an ID to it.

    "MLibby" wrote:

    > My custom control's ID is automatically generated at design time when
    > dragging the control onto the webform, however it is not generated when the
    > control is programmatically created in the code behind. Is there a way for a
    > control to automatically generate a unique id if one has not been assigned to
    > it?
    >
    > For example...
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > MyControl control = new MyControl();
    > // the control.ID is not assigned. Is there a way for the control
    > // to automatically generate the ID if not assigned???
    > Form1.Controls.Add(control);
    > }
    > ...
    >
    > Mike
    >
    >
    >
    > Mike
    > --
    > mcp, mcse, mcsd, mcad.net, mcsd.net
    Viksi, Dec 1, 2004
    #2
    1. Advertising

  3. Hi Viksi,

    Thanks for your reply. But I'm afraid the INamingContainer won't work for
    this scenairo since it only ensure that all the sub controls in the
    Control(which implemeent the INamingContainer) will have a unique ID under
    that control's namespace. As for the control it self, it won't be assigned
    a ID automatically if we create it programly.

    Hi Mike,

    If you do need to provide a auto assigned "Default ID" for your custom
    controls, my suggestion is to assign the Control's ID property in its
    Construntor. For example:

    public MyControl()
    {
    this.ID = "ctrl" + DateTime.Now.Ticks;
    }

    And if we drag a control on form at design-time , the new assigned ID will
    override it. How do you think? If you have any other ideas ,please also
    feel free to post here. 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], Dec 3, 2004
    #3
  4. Try this:
    private void Page_Load(object sender, System.EventArgs e)
    {
    MyControl control = new MyControl();
    control.ID = "example";
    Form1.Controls.Add(control);
    }



    "MLibby" <> wrote in message
    news:...
    > My custom control's ID is automatically generated at design time when
    > dragging the control onto the webform, however it is not generated when
    > the
    > control is programmatically created in the code behind. Is there a way
    > for a
    > control to automatically generate a unique id if one has not been assigned
    > to
    > it?
    >
    > For example...
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > MyControl control = new MyControl();
    > // the control.ID is not assigned. Is there a way for the control
    > // to automatically generate the ID if not assigned???
    > Form1.Controls.Add(control);
    > }
    > ...
    >
    > Mike
    >
    >
    >
    > Mike
    > --
    > mcp, mcse, mcsd, mcad.net, mcsd.net
    news.microsoft.com, Dec 3, 2004
    #4
    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. Replies:
    0
    Views:
    709
  2. Matthew Louden
    Replies:
    1
    Views:
    6,894
    Scott M.
    Oct 11, 2003
  3. Mad Scientist Jr
    Replies:
    0
    Views:
    626
    Mad Scientist Jr
    Mar 22, 2006
  4. shapper
    Replies:
    6
    Views:
    943
    shapper
    Dec 26, 2007
  5. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    532
    Jonathan Wood
    Feb 2, 2010
Loading...

Share This Page