Firing events for child controls inside a DataGrid

Discussion in 'ASP .Net Web Controls' started by glenn, Jul 19, 2006.

  1. glenn

    glenn Guest

    Hi folks,

    Apparently, when you have a child control inside of a DataGrid, the event
    handler for the control does not work. I want to put a child control
    (checkbox, dropdownlist, etc.) into my datagrid and have the value selected
    to immediately fire an event and immediately update my database.

    For example, if a checkbox is checked (in a DataGrid there would be a
    checkbox column with a checkbox in each row), an event would be fired to
    update the database which in this case would have a boolean value indicating
    whether the checkbox had been checked or unchecked.

    thanks,
    Glenn
     
    glenn, Jul 19, 2006
    #1
    1. Advertising

  2. hi glenn, you can either try to hookup the event in your declarative code,
    this will fire, regardless of the fact that its a child control of your
    datacontrol. so, you can try something like this :
    <asp:CheckBox id="CheckBox1" runat="server"
    AutoPostBack="True"
    OnCheckedChanged="CheckBox1_CheckedChanged"/>

    code behind :

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
    CheckBox cb1 = (CheckBox)sender;
    if (cb1 != null)
    Response.Write(cb1.Checked.ToString());
    }


    You can do the same for your dropdownlist too.
    Command events are bubbled up in the datagrids ItemCommand method, giving
    you a central location to look for events fired by child controls in your
    templated datacontrol, however this wont work with the checkbox control or
    your dropdownlist. Unlike the button, linkbutton and ImageButton which raise
    a command event, this is not supported on the checkbox or the dropdownlist.
    The ItemCommand method exposed by the datagrid only bubbles up command
    events, since this allows you to set CommandName and CommandArgument values
    to the controls posting back, in turn allowing you to check what control
    fired the postback event in the ItemCommand method and take action
    accordingly.

    for more on the datagrids ItemCommand you can reference, the following
    document on msdn. Specially take a close look at the ItemsGrid_Command
    method in the sample code provided :
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.itemcommand.aspx

    Regards,
    Alessandro Zifiglio
    http://www.AsyncUI.net

    "glenn" <> ha scritto nel messaggio
    news:...
    > Hi folks,
    >
    > Apparently, when you have a child control inside of a DataGrid, the event
    > handler for the control does not work. I want to put a child control
    > (checkbox, dropdownlist, etc.) into my datagrid and have the value
    > selected
    > to immediately fire an event and immediately update my database.
    >
    > For example, if a checkbox is checked (in a DataGrid there would be a
    > checkbox column with a checkbox in each row), an event would be fired to
    > update the database which in this case would have a boolean value
    > indicating
    > whether the checkbox had been checked or unchecked.
    >
    > thanks,
    > Glenn
    >
     
    Alessandro Zifiglio, Jul 19, 2006
    #2
    1. Advertising

  3. glenn

    glenn Guest

    Alessandro,

    If you know VB, maybe you can tell me how
    to handle the line
    Dim cb As CheckBox = CheckBox (sender) where I get a compiler error
    'CheckBox' is a type and cannot be used as an expression'

    I used the following code in VB and I get a compiler error

    protected Sub Check_Clicked(sender as object, e as System.EventArgs)
    Dim cb As CheckBox = CheckBox (sender)
    Response.Write(cb.Checked.ToString());
    end sub


    thanks,
    glenn


    "Alessandro Zifiglio" wrote:

    > hi glenn, you can either try to hookup the event in your declarative code,
    > this will fire, regardless of the fact that its a child control of your
    > datacontrol. so, you can try something like this :
    > <asp:CheckBox id="CheckBox1" runat="server"
    > AutoPostBack="True"
    > OnCheckedChanged="CheckBox1_CheckedChanged"/>
    >
    > code behind :
    >
    > protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    > {
    > CheckBox cb1 = (CheckBox)sender;
    > if (cb1 != null)
    > Response.Write(cb1.Checked.ToString());
    > }
    >
    >
    > You can do the same for your dropdownlist too.
    > Command events are bubbled up in the datagrids ItemCommand method, giving
    > you a central location to look for events fired by child controls in your
    > templated datacontrol, however this wont work with the checkbox control or
    > your dropdownlist. Unlike the button, linkbutton and ImageButton which raise
    > a command event, this is not supported on the checkbox or the dropdownlist.
    > The ItemCommand method exposed by the datagrid only bubbles up command
    > events, since this allows you to set CommandName and CommandArgument values
    > to the controls posting back, in turn allowing you to check what control
    > fired the postback event in the ItemCommand method and take action
    > accordingly.
    >
    > for more on the datagrids ItemCommand you can reference, the following
    > document on msdn. Specially take a close look at the ItemsGrid_Command
    > method in the sample code provided :
    > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.itemcommand.aspx
    >
    > Regards,
    > Alessandro Zifiglio
    > http://www.AsyncUI.net
    >
    > "glenn" <> ha scritto nel messaggio
    > news:...
    > > Hi folks,
    > >
    > > Apparently, when you have a child control inside of a DataGrid, the event
    > > handler for the control does not work. I want to put a child control
    > > (checkbox, dropdownlist, etc.) into my datagrid and have the value
    > > selected
    > > to immediately fire an event and immediately update my database.
    > >
    > > For example, if a checkbox is checked (in a DataGrid there would be a
    > > checkbox column with a checkbox in each row), an event would be fired to
    > > update the database which in this case would have a boolean value
    > > indicating
    > > whether the checkbox had been checked or unchecked.
    > >
    > > thanks,
    > > Glenn
    > >

    >
    >
    >
     
    glenn, Jul 19, 2006
    #3
  4. glenn

    glenn Guest

    Alessandro,

    Ok. I now know how to cast in VB.

    I would write:
    CType(sender, CheckBox)

    but I am still not getting the event to fire. I have OnCheckChanged set =
    to "Changed_Click" in the <asp:CheckBox html.

    thanks,
    glenn

    "Alessandro Zifiglio" wrote:

    > hi glenn, you can either try to hookup the event in your declarative code,
    > this will fire, regardless of the fact that its a child control of your
    > datacontrol. so, you can try something like this :
    > <asp:CheckBox id="CheckBox1" runat="server"
    > AutoPostBack="True"
    > OnCheckedChanged="CheckBox1_CheckedChanged"/>
    >
    > code behind :
    >
    > protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    > {
    > CheckBox cb1 = (CheckBox)sender;
    > if (cb1 != null)
    > Response.Write(cb1.Checked.ToString());
    > }
    >
    >
    > You can do the same for your dropdownlist too.
    > Command events are bubbled up in the datagrids ItemCommand method, giving
    > you a central location to look for events fired by child controls in your
    > templated datacontrol, however this wont work with the checkbox control or
    > your dropdownlist. Unlike the button, linkbutton and ImageButton which raise
    > a command event, this is not supported on the checkbox or the dropdownlist.
    > The ItemCommand method exposed by the datagrid only bubbles up command
    > events, since this allows you to set CommandName and CommandArgument values
    > to the controls posting back, in turn allowing you to check what control
    > fired the postback event in the ItemCommand method and take action
    > accordingly.
    >
    > for more on the datagrids ItemCommand you can reference, the following
    > document on msdn. Specially take a close look at the ItemsGrid_Command
    > method in the sample code provided :
    > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.itemcommand.aspx
    >
    > Regards,
    > Alessandro Zifiglio
    > http://www.AsyncUI.net
    >
    > "glenn" <> ha scritto nel messaggio
    > news:...
    > > Hi folks,
    > >
    > > Apparently, when you have a child control inside of a DataGrid, the event
    > > handler for the control does not work. I want to put a child control
    > > (checkbox, dropdownlist, etc.) into my datagrid and have the value
    > > selected
    > > to immediately fire an event and immediately update my database.
    > >
    > > For example, if a checkbox is checked (in a DataGrid there would be a
    > > checkbox column with a checkbox in each row), an event would be fired to
    > > update the database which in this case would have a boolean value
    > > indicating
    > > whether the checkbox had been checked or unchecked.
    > >
    > > thanks,
    > > Glenn
    > >

    >
    >
    >
     
    glenn, Jul 19, 2006
    #4
  5. hi glenn, it works for me and works well. Here is a test you can try
    yourself.

    Regards,
    Alessandro Zifiglio
    http://www.AsyncUI.net


    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
    Function CreateDataSource() As ICollection
    Dim dt As New DataTable()
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn("StringValue1", GetType(String)))
    dt.Columns.Add(New DataColumn("StringValue2", GetType(String)))
    Dim i As Integer
    For i = 0 To 5
    dr = dt.NewRow()
    dr(0) = "Checkbox " + i.ToString()
    dr(1) = "TextBox " + i.ToString()
    dt.Rows.Add(dr)
    Next i

    Dim dv As New DataView(dt)
    Return dv
    End Function

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    If Not IsPostBack Then
    ' Load this data only once.
    DataGrid1.DataSource = CreateDataSource()
    DataGrid1.DataBind()
    End If
    End Sub 'Page_Load

    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e
    As System.EventArgs)
    Dim cb1 As CheckBox = CType(sender, CheckBox)

    If (cb1 IsNot Nothing) Then
    Response.Write(cb1.Checked)
    ' Lets get a reference to the tableCell holding our checkbox and
    then find our textbox control
    ' which happens to be within the same tablecell.
    ' another way to achieve a better result is to
    ' reference cb1.NamingContainer, instead of cb1.Parent
    ' since that would return a DataGridItem,
    ' the entire row, and then we can navigate through
    ' the cells collection.
    ' this is just in case you want to do something with
    ' data in the current row holding your checkbox.
    ' i dont mean to confuse you with the following extra few lines
    here.
    Dim tc As TableCell = CType(cb1.Parent, TableCell)
    Dim tb1 As TextBox = CType(tc.Controls(3), TextBox)
    If (tb1 IsNot Nothing) Then
    Response.Write(tb1.Text)
    End If

    End If
    End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <h3>
    DataGrid Example</h3>
    <b>Product List</b>
    <asp:DataGrid ID="DataGrid1" BorderColor="black" BorderWidth="1"
    CellPadding="3"
    AutoGenerateColumns="false" runat="server">
    <HeaderStyle BackColor="#00aaaa"></HeaderStyle>
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox AutoPostBack="true" ID="CheckBox1"
    Text='<%# DataBinder.Eval(Container.DataItem, "StringValue1") %>'
    OnCheckedChanged="CheckBox1_CheckedChanged"
    runat="server" />
    <asp:TextBox ID="TextBox1"
    Text='<%#DataBinder.Eval(Container.DataItem, "StringValue2")%>'
    runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </div>
    </form>
    </body>
    </html>

    "glenn" <> ha scritto nel messaggio
    news:D...
    > Alessandro,
    >
    > Ok. I now know how to cast in VB.
    >
    > I would write:
    > CType(sender, CheckBox)
    >
    > but I am still not getting the event to fire. I have OnCheckChanged set =
    > to "Changed_Click" in the <asp:CheckBox html.
    >
    > thanks,
    > glenn
    >
    > "Alessandro Zifiglio" wrote:
    >
    >> hi glenn, you can either try to hookup the event in your declarative
    >> code,
    >> this will fire, regardless of the fact that its a child control of your
    >> datacontrol. so, you can try something like this :
    >> <asp:CheckBox id="CheckBox1" runat="server"
    >> AutoPostBack="True"
    >> OnCheckedChanged="CheckBox1_CheckedChanged"/>
    >>
    >> code behind :
    >>
    >> protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    >> {
    >> CheckBox cb1 = (CheckBox)sender;
    >> if (cb1 != null)
    >> Response.Write(cb1.Checked.ToString());
    >> }
    >>
    >>
    >> You can do the same for your dropdownlist too.
    >> Command events are bubbled up in the datagrids ItemCommand method, giving
    >> you a central location to look for events fired by child controls in your
    >> templated datacontrol, however this wont work with the checkbox control
    >> or
    >> your dropdownlist. Unlike the button, linkbutton and ImageButton which
    >> raise
    >> a command event, this is not supported on the checkbox or the
    >> dropdownlist.
    >> The ItemCommand method exposed by the datagrid only bubbles up command
    >> events, since this allows you to set CommandName and CommandArgument
    >> values
    >> to the controls posting back, in turn allowing you to check what control
    >> fired the postback event in the ItemCommand method and take action
    >> accordingly.
    >>
    >> for more on the datagrids ItemCommand you can reference, the following
    >> document on msdn. Specially take a close look at the ItemsGrid_Command
    >> method in the sample code provided :
    >> http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.itemcommand.aspx
    >>
    >> Regards,
    >> Alessandro Zifiglio
    >> http://www.AsyncUI.net
    >>
    >> "glenn" <> ha scritto nel messaggio
    >> news:...
    >> > Hi folks,
    >> >
    >> > Apparently, when you have a child control inside of a DataGrid, the
    >> > event
    >> > handler for the control does not work. I want to put a child control
    >> > (checkbox, dropdownlist, etc.) into my datagrid and have the value
    >> > selected
    >> > to immediately fire an event and immediately update my database.
    >> >
    >> > For example, if a checkbox is checked (in a DataGrid there would be a
    >> > checkbox column with a checkbox in each row), an event would be fired
    >> > to
    >> > update the database which in this case would have a boolean value
    >> > indicating
    >> > whether the checkbox had been checked or unchecked.
    >> >
    >> > thanks,
    >> > Glenn
    >> >

    >>
    >>
    >>
     
    Alessandro Zifiglio, Jul 19, 2006
    #5
    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. Vikram Sinha
    Replies:
    0
    Views:
    469
    Vikram Sinha
    Apr 12, 2004
  2. Juan Romero
    Replies:
    2
    Views:
    4,904
    Juan Romero
    Apr 19, 2004
  3. =?Utf-8?B?Z2xlbm4=?=

    Firing events for child controls in a DataGrid

    =?Utf-8?B?Z2xlbm4=?=, Jul 19, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,983
    =?Utf-8?B?Z2xlbm4=?=
    Jul 19, 2006
  4. SevDer
    Replies:
    2
    Views:
    1,329
    Walter Wang [MSFT]
    Nov 13, 2007
  5. David Boike

    Child controls in Composite Controls not firing events

    David Boike, Aug 7, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    334
    David Boike
    Aug 7, 2003
Loading...

Share This Page