CheckBox Control in VB.NET

In this article, We will see how to delete the multiple record throw the checkbox in VB.NET.
  • 2056

In this article, We will see how to delete the multiple record throw the checkbox in VB.NET.

This Code is aspx:-

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<script type="text/javascript">
    function ConfirmDelete() {
        var count = document.getElementById("<%=hfCount.ClientID %>").value;
        var gv = document.getElementById("<%=gridview.ClientID%>");
        var chk = gridview.getElementsByTagName("DeleteCheckRow");
        for (var i = 0; i < chk.length; i++) {
            if (chk[i].checked && chk[i].id.indexOf("chkSelection") == -1) {
                count++;
            }
        }
        if (count == 0) {
            alert("No records to delete.");
            return false;
        }
        else {
            return confirm("Do you want to delete " + count + " records.");
        }
    }
</script>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        #btnShow
        {
            width: 141px;
        }
    </style>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gridview" runat="server" DataKeyField="StId" BorderStyle="Ridge"
            GridLines="None" BorderWidth="2px" BorderColor="White" BackColor="White" CellPadding="3"
            CellSpacing="1" AllowSorting="True" PagerStyle-HorizontalAlign="Center"             
HorizontalAlign="Left" AutoGenerateColumns="false" Width="50%">
            <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
            <HeaderStyle Font-Bold="True" ForeColor="#FFFFFF" BackColor="#A53A6A"></HeaderStyle>
            <FooterStyle BackColor="beige" />
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="labelStId" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "StId")%>'></asp:Label></li>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="StudentName" HeaderText="StudentName">
                    <ItemStyle BackColor="GhostWhite" />
                </asp:BoundField>
                <asp:BoundField DataField="ClassName" HeaderText="ClassName">
                    <ItemStyle BackColor="GhostWhite" />
                </asp:BoundField>
                <asp:BoundField DataField="RollNo" HeaderText="RollNo">
                    <ItemStyle BackColor="GhostWhite" />
                </asp:BoundField>
                <asp:BoundField DataField="EmailId" HeaderText="EmailId">
                    <ItemStyle BackColor="GhostWhite" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSelection" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <asp:HiddenField ID="hfCount" runat="server" Value="0" />
        <asp:Button ID="DeleteCheckRow" Text="Delete Checked Rows" OnClientClick="return ConfirmDelet
();"
runat="server" Width="491px" OnClick="DeleteCheckRow_Click" />
    </div>
    </form>
</body>
</
html>

This Code is .vb:-

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
-------------------------------------------------------------------------------------------------------
Partial Class _Default
    Inherits System.Web.UI.Page
    Private sqlDataAdapter As Data.SqlClient.SqlDataAdapter
    Private dataSet As New Data.DataSet()
    Private sqlConnection As Data.SqlClient.SqlConnection
    Private sqlCommand As New Data.SqlClient.SqlCommand()

-------------------------------------------------------------------------------------------------------    

Protected Sub gridview_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridview.Load
        If Not Page.IsPostBack Then
            Binddata()
        End If
   End Sub

-------------------------------------------------------------------------------------------------------    

Public Sub Binddata()
        sqlConnection = New Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("connect"))
        sqlCommand.CommandText = "Select * From StudentRecords"
        sqlCommand.Connection = sqlConnection
        sqlDataAdapter = New Data.SqlClient.SqlDataAdapter(sqlCommand)
        sqlDataAdapter.Fill(dataSet)
        sqlConnection.Open()
        sqlCommand.ExecuteNonQuery()
        gridview.DataSource = dataSet
        gridview.DataBind()
        sqlConnection.Close()
    End Sub
 

-------------------------------------------------------------------------------------------------------   

Protected Sub DeleteCheckRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DeleteCheckRow.Click, DeleteCheckRow.Click
        For i As Integer = 0 To gridview.Rows.Count - 1
            Dim chk As CheckBox = DirectCast(gridview.Rows(i).FindControl("chkSelection"), CheckBox)
            If chk.Checked = True Then
                Dim StId As String = DirectCast(gridview.Rows(i).FindControl("labelStId"), Label).Text
                sqlConnection.Open()
                Dim comm As New SqlCommand("DELETE from StudentRecords where StId=@StId",sqlConnection)
                comm.Parameters.AddWithValue("@StId", Convert.ToInt32(StId.ToString()))
                comm.ExecuteNonQuery()
                sqlConnection.Close()
                gridview.DataBind()
            End If
        Next
                Binddata()
    End Sub
End Class

-------------------------------------------------------------------------------------------------------

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.