472,143 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

Adding javascript event to datagrid control

Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!
Nov 18 '05 #1
19 2362
This should be fine. Where is the datagrid mentioned in the subject?

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!

Nov 18 '05 #2
Hi,

You can directly add the onClick attribute to the checkbox as in
<asp:CheckBox onClick="doJavascriptThing()" runat=server>

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!

Nov 18 '05 #3
This will add a server-side event. If a client-side one is needed,
Attributes is the way to go.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
Hi,

You can directly add the onClick attribute to the checkbox as in
<asp:CheckBox onClick="doJavascriptThing()" runat=server>

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!

Nov 18 '05 #4
Hi Eliyahu,

For a CheckBox server control, onClick attribute hooks a client-side JS
event handler.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:ua**************@TK2MSFTNGP09.phx.gbl...
This will add a server-side event. If a client-side one is needed,
Attributes is the way to go.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
Hi,

You can directly add the onClick attribute to the checkbox as in
<asp:CheckBox onClick="doJavascriptThing()" runat=server>

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!


Nov 18 '05 #5
What do you mean? Where will doJavascriptThing() run? In your example it
will run on server after a postback. By the way, there is no server event
OnClick for CheckBox control in the first place.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
Hi Eliyahu,

For a CheckBox server control, onClick attribute hooks a client-side JS
event handler.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:ua**************@TK2MSFTNGP09.phx.gbl...
This will add a server-side event. If a client-side one is needed,
Attributes is the way to go.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
Hi,

You can directly add the onClick attribute to the checkbox as in
<asp:CheckBox onClick="doJavascriptThing()" runat=server>

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!


Nov 18 '05 #6
onClick event-handler for a server-side CheckBox runs on the client-side
(yes, you are right that check boxes do not have server-side Click event).
As I understand from the original post, the requirement is to run a JS
script on click of a checkbox; so onClick can be used.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:OC**************@TK2MSFTNGP09.phx.gbl...
What do you mean? Where will doJavascriptThing() run? In your example it
will run on server after a postback. By the way, there is no server event
OnClick for CheckBox control in the first place.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
Hi Eliyahu,

For a CheckBox server control, onClick attribute hooks a client-side JS
event handler.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:ua**************@TK2MSFTNGP09.phx.gbl...
This will add a server-side event. If a client-side one is needed,
Attributes is the way to go.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
Hi,

You can directly add the onClick attribute to the checkbox as in
<asp:CheckBox onClick="doJavascriptThing()" runat=server>

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!



Nov 18 '05 #7
"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:up**************@tk2msftngp13.phx.gbl...
This should be fine. Where is the datagrid mentioned in the subject?

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!


Sorry forgot to specifically mention the Datagrid...
Simply adding 'onClick="doJavascriptThing()"' ends up with '<span
onClick="doJavascriptThing()"><input type="checkbox"...' which is not very
useful.
Whenever I put 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")'
in onLoad-event it throws an 'Object reference not set to an instance of an
object'.

Design-code:
<asp:TemplateColumn HeaderText="Mail">
<ItemTemplate>
<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail")
%>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

Code-behind:
Protected WithEvents cbDoMail As System.Web.UI.WebControls.CheckBox

Private Sub Page_Load(......)
cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
....

Hope this this is more clear...
Nov 18 '05 #8
Hi,

Change the checkbox markup in the template column as:

<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail")
%>' onClick='alert("Clicked");'></asp:CheckBox>

I have put alert() just for demo purpose. Of course, you can have your own
JS script here.

Another option is to handle the ItemDataBound event of the grid: Get the
reference to the checkbox control in the row being bound, and do
<chkbox>.Attributes.Add(..)

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:up**************@tk2msftngp13.phx.gbl...
This should be fine. Where is the datagrid mentioned in the subject?

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!


Sorry forgot to specifically mention the Datagrid...
Simply adding 'onClick="doJavascriptThing()"' ends up with '<span
onClick="doJavascriptThing()"><input type="checkbox"...' which is not very
useful.
Whenever I put 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")'
in onLoad-event it throws an 'Object reference not set to an instance of an
object'.

Design-code:
<asp:TemplateColumn HeaderText="Mail">
<ItemTemplate>
<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail")
%>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

Code-behind:
Protected WithEvents cbDoMail As System.Web.UI.WebControls.CheckBox

Private Sub Page_Load(......)
cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
....

Hope this this is more clear...

Nov 18 '05 #9
You forgot to mention the most important thing! That is that the whole story
is taking place in a TemplateColumn.
The solution is simple. Just do cbDoMail.Attributes.Add("onClick",
"doJavascriptThing()") in ItemDataBound event handler rather than in the
PageLoad one. On page load stage there are no items. They are created and
populated on databind stage.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:up**************@tk2msftngp13.phx.gbl...
This should be fine. Where is the datagrid mentioned in the subject?

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()") would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!

Sorry forgot to specifically mention the Datagrid...
Simply adding 'onClick="doJavascriptThing()"' ends up with '<span
onClick="doJavascriptThing()"><input type="checkbox"...' which is not very
useful.
Whenever I put 'cbDoMail.Attributes.Add("onClick",

"doJavascriptThing()")' in onLoad-event it throws an 'Object reference not set to an instance of an object'.

Design-code:
<asp:TemplateColumn HeaderText="Mail">
<ItemTemplate>
<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail")
%>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

Code-behind:
Protected WithEvents cbDoMail As System.Web.UI.WebControls.CheckBox

Private Sub Page_Load(......)
cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
...

Hope this this is more clear...

Nov 18 '05 #10
I tried:

Private Sub gridItems_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
gridItems.ItemDataBound
Dim cb As CheckBox
cb = e.Item.FindControl("cbDoMail")
cb.Attributes.Add("onClick", "doJavascriptThing()")
End Sub

Result: 'Object reference not set to an instance of an object'.

Since this is my first ItemDataBound event-handling, I might be doing
something wrong...
"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:%2****************@TK2MSFTNGP11.phx.gbl...
You forgot to mention the most important thing! That is that the whole story is taking place in a TemplateColumn.
The solution is simple. Just do cbDoMail.Attributes.Add("onClick",
"doJavascriptThing()") in ItemDataBound event handler rather than in the
PageLoad one. On page load stage there are no items. They are created and
populated on databind stage.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:up**************@tk2msftngp13.phx.gbl...
This should be fine. Where is the datagrid mentioned in the subject?

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> Hello,
> I need to add a Javascript event (onClick) to a servercontrol
> (asp:checkbox). Checkbox is named 'cbDoMail'.
> Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()") > would do the job, unfortunately it didn't.
>
> Can anyone point me in the right direction?
>
> Thanks!
>


Sorry forgot to specifically mention the Datagrid...
Simply adding 'onClick="doJavascriptThing()"' ends up with '<span
onClick="doJavascriptThing()"><input type="checkbox"...' which is not very useful.
Whenever I put 'cbDoMail.Attributes.Add("onClick",

"doJavascriptThing()")'
in onLoad-event it throws an 'Object reference not set to an instance of

an
object'.

Design-code:
<asp:TemplateColumn HeaderText="Mail">
<ItemTemplate>
<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail") %>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

Code-behind:
Protected WithEvents cbDoMail As System.Web.UI.WebControls.CheckBox

Private Sub Page_Load(......)
cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
...

Hope this this is more clear...


Nov 18 '05 #11
"Shiva" <sh******@online.excite.com> schreef in bericht
news:uo*************@TK2MSFTNGP12.phx.gbl...
Hi,

Change the checkbox markup in the template column as:

<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail")
%>' onClick='alert("Clicked");'></asp:CheckBox>

I have put alert() just for demo purpose. Of course, you can have your own
JS script here.


This DID do the trick... apparently the single-quotes made sense to the
compiler, double quotes didn't
Thanks!
Nov 18 '05 #12
You need to ignore items of types Header, Footer and Pager. The reason for
the message is that the first item is the header, and there is no checkbox
over there.

Also, item represents a row and your checkbox is in a cell. Therefore, you
should use e.Item.Cell{index).FindControl("cbDoMail"), where index is the
index of the TemplateColumn with the checkbox.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
I tried:

Private Sub gridItems_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
gridItems.ItemDataBound
Dim cb As CheckBox
cb = e.Item.FindControl("cbDoMail")
cb.Attributes.Add("onClick", "doJavascriptThing()")
End Sub

Result: 'Object reference not set to an instance of an object'.

Since this is my first ItemDataBound event-handling, I might be doing
something wrong...
"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:%2****************@TK2MSFTNGP11.phx.gbl...
You forgot to mention the most important thing! That is that the whole

story
is taking place in a TemplateColumn.
The solution is simple. Just do cbDoMail.Attributes.Add("onClick",
"doJavascriptThing()") in ItemDataBound event handler rather than in the
PageLoad one. On page load stage there are no items. They are created and
populated on databind stage.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:up**************@tk2msftngp13.phx.gbl...
> This should be fine. Where is the datagrid mentioned in the subject?
>
> Eliyahu
>
> "Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
> news:%2****************@TK2MSFTNGP12.phx.gbl...
> > Hello,
> > I need to add a Javascript event (onClick) to a servercontrol
> > (asp:checkbox). Checkbox is named 'cbDoMail'.
> > Assumed that 'cbDoMail.Attributes.Add("onClick",

"doJavascriptThing()")
> > would do the job, unfortunately it didn't.
> >
> > Can anyone point me in the right direction?
> >
> > Thanks!
> >

Sorry forgot to specifically mention the Datagrid...
Simply adding 'onClick="doJavascriptThing()"' ends up with '<span
onClick="doJavascriptThing()"><input type="checkbox"...' which is not very useful.
Whenever I put 'cbDoMail.Attributes.Add("onClick",

"doJavascriptThing()")'
in onLoad-event it throws an 'Object reference not set to an instance
of an
object'.

Design-code:
<asp:TemplateColumn HeaderText="Mail">
<ItemTemplate>
<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,

"DoMail") %>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

Code-behind:
Protected WithEvents cbDoMail As System.Web.UI.WebControls.CheckBox

Private Sub Page_Load(......)
cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
...

Hope this this is more clear...



Nov 18 '05 #13
I am very surprised. Is that a sort of hacking thing? OnClick is NOT
documented for checkbox.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:uz*************@TK2MSFTNGP09.phx.gbl...
"Shiva" <sh******@online.excite.com> schreef in bericht
news:uo*************@TK2MSFTNGP12.phx.gbl...
Hi,

Change the checkbox markup in the template column as:

<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail") %>' onClick='alert("Clicked");'></asp:CheckBox>

I have put alert() just for demo purpose. Of course, you can have your own JS script here.


This DID do the trick... apparently the single-quotes made sense to the
compiler, double quotes didn't
Thanks!

Nov 18 '05 #14
No hacking! It is not in the documentation may be because its a client-side
event.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:#b**************@tk2msftngp13.phx.gbl...
I am very surprised. Is that a sort of hacking thing? OnClick is NOT
documented for checkbox.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:uz*************@TK2MSFTNGP09.phx.gbl...
"Shiva" <sh******@online.excite.com> schreef in bericht
news:uo*************@TK2MSFTNGP12.phx.gbl...
Hi,

Change the checkbox markup in the template column as:

<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail") %>' onClick='alert("Clicked");'></asp:CheckBox>

I have put alert() just for demo purpose. Of course, you can have your own JS script here.


This DID do the trick... apparently the single-quotes made sense to the
compiler, double quotes didn't
Thanks!


Nov 18 '05 #15
Not sure, maybe it just adds all unknown parameters to the input-tag.
Would cover all future tags...
"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:%2****************@tk2msftngp13.phx.gbl...
I am very surprised. Is that a sort of hacking thing? OnClick is NOT
documented for checkbox.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:uz*************@TK2MSFTNGP09.phx.gbl...
"Shiva" <sh******@online.excite.com> schreef in bericht
news:uo*************@TK2MSFTNGP12.phx.gbl...
Hi,

Change the checkbox markup in the template column as:

<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail") %>' onClick='alert("Clicked");'></asp:CheckBox>

I have put alert() just for demo purpose. Of course, you can have your own JS script here.


This DID do the trick... apparently the single-quotes made sense to the
compiler, double quotes didn't
Thanks!


Nov 18 '05 #16
Thanks for the support.
Learned some things today!

Thanks again!

"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
You need to ignore items of types Header, Footer and Pager. The reason for
the message is that the first item is the header, and there is no checkbox
over there.

Also, item represents a row and your checkbox is in a cell. Therefore, you
should use e.Item.Cell{index).FindControl("cbDoMail"), where index is the
index of the TemplateColumn with the checkbox.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
I tried:

Private Sub gridItems_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
gridItems.ItemDataBound
Dim cb As CheckBox
cb = e.Item.FindControl("cbDoMail")
cb.Attributes.Add("onClick", "doJavascriptThing()")
End Sub

Result: 'Object reference not set to an instance of an object'.

Since this is my first ItemDataBound event-handling, I might be doing
something wrong...
"Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
news:%2****************@TK2MSFTNGP11.phx.gbl...
You forgot to mention the most important thing! That is that the whole story
is taking place in a TemplateColumn.
The solution is simple. Just do cbDoMail.Attributes.Add("onClick",
"doJavascriptThing()") in ItemDataBound event handler rather than in the PageLoad one. On page load stage there are no items. They are created and populated on databind stage.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
> "Eliyahu Goldin" <re*************@monarchmed.com> schreef in bericht
> news:up**************@tk2msftngp13.phx.gbl...
> > This should be fine. Where is the datagrid mentioned in the subject? > >
> > Eliyahu
> >
> > "Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message > > news:%2****************@TK2MSFTNGP12.phx.gbl...
> > > Hello,
> > > I need to add a Javascript event (onClick) to a servercontrol
> > > (asp:checkbox). Checkbox is named 'cbDoMail'.
> > > Assumed that 'cbDoMail.Attributes.Add("onClick",
"doJavascriptThing()")
> > > would do the job, unfortunately it didn't.
> > >
> > > Can anyone point me in the right direction?
> > >
> > > Thanks!
> > >
>
> Sorry forgot to specifically mention the Datagrid...
> Simply adding 'onClick="doJavascriptThing()"' ends up with '<span
> onClick="doJavascriptThing()"><input type="checkbox"...' which is
not
very
> useful.
> Whenever I put 'cbDoMail.Attributes.Add("onClick",
"doJavascriptThing()")'
> in onLoad-event it throws an 'Object reference not set to an

instance of an
> object'.
>
> Design-code:
> <asp:TemplateColumn HeaderText="Mail">
> <ItemTemplate>
> <asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
> Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,

"DoMail")
> %>'></asp:CheckBox>
> </ItemTemplate>
> </asp:TemplateColumn>
>
> Code-behind:
> Protected WithEvents cbDoMail As System.Web.UI.WebControls.CheckBox
>
> Private Sub Page_Load(......)
> cbDoMail.Attributes.Add("onClick", "doJavascriptThing()")
> ...
>
> Hope this this is more clear...
>
>



Nov 18 '05 #17
And you can setup all other client events in the same way? If it is not
documented, you can't guarantee it will be there in the next version.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
No hacking! It is not in the documentation may be because its a client-side event.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:#b**************@tk2msftngp13.phx.gbl...
I am very surprised. Is that a sort of hacking thing? OnClick is NOT
documented for checkbox.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:uz*************@TK2MSFTNGP09.phx.gbl...
"Shiva" <sh******@online.excite.com> schreef in bericht
news:uo*************@TK2MSFTNGP12.phx.gbl...
Hi,

Change the checkbox markup in the template column as:

<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail") %>' onClick='alert("Clicked");'></asp:CheckBox>

I have put alert() just for demo purpose. Of course, you can have your own JS script here.


This DID do the trick... apparently the single-quotes made sense to the
compiler, double quotes didn't
Thanks!


Nov 18 '05 #18
Shiva is right. VS.NET will complain that it's not a valid server control
attribute, but it normally shows up just fine when run.

-Max

"Shiva" <sh******@online.excite.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
onClick event-handler for a server-side CheckBox runs on the client-side
(yes, you are right that check boxes do not have server-side Click event).
As I understand from the original post, the requirement is to run a JS
script on click of a checkbox; so onClick can be used.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:OC**************@TK2MSFTNGP09.phx.gbl...
What do you mean? Where will doJavascriptThing() run? In your example it
will run on server after a postback. By the way, there is no server event
OnClick for CheckBox control in the first place.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
Hi Eliyahu,

For a CheckBox server control, onClick attribute hooks a client-side JS
event handler.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:ua**************@TK2MSFTNGP09.phx.gbl...
This will add a server-side event. If a client-side one is needed,
Attributes is the way to go.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
Hi,

You can directly add the onClick attribute to the checkbox as in
<asp:CheckBox onClick="doJavascriptThing()" runat=server>

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Hello,
I need to add a Javascript event (onClick) to a servercontrol
(asp:checkbox). Checkbox is named 'cbDoMail'.
Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()") would do the job, unfortunately it didn't.

Can anyone point me in the right direction?

Thanks!



Nov 18 '05 #19
Hi,
What I meant was that onClick is not described in the CheckBox control
documentation. But, it is up there in the HTML/DHTML documentation for
client-side events. Also, this event is part of the HTML standard
(http://msdn.microsoft.com/library/de...uthor/dhtml/re
ference/events/onclick.asp)

And, I dont think this event will be removed in any future versions of
CheckBox control.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:O2**************@TK2MSFTNGP09.phx.gbl...
And you can setup all other client events in the same way? If it is not
documented, you can't guarantee it will be there in the next version.

Eliyahu

"Shiva" <sh******@online.excite.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
No hacking! It is not in the documentation may be because its a client-side event.

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:#b**************@tk2msftngp13.phx.gbl...
I am very surprised. Is that a sort of hacking thing? OnClick is NOT
documented for checkbox.

Eliyahu

"Sjaakie Helderhorst" <di*********@hotNOTmail.com> wrote in message
news:uz*************@TK2MSFTNGP09.phx.gbl...
"Shiva" <sh******@online.excite.com> schreef in bericht
news:uo*************@TK2MSFTNGP12.phx.gbl...
Hi,

Change the checkbox markup in the template column as:

<asp:CheckBox ID="cbDoMail" Visible="true" enabled="true"
Runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "DoMail") %>' onClick='alert("Clicked");'></asp:CheckBox>

I have put alert() just for demo purpose. Of course, you can have your own JS script here.


This DID do the trick... apparently the single-quotes made sense to the
compiler, double quotes didn't
Thanks!



Nov 18 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jim Heavey | last post: by
8 posts views Thread by sumit | last post: by
2 posts views Thread by Luis Esteban Valencia Muñoz | last post: by
1 post views Thread by Dhruba Bandopadhyay | last post: by
1 post views Thread by The Eclectic Electric | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.