Adding javascript event to datagrid control

Discussion in 'ASP .Net' started by Sjaakie Helderhorst, Jul 26, 2004.

  1. 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!
     
    Sjaakie Helderhorst, Jul 26, 2004
    #1
    1. Advertising

  2. Sjaakie Helderhorst

    Shiva Guest

    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!
     
    Shiva, Jul 26, 2004
    #2
    1. Advertising

  3. Sjaakie Helderhorst

    Shiva Guest

    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!
    >
    >
    >
     
    Shiva, Jul 26, 2004
    #3
  4. 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!
    >
    >
     
    Eliyahu Goldin, Jul 26, 2004
    #4
  5. 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!
    >
    >
    >
     
    Eliyahu Goldin, Jul 26, 2004
    #5
  6. Sjaakie Helderhorst

    Shiva Guest

    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!
    > >
    > >
    > >

    >
    >
    >
     
    Shiva, Jul 26, 2004
    #6
  7. "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...
     
    Sjaakie Helderhorst, Jul 26, 2004
    #7
  8. 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!
    > >
    > >
    > >

    >
    >
    >
     
    Eliyahu Goldin, Jul 26, 2004
    #8
  9. Sjaakie Helderhorst

    Shiva Guest

    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...
     
    Shiva, Jul 26, 2004
    #9
  10. 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...
    >
    >
     
    Eliyahu Goldin, Jul 26, 2004
    #10
  11. 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" <> schreef in bericht
    news:%...
    > 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...
    > >
    > >

    >
    >
     
    Sjaakie Helderhorst, Jul 26, 2004
    #11
  12. "Shiva" <> schreef in bericht
    news:uouu$...
    > 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!
     
    Sjaakie Helderhorst, Jul 26, 2004
    #12
  13. Not sure, maybe it just adds all unknown parameters to the input-tag.
    Would cover all future tags...


    "Eliyahu Goldin" <> schreef in bericht
    news:%...
    > I am very surprised. Is that a sort of hacking thing? OnClick is NOT
    > documented for checkbox.
    >
    > Eliyahu
    >
    > "Sjaakie Helderhorst" <> wrote in message
    > news:uz$...
    > > "Shiva" <> schreef in bericht
    > > news:uouu$...
    > > > 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!
    > >
    > >

    >
    >
     
    Sjaakie Helderhorst, Jul 26, 2004
    #13
  14. Thanks for the support.
    Learned some things today!

    Thanks again!

    "Eliyahu Goldin" <> schreef in bericht
    news:%23RR$...
    > 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" <> wrote in message
    > news:...
    > > 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" <> schreef in bericht
    > > news:%...
    > > > 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...
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Sjaakie Helderhorst, Jul 26, 2004
    #14
  15. Sjaakie Helderhorst

    Shiva Guest

    No hacking! It is not in the documentation may be because its a client-side
    event.

    "Eliyahu Goldin" <> wrote in message
    news:#...
    I am very surprised. Is that a sort of hacking thing? OnClick is NOT
    documented for checkbox.

    Eliyahu

    "Sjaakie Helderhorst" <> wrote in message
    news:uz$...
    > "Shiva" <> schreef in bericht
    > news:uouu$...
    > > 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!
    >
    >
     
    Shiva, Jul 26, 2004
    #15
  16. 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" <> wrote in message
    news:...
    > 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" <> schreef in bericht
    > news:%...
    > > 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...
    > > >
    > > >

    > >
    > >

    >
    >
     
    Eliyahu Goldin, Jul 26, 2004
    #16
  17. 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" <> wrote in message
    news:%...
    > 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!
    > > >
    > > >
    > > >

    > >
    > >
    > >

    >
    >
    >
     
    msnews.microsoft.com, Jul 26, 2004
    #17
  18. I am very surprised. Is that a sort of hacking thing? OnClick is NOT
    documented for checkbox.

    Eliyahu

    "Sjaakie Helderhorst" <> wrote in message
    news:uz$...
    > "Shiva" <> schreef in bericht
    > news:uouu$...
    > > 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!
    >
    >
     
    Eliyahu Goldin, Jul 26, 2004
    #18
  19. 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" <> wrote in message
    news:%...
    > No hacking! It is not in the documentation may be because its a

    client-side
    > event.
    >
    > "Eliyahu Goldin" <> wrote in message
    > news:#...
    > I am very surprised. Is that a sort of hacking thing? OnClick is NOT
    > documented for checkbox.
    >
    > Eliyahu
    >
    > "Sjaakie Helderhorst" <> wrote in message
    > news:uz$...
    > > "Shiva" <> schreef in bericht
    > > news:uouu$...
    > > > 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!
    > >
    > >

    >
    >
    >
     
    Eliyahu Goldin, Jul 26, 2004
    #19
  20. Sjaakie Helderhorst

    Shiva Guest

    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/default.asp?url=/workshop/author/dhtml/re
    ference/events/onclick.asp)

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

    "Eliyahu Goldin" <> wrote in message
    news:...
    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" <> wrote in message
    news:%...
    > No hacking! It is not in the documentation may be because its a

    client-side
    > event.
    >
    > "Eliyahu Goldin" <> wrote in message
    > news:#...
    > I am very surprised. Is that a sort of hacking thing? OnClick is NOT
    > documented for checkbox.
    >
    > Eliyahu
    >
    > "Sjaakie Helderhorst" <> wrote in message
    > news:uz$...
    > > "Shiva" <> schreef in bericht
    > > news:uouu$...
    > > > 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!
    > >
    > >

    >
    >
    >
     
    Shiva, Jul 26, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laurent Bugnion
    Replies:
    1
    Views:
    10,071
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 21, 2006
  2. Nathan Sokalski
    Replies:
    8
    Views:
    4,627
    Raymond
    Mar 1, 2006
  3. Jc Morin
    Replies:
    1
    Views:
    192
    Jc Morin
    Dec 5, 2003
  4. Jimmy [Used-Disks]

    CheckBoxList: Adding onclick javascript event to each ListItem.

    Jimmy [Used-Disks], Nov 24, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    755
    Jimmy [Used-Disks]
    Dec 10, 2003
  5. Tillman

    Make parent control event call child control event

    Tillman, May 21, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    316
    Tillman
    May 21, 2004
Loading...

Share This Page