Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. When to add javascript events

When to add javascript events

Scheduled Pinned Locked Moved ASP.NET
helpjavascripthtmlquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luke Murray
    wrote on last edited by
    #1

    Hi all, i'm trying to add a onclick and onmouseout event to some html in a custom DataGridColumn. The issue I get is that the javascript function I call need to reference the ClientID of another control in the DataGridColumn So something like public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType) { base.InitializeCell(cell, columnIndex, itemType); switch (itemType) { case ListItemType.Item: AddControls(cell); break; case ListItemType.AlternatingItem: AddControls(cell); break; } } private void AddControls(TableCell cell) { cell.Controls.Add(_contextMenu); HyperLink link = new HyperLink(); link.ImageUrl = _imageUrl; link.NavigateUrl = "javascript:void;"; link.Attributes.Add("onclick", "sampleFunc('" + _contextMenu.Controls[0].ClientID + "'); cell.Controls.Add(link); } The problem is that _contextMenu.Controls[0].ClientID will fail because the controls have not been created yet. as they get created in the CreateChildControls() method in ContextMenu. When can I get access to the ClientID of the child control and use add it to the link attributes? Thanks, Luke

    G 1 Reply Last reply
    0
    • L Luke Murray

      Hi all, i'm trying to add a onclick and onmouseout event to some html in a custom DataGridColumn. The issue I get is that the javascript function I call need to reference the ClientID of another control in the DataGridColumn So something like public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType) { base.InitializeCell(cell, columnIndex, itemType); switch (itemType) { case ListItemType.Item: AddControls(cell); break; case ListItemType.AlternatingItem: AddControls(cell); break; } } private void AddControls(TableCell cell) { cell.Controls.Add(_contextMenu); HyperLink link = new HyperLink(); link.ImageUrl = _imageUrl; link.NavigateUrl = "javascript:void;"; link.Attributes.Add("onclick", "sampleFunc('" + _contextMenu.Controls[0].ClientID + "'); cell.Controls.Add(link); } The problem is that _contextMenu.Controls[0].ClientID will fail because the controls have not been created yet. as they get created in the CreateChildControls() method in ContextMenu. When can I get access to the ClientID of the child control and use add it to the link attributes? Thanks, Luke

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Why not do it in the client script? link.Attributes.Add("onclick", "sampleFunc(document.getElementById('" + _contextMenu.ClientID + "').childNodes[0].id);"); --- b { font-weight: normal; }

      L 2 Replies Last reply
      0
      • G Guffa

        Why not do it in the client script? link.Attributes.Add("onclick", "sampleFunc(document.getElementById('" + _contextMenu.ClientID + "').childNodes[0].id);"); --- b { font-weight: normal; }

        L Offline
        L Offline
        Luke Murray
        wrote on last edited by
        #3

        Hmm, I will havea try, the thing is _contextMenu.ClientID never gets rendered in the HTML so the javascript will not be able to find it. ContextMenu renders a div tag full of spans. The div tag is the ContextMenu.Controls[0] and the spans are chilren of that div. So I need the div's id. I'll look into making the contextMenu.ClientID being the div itsself and the spans children of the ContextMenu. Any ideas? Thanks Luke

        1 Reply Last reply
        0
        • G Guffa

          Why not do it in the client script? link.Attributes.Add("onclick", "sampleFunc(document.getElementById('" + _contextMenu.ClientID + "').childNodes[0].id);"); --- b { font-weight: normal; }

          L Offline
          L Offline
          Luke Murray
          wrote on last edited by
          #4

          On that note... I now have the control rendering itself as a span and I can use ContextMenu.ClientID now. Though how do I make the control render as a div tag? Thanks, Luke

          L 1 Reply Last reply
          0
          • L Luke Murray

            On that note... I now have the control rendering itself as a span and I can use ContextMenu.ClientID now. Though how do I make the control render as a div tag? Thanks, Luke

            L Offline
            L Offline
            Luke Murray
            wrote on last edited by
            #5

            No my problem still is that the ClientID doesn't seem correct. If I use ContextMenu.ClientID I get onkeypress="sampleFunc('_ctl0') but the ContextMenu renders <span id="contactsGrid__ctl3_ContextMenuRoot" ... So it's like the Controls etc have not been created yet when I try to reference them. Any ideas anyone? I trying to have a column in a dataGrid that has a ContextMenu control that will popup when clicked. Thanks Luke

            G 1 Reply Last reply
            0
            • L Luke Murray

              No my problem still is that the ClientID doesn't seem correct. If I use ContextMenu.ClientID I get onkeypress="sampleFunc('_ctl0') but the ContextMenu renders <span id="contactsGrid__ctl3_ContextMenuRoot" ... So it's like the Controls etc have not been created yet when I try to reference them. Any ideas anyone? I trying to have a column in a dataGrid that has a ContextMenu control that will popup when clicked. Thanks Luke

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Why not just set an id on the control yourself? Then you know what id it has, and you don't have to try to get any automatically created id. --- b { font-weight: normal; }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups