Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Adding javascript event to datagrid control

Reply
Thread Tools

Adding javascript event to datagrid control

 
 
Sjaakie Helderhorst
Guest
Posts: n/a
 
      07-26-2004
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!


 
Reply With Quote
 
 
 
 
Shiva
Guest
Posts: n/a
 
      07-26-2004
Hi,

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

"Sjaakie Helderhorst" <> wrote in message
news:#...
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!



 
Reply With Quote
 
 
 
 
Shiva
Guest
Posts: n/a
 
      07-26-2004
Hi Eliyahu,

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

"Eliyahu Goldin" <> wrote in message
news:...
This will add a server-side event. If a client-side one is needed,
Attributes is the way to go.

Eliyahu

"Shiva" <> wrote in message
news:...
> Hi,
>
> You can directly add the onClick attribute to the checkbox as in
> <asp:CheckBox onClick="doJavascriptThing()" runat=server>
>
> "Sjaakie Helderhorst" <> wrote in message
> news:#...
> 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!
>
>
>




 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      07-26-2004
This should be fine. Where is the datagrid mentioned in the subject?

Eliyahu

"Sjaakie Helderhorst" <> wrote in message
news:%...
> 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!
>
>



 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      07-26-2004
This will add a server-side event. If a client-side one is needed,
Attributes is the way to go.

Eliyahu

"Shiva" <> wrote in message
news:...
> Hi,
>
> You can directly add the onClick attribute to the checkbox as in
> <asp:CheckBox onClick="doJavascriptThing()" runat=server>
>
> "Sjaakie Helderhorst" <> wrote in message
> news:#...
> 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!
>
>
>



 
Reply With Quote
 
Shiva
Guest
Posts: n/a
 
      07-26-2004
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" <> wrote in message
news:...
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" <> wrote in message
news:...
> Hi Eliyahu,
>
> For a CheckBox server control, onClick attribute hooks a client-side JS
> event handler.
>
> "Eliyahu Goldin" <> wrote in message
> news:...
> This will add a server-side event. If a client-side one is needed,
> Attributes is the way to go.
>
> Eliyahu
>
> "Shiva" <> wrote in message
> news:...
> > Hi,
> >
> > You can directly add the onClick attribute to the checkbox as in
> > <asp:CheckBox onClick="doJavascriptThing()" runat=server>
> >
> > "Sjaakie Helderhorst" <> wrote in message
> > news:#...
> > 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!
> >
> >
> >

>
>
>




 
Reply With Quote
 
Sjaakie Helderhorst
Guest
Posts: n/a
 
      07-26-2004
"Eliyahu Goldin" <> schreef in bericht
news:...
> This should be fine. Where is the datagrid mentioned in the subject?
>
> Eliyahu
>
> "Sjaakie Helderhorst" <> wrote in message
> news:%...
> > 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...


 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      07-26-2004
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" <> wrote in message
news:...
> Hi Eliyahu,
>
> For a CheckBox server control, onClick attribute hooks a client-side JS
> event handler.
>
> "Eliyahu Goldin" <> wrote in message
> news:...
> This will add a server-side event. If a client-side one is needed,
> Attributes is the way to go.
>
> Eliyahu
>
> "Shiva" <> wrote in message
> news:...
> > Hi,
> >
> > You can directly add the onClick attribute to the checkbox as in
> > <asp:CheckBox onClick="doJavascriptThing()" runat=server>
> >
> > "Sjaakie Helderhorst" <> wrote in message
> > news:#...
> > 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!
> >
> >
> >

>
>
>



 
Reply With Quote
 
Shiva
Guest
Posts: n/a
 
      07-26-2004
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" <> wrote in message
news:...
"Eliyahu Goldin" <> schreef in bericht
news:...
> This should be fine. Where is the datagrid mentioned in the subject?
>
> Eliyahu
>
> "Sjaakie Helderhorst" <> wrote in message
> news:%...
> > 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...



 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      07-26-2004
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" <> wrote in message
news:...
> "Eliyahu Goldin" <> schreef in bericht
> news:...
> > This should be fine. Where is the datagrid mentioned in the subject?
> >
> > Eliyahu
> >
> > "Sjaakie Helderhorst" <> wrote in message
> > news:%...
> > > 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...
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
edit attribute values of a web control just before control rendered to the page (control's Load event?) TJ ASP .Net Web Controls 4 04-07-2006 01:30 AM
Avoiding &amp; when adding a JavaScript event handler using Attributes.Add() Nathan Sokalski ASP .Net 8 03-01-2006 05:08 AM
Adding "onunload" JavaScript event handler to body in code-behind Laurent Bugnion ASP .Net 1 01-21-2006 09:53 PM
Make parent control event call child control event Tillman ASP .Net Web Controls 0 05-21-2004 08:56 PM
Control derived from datagrid, problem with adding other control and databinding (VB) Jc Morin ASP .Net Building Controls 1 12-05-2003 07:22 PM



Advertisments