When to add javascript events
-
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 theCreateChildControls()
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 -
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 theCreateChildControls()
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 -
Why not do it in the client script? link.Attributes.Add("onclick", "sampleFunc(document.getElementById('" + _contextMenu.ClientID + "').childNodes[0].id);"); --- b { font-weight: normal; }
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
-
Why not do it in the client script? link.Attributes.Add("onclick", "sampleFunc(document.getElementById('" + _contextMenu.ClientID + "').childNodes[0].id);"); --- b { font-weight: normal; }
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
-
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
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 -
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