In some situations we need to perform some operations on clicking the checkbox inside a datagrid. You need to perform the following steps.
Populate the datagrid with data from database. Add one template column in the HTML source and add a checkbox in that.I have given below the HTML source for this.
Let me explain the scenario in which we can apply this. Consider that we have a datagrid which is having the list of employees. In each row of the datagrid we have one check box and while clicking the check box we need to perform some database operations. Here 'DoOperation' is a user defined procedure which will contain the code to perform the required operation. Call this function inside the itemdatabound event as given below.
Private Sub dgdTest_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdTest.ItemDataBound Try Dim ChkTemp as CheckBox chkTemp = DirectCast(e.Item.FindControl("chkTest"), CheckBox) If Not chkTemp Is Nothing Then AddHandler chkTest.CheckedChanged, AddressOf DoOperation End If
Catch ex As Exception 'handle exceptions here End Try End Sub
In the following procedure you have to write the required functionality.
Public Sub DoOperation(ByVal sender As Object, ByVal e As System.EventArgs)
Try Dim intCounter As Integer
For intCounter = 0 To dgdTest.Items.Count - 1
Dim chkTest As CheckBox chkTest = CType(dgdTest.Items(i).FindControl("chkTest"), CheckBox) If Not chkTest Is CType(sender, CheckBox) Then
Else
If CType(sender, CheckBox).Checked = True Then 'write code End If
End If Next Catch ex As Exception 'handle exceptions here End Try
End Sub
In the HTML source we have to give the procedure name as given below.Don't forget to set the autopostback property to true.
<asp:datagrid id="dgdTest" runat="server"> <asp:TemplateColumn HeaderText="Name"> <HeaderStyle HorizontalAlign="Center" Width="18%" CssClass="gridHeader"></HeaderStyle> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>' > </asp:Label> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Department"> <HeaderStyle HorizontalAlign="Center" Width="18%" CssClass="gridHeader"></HeaderStyle> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> <ItemTemplate> <asp:Label ID="lblDept" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Department")%>' > </asp:Label> </ItemTemplate> </asp:TemplateColumn> <asp:datagrid id="dgdTest" Runat="server" Width="100%" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn HeaderText="Check"> <ItemTemplate> <asp:CheckBox id="chkTest" AutoPostBack="True" OnCheckedChanged="SourceCheck" Runat="server"></asp:CheckBox> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp;datagrid>
Happy Coding...
|
| Author: Siddesh Kapadi 13 Apr 2005 | Member Level: Bronze Points : 0 |
I didnot find this article useful as this doesnot give the detailed describtion and the proper execution of the code
|
| Author: Parvathi Gandhi 18 Apr 2005 | Member Level: Silver Points : 0 |
The article contains only the required code not the entire code of the page. If u have a requirement like this u can use this code. Now i have added some more description. Have a look.
-Parvathi.K
|