How to capture CheckChanged event from Radiobutton in Datagrid?

Discussion in 'ASP .Net Datagrid Control' started by David Hearn, Mar 11, 2005.

  1. David Hearn

    David Hearn Guest

    I have a radio button that I have inserted into a datagrid via a
    TemplateColumn. I need to know when the checked property of the radiobutton
    has changed so that I can do something. I have named a sub in the
    OnCheckChanged event so I really just need to know how to write the sub so
    that it knows which radiobutton it is that changed.

    Thanks in advance!
    David Hearn, Mar 11, 2005
    #1
    1. Advertising

  2. David Hearn

    Elton Wang Guest

    Hi David,

    You can identify the radiobutton from its ClientID.

    HTH

    Elton Wang



    >-----Original Message-----
    >I have a radio button that I have inserted into a

    datagrid via a
    >TemplateColumn. I need to know when the checked property

    of the radiobutton
    >has changed so that I can do something. I have named a

    sub in the
    >OnCheckChanged event so I really just need to know how to

    write the sub so
    >that it knows which radiobutton it is that changed.
    >
    >Thanks in advance!
    >
    >
    >.
    >
    Elton Wang, Mar 11, 2005
    #2
    1. Advertising

  3. David Hearn

    Scott M. Guest

    This code is for a checkbox, but you can easily adapt it for a RadioButton:

    To capture the event for a control placed into a Template column of a
    DataGrid, you need to add the following code to your code-behind (this
    assumes the checkbox is named "chkDynamic"):

    Protected Sub chkDynamic_CheckedChanged(ByVal Sender As Object, ByVal e As
    System.EventArgs)
    'This sub will be run for every row that has had its value changed
    Dim chk As CheckBox = CType(Sender, CheckBox)
    Dim item As DataGridItem = CType(chk.NamingContainer, DataGridItem)
    ' "item" is now a reference to the DataGrid selected row
    If chk.Checked Then item.BackColor = Color.Gray
    'From here, you could grab some data from this row with:
    item.FindControl("ControlName")
    'and then you would know which record was selected.
    End Sub

    Private Sub dg_ItemCreated(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemCreated
    'This sub runs anytime the DataGrid needs to dynamically create a cotrol
    'but we only are interested in rows that may have controls (not header,
    footer or pager rows)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    ListItemType.AlternatingItem Then
    Dim chk As CheckBox = CType(e.Item.FindControl("chkDynamic"),
    CheckBox)
    AddHandler chk.CheckedChanged, AddressOf chkDynamic_CheckedChanged
    'This adds a client-side JavaScript event handler.
    'You must also have added a client-side JavaScript function called
    chkShow(sender)
    chk.Attributes.Add("onClick", "chkShow(this," & rowNum.ToString &
    ")")
    End If
    End Sub




    "Elton Wang" <> wrote in message
    news:609b01c5267e$125062c0$...
    > Hi David,
    >
    > You can identify the radiobutton from its ClientID.
    >
    > HTH
    >
    > Elton Wang
    >
    >
    >
    >>-----Original Message-----
    >>I have a radio button that I have inserted into a

    > datagrid via a
    >>TemplateColumn. I need to know when the checked property

    > of the radiobutton
    >>has changed so that I can do something. I have named a

    > sub in the
    >>OnCheckChanged event so I really just need to know how to

    > write the sub so
    >>that it knows which radiobutton it is that changed.
    >>
    >>Thanks in advance!
    >>
    >>
    >>.
    >>
    Scott M., Mar 13, 2005
    #3
  4. David Hearn

    John Walker Guest

    Re: How to capture CheckChanged event from Radiobutton in Datagrid

    Scott,
    Thanks! this code came in very handy.

    "Scott M." wrote:

    > This code is for a checkbox, but you can easily adapt it for a RadioButton:
    >
    > To capture the event for a control placed into a Template column of a
    > DataGrid, you need to add the following code to your code-behind (this
    > assumes the checkbox is named "chkDynamic"):
    >
    > Protected Sub chkDynamic_CheckedChanged(ByVal Sender As Object, ByVal e As
    > System.EventArgs)
    > 'This sub will be run for every row that has had its value changed
    > Dim chk As CheckBox = CType(Sender, CheckBox)
    > Dim item As DataGridItem = CType(chk.NamingContainer, DataGridItem)
    > ' "item" is now a reference to the DataGrid selected row
    > If chk.Checked Then item.BackColor = Color.Gray
    > 'From here, you could grab some data from this row with:
    > item.FindControl("ControlName")
    > 'and then you would know which record was selected.
    > End Sub
    >
    > Private Sub dg_ItemCreated(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemCreated
    > 'This sub runs anytime the DataGrid needs to dynamically create a cotrol
    > 'but we only are interested in rows that may have controls (not header,
    > footer or pager rows)
    > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    > ListItemType.AlternatingItem Then
    > Dim chk As CheckBox = CType(e.Item.FindControl("chkDynamic"),
    > CheckBox)
    > AddHandler chk.CheckedChanged, AddressOf chkDynamic_CheckedChanged
    > 'This adds a client-side JavaScript event handler.
    > 'You must also have added a client-side JavaScript function called
    > chkShow(sender)
    > chk.Attributes.Add("onClick", "chkShow(this," & rowNum.ToString &
    > ")")
    > End If
    > End Sub
    >
    >
    >
    >
    > "Elton Wang" <> wrote in message
    > news:609b01c5267e$125062c0$...
    > > Hi David,
    > >
    > > You can identify the radiobutton from its ClientID.
    > >
    > > HTH
    > >
    > > Elton Wang
    > >
    > >
    > >
    > >>-----Original Message-----
    > >>I have a radio button that I have inserted into a

    > > datagrid via a
    > >>TemplateColumn. I need to know when the checked property

    > > of the radiobutton
    > >>has changed so that I can do something. I have named a

    > > sub in the
    > >>OnCheckChanged event so I really just need to know how to

    > > write the sub so
    > >>that it knows which radiobutton it is that changed.
    > >>
    > >>Thanks in advance!
    > >>
    > >>
    > >>.
    > >>

    >
    >
    >
    John Walker, May 17, 2005
    #4
    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. Jim Heavey

    CheckBox CheckChanged Event

    Jim Heavey, Mar 5, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    6,553
    Jim Heavey
    Mar 5, 2004
  2. Chris
    Replies:
    0
    Views:
    465
    Chris
    Apr 21, 2005
  3. japi
    Replies:
    2
    Views:
    7,137
    jinksk
    Aug 26, 2007
  4. Replies:
    1
    Views:
    1,031
    nahid
    Jul 12, 2007
  5. Emma Middlebrook
    Replies:
    0
    Views:
    556
    Emma Middlebrook
    Apr 23, 2008
Loading...

Share This Page