Dynamically create controls with non-hierarchical IDs

Ask a Question related to ASP.NET Building Controls, Design and Development.

  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" <SimonWallis@discussions.microsoft.com> wrote in message
    news:6F76A711-7FF3-4047-A4CB-03D02533FB78@microsoft.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 = document.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. Similar Questions and Discussions

    1. Create User Controls dynamically
      Howdy, I have a user control that needs to be displayed X amount of times on one page. How is this done? I was thinking of putting it into a...
    2. How do you create a drop-down hierarchical Data Grid?
      Is there a way to dynamically create a drop-down hierarchical data grid? You may want to look at this reference. It has a 1-level hierarchy. ...
    3. Template controls and hierarchical data
      Hi, I have relational data contained in a dataset, say a widget table and a model table. Each widget has a model_id which relates to the primary...
    4. Dynamically create controls from a network directory tree
      I'm trying to create a search page with msIndex service and i want the user to be able to select the directories they would like to search within....
    5. How do I dynamically create user controls?
      Thanks for any help...! My error is: Object reference not set to an instance of an object. Here's the setup: I have made a user control...
  3. #2

    Default Re: Dynamically create controls with non-hierarchical IDs

    "Simon Wallis" <SimonWallis@discussions.microsoft.com> wrote in message
    news:6F76A711-7FF3-4047-A4CB-03D02533FB78@microsoft.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 = document.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

Posting Permissions

  • You may not post new threads
  • You may 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