|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Firing events for child controls inside a DataGridHi 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 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 Show quoteHide quote "glenn" <gl***@discussions.microsoft.com> ha scritto nel messaggio news:2FFAE80F-2B12-4678-A2A7-206C24D2FF8E@microsoft.com... > 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,
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 Show quoteHide quote "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" <gl***@discussions.microsoft.com> ha scritto nel messaggio > news:2FFAE80F-2B12-4678-A2A7-206C24D2FF8E@microsoft.com... > > 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,
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 Show quoteHide quote "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" <gl***@discussions.microsoft.com> ha scritto nel messaggio > news:2FFAE80F-2B12-4678-A2A7-206C24D2FF8E@microsoft.com... > > 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 > > > > > 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> Show quoteHide quote "glenn" <gl***@discussions.microsoft.com> ha scritto nel messaggio news:D28DB45E-C164-4009-9793-CECD2F7E8E27@microsoft.com... > 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" <gl***@discussions.microsoft.com> ha scritto nel messaggio >> news:2FFAE80F-2B12-4678-A2A7-206C24D2FF8E@microsoft.com... >> > 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 >> > >> >> >>
DropdownList problem with internet explorer
passing data on the cleint side onTextChanged won't fire in UserControl How-to link 2 Detailsview Formview child control data retrieval CustomValidator ASP.NET vs Windows Forms for building client application Windows control in .ASPX page .Net 2.0 - WebBrowser control embedded in IE causes problems Commenting Out Controls In HTML View |
|||||||||||||||||||||||