Professional Web Applications Themes

Dynamically create controls with non-hierarchical IDs - ASP.NET Building Controls

You could add a Name attribute to the control and use GetElementsByName to find it. Dale "Simon Wallis" <SimonWallisdiscussions.microsoft.com> wrote in message news:6F76A711-7FF3-4047-A4CB-03D02533FB78microsoft.com... > I have a PlaceHolder inside a repeater so that I can dynamically add controls to my page. Each time the repeater.ItemDataBound event fires I create a Label and add it to the PlaceHolder. > > Now, when I create this Label, I set the ID to have the dynamic value: > "lblSubTotal_" + strCurrencyCode; > > ASP.NET will turn this Label into a [span] tag in the resulting HTML output, and I want to manipulate this ...

  1. #1

    Default Re: Dynamically create controls with non-hierarchical IDs

    You could add a Name attribute to the control and use GetElementsByName to
    find it.

    Dale


    "Simon Wallis" <SimonWallisdiscussions.microsoft.com> wrote in message
    news:6F76A711-7FF3-4047-A4CB-03D02533FB78microsoft.com...
    > I have a PlaceHolder inside a repeater so that I can dynamically add
    controls to my page. Each time the repeater.ItemDataBound event fires I
    create a Label and add it to the PlaceHolder.
    >
    > Now, when I create this Label, I set the ID to have the dynamic value:
    > "lblSubTotal_" + strCurrencyCode;
    >
    > ASP.NET will turn this Label into a [span] tag in the resulting HTML
    output, and I want to manipulate this tag using Javascript. In JS I want to
    get a reference to the tag via its ID value. For example:
    > objCurrencySubtotal = doent.getElementById("lblSubTotal_USD");
    >
    > The problem is that when ASP.NET creates the [span] tag, it modifies the
    ID value by prepending a whole bunch of hierarchically-qualified info. For
    example, this is the span tag I end up with in the HTML output:
    > <span
    id="pbdTemplate__PageTemplate_innerHolder_ctrlBala nces_rptrAggregateTotals__
    ctl1_lblSubTotal_USD">
    >
    > I cannot find this easily using Javascript. Does anyone know how to stop
    ASP.NET from prepending all that hierchical info and force it to use the ID
    value I myself assign to the control?
    >
    > Thanks,
    > Simon.

    Dale Guest

  2. #2

    Default Re: Dynamically create controls with non-hierarchical IDs

    "Simon Wallis" <SimonWallisdiscussions.microsoft.com> wrote in message
    news:6F76A711-7FF3-4047-A4CB-03D02533FB78microsoft.com...
    > I have a PlaceHolder inside a repeater so that I can dynamically add
    controls to my page. Each time the repeater.ItemDataBound event fires I
    create a Label and add it to the PlaceHolder.
    >
    > Now, when I create this Label, I set the ID to have the dynamic value:
    > "lblSubTotal_" + strCurrencyCode;
    >
    > ASP.NET will turn this Label into a [span] tag in the resulting HTML
    output, and I want to manipulate this tag using Javascript. In JS I want to
    get a reference to the tag via its ID value. For example:
    > objCurrencySubtotal = doent.getElementById("lblSubTotal_USD");
    >
    > The problem is that when ASP.NET creates the [span] tag, it modifies the
    ID value by prepending a whole bunch of hierarchically-qualified info. For
    example, this is the span tag I end up with in the HTML output:
    > <span
    id="pbdTemplate__PageTemplate_innerHolder_ctrlBala nces_rptrAggregateTotals__
    ctl1_lblSubTotal_USD">
    >
    > I cannot find this easily using Javascript. Does anyone know how to stop
    ASP.NET from prepending all that hierchical info and force it to use the ID
    value I myself assign to the control?

    Simon,

    Another solution is to pass the ID to the JavaScript code. For instance, if
    you need to call your JavaScript on a Click event:

    lblClickMe.Attributes.Add("onclick",
    string.Format("MyJSFunction('{0}');", myLabel.UniqueID));

    MyJSFunction will then be able to get the element using getElementById.
    --
    John Saunders
    johnwsaundersiii at hotmail


    John Saunders Guest

Similar Threads

  1. Create User Controls dynamically
    By David Lozzi in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: October 19th, 08:26 AM
  2. How do you create a drop-down hierarchical Data Grid?
    By Anonieko Ramos in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: November 14th, 11:09 AM
  3. Template controls and hierarchical data
    By bclegg in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: February 7th, 01:23 PM
  4. Dynamically create controls from a network directory tree
    By Jereme in forum ASP.NET Building Controls
    Replies: 0
    Last Post: September 26th, 03:41 PM
  5. How do I dynamically create user controls?
    By David J. Berman in forum ASP.NET Building Controls
    Replies: 2
    Last Post: June 30th, 07:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139