My GridView becomes white and the field names disappear except the DropDownList where the problem could
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Newsearchdata.aspx.vb" Inherits="UNIFORMPROJVB.Newsearchdata" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div class="auto-style1">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$("[id*=Stockbooks]").DataTable(
{
bLengthChange: true,
lengthMenu: [[15, 20, -1], [15, 20, "All"]],
bFilter: true,
bSort: true,
bPaginate: true
});
});
</script>
<div>
<asp:GridView ID="Stockbooks" runat="server" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound" CssClass="w3-table-all w3-hoverable"
HeaderStyle-BackColor="#FFCCCC" Width="73%" ClientIDMode="Static">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="admno" runat="server" Text='<%#Eval("admno")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black"
HeaderText="Admno">
<ItemTemplate>
<asp:Label ID="Name" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="12px"
Text='<%#Eval("admno")%>'>
</asp:Label>
</ItemTemplate>
<HeaderStyle Font-Names="Arial" Font-Size="12px" ForeColor="white" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black"
HeaderText="Student Names">
<ItemTemplate>
<asp:Label ID="Class" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="12px"
Text='<%#Eval("Name") %>'>
</asp:Label>
</ItemTemplate>
<ControlStyle Width="250px" />
<HeaderStyle Font-Names="Arial" Font-Size="12px" ForeColor="white" />
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Class">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Class") %>' Visible="False" />
<asp:DropDownList ID="ddlclasses" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black" HeaderText="Stream">
<ItemTemplate>
<asp:Label ID="Lblstream" runat="server" Text='<%# Eval("Stream") %>' Visible="False"></asp:Label>
<asp:DropDownList ID="ddlstream" runat="server">
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Font-Names="Arial" Font-Size="12px" ForeColor="white" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black" HeaderText="Sex">
<ItemTemplate>
<asp:Label ID="lblsex" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="12px"
Text='<%# Eval("Sex") %>' Visible="False"></asp:Label>
<asp:DropDownList ID="ddlsex" runat="server">
</asp:DropDownList>
</ItemTemplate>
<ControlStyle Width="100px"></ControlStyle>
<HeaderStyle Font-Names="Arial" Font-Size="12px" ForeColor="Black" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black"
HeaderText="STDTYPE">
<ItemTemplate>
<asp:Label ID="lblstdtype" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="12px"
Text='<%# Eval("STDTYPE") %>' Visible="False"></asp:Label>
<asp:DropDownList ID="ddlstdtype" runat="server">
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Font-Names="Arial" Font-Size="12px" ForeColor="Black"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black"
HeaderText="Status">
<ItemTemplate>
<asp:Label ID="Colour" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="12px"
Text='<%#Eval("House") %>'>
</asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
<HeaderStyle Font-Names="Arial" Font-Size="12px" ForeColor="Black"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black"
Visible="false">
<ItemTemplate>
<asp:Label ID="ACNOS" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="12px"
Text='<%#Eval("ACNOS") %>'>
</asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
<HeaderStyle Font-Names="Arial" Font-Size="12px" ForeColor="white" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black"
Visible="false">
<ItemTemplate>
<asp:Label ID="lblreminder" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="12px"
Text='<%#Eval("Classr") %>'>
</asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
<HeaderStyle Font-Names="Arial" Font-Size="12px" ForeColor="white" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="Black"
Visible="false">
<ItemTemplate>
<asp:Label ID="lblpo" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="12px"
Text='<%#Eval("autofield") %>'>
</asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
<HeaderStyle Font-Names="Arial" Font-Size="12px" ForeColor="white" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Record Available
</EmptyDataTemplate>
<HeaderStyle BackColor="#4dc3ff" />
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
Imports System.Data.SqlClient
Imports System.Data
Imports System.Linq
Imports System.Configuration
Imports System.Web.Services
Imports System.Collections
Public Class Newsearchdata
Inherits System.Web.UI.Page
Dim conString As String = ConfigurationManager.ConnectionStrings("STOREConnectionString1").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
'Enablall()
End If
End Sub
Protected Sub Enablall()
For Each row As GridViewRow In Stockbooks.Rows
If row.RowType = DataControlRowType.DataRow Then
row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked = True
End If
Next
For Each row As GridViewRow In Stockbooks.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
For i As Integer = 1 To row.Cells.Count - 1
' Dim country As String = row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Text
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = Not isChecked
If row.Cells(i).Controls.OfType(Of TextBox)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Visible = isChecked
End If
If row.Cells(i).Controls.OfType(Of DropDownList)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = isChecked
End If
' If isChecked AndAlso Not isUpdateVisible Then
'isUpdateVisible = True
' End If
' If Not isChecked Then
'chkAll.Checked = False
' End If
Next
End If
Next
End Sub
Private Sub BindGrid()
Dim cmd As New SqlCommand("SELECT Name,class as Classr,class,stream, House, ACNOS,admno,Sex,STDTYPE,Autofield FROM STUDENT")
Stockbooks.DataSource = Me.ExecuteQuery(cmd, "SELECT")
Stockbooks.DataBind()
Stockbooks.UseAccessibleHeader = True
Stockbooks.HeaderRow.TableSection = TableRowSection.TableHeader
End Sub
Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim isUpdateVisible As Boolean = False
Dim chk As CheckBox = TryCast(sender, CheckBox)
If chk.ID = "chkAll" Then
For Each row As GridViewRow In Stockbooks.Rows
If row.RowType = DataControlRowType.DataRow Then
row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked = chk.Checked
End If
Next
End If
Dim chkAll As CheckBox = TryCast(Stockbooks.HeaderRow.FindControl("chkAll"), CheckBox)
chkAll.Checked = True
For Each row As GridViewRow In Stockbooks.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
For i As Integer = 1 To row.Cells.Count - 1
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = Not isChecked
If row.Cells(i).Controls.OfType(Of TextBox)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Visible = isChecked
End If
If row.Cells(i).Controls.OfType(Of DropDownList)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = isChecked
End If
If isChecked AndAlso Not isUpdateVisible Then
isUpdateVisible = True
End If
If Not isChecked Then
chkAll.Checked = False
End If
Next
End If
Next
' btnUpdate.Visible = isUpdateVisible
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim ddlCountriestr As DropDownList = CType(e.Row.FindControl("ddlstream"), DropDownList)
Dim cmd As New SqlCommand("SELECT DISTINCT(Stream) as Stream FROM Streams")
ddlCountriestr.DataSource = Me.ExecuteQuery(cmd, "SELECT")
'ddlCountriestr.DataSource = GetData("SELECT DISTINCT(Stream) FROM Streams")
ddlCountriestr.DataTextField = "Stream"
ddlCountriestr.DataValueField = "Stream"
ddlCountriestr.DataBind()
'Add Default Item in the DropDownList
ddlCountriestr.Items.Insert(0, New ListItem("Please select"))
'Select the Country of Customer in DropDownList
Dim country2 As String = TryCast(e.Row.FindControl("lblstream"), Label).Text
If country2 = "" Then
Exit Sub
Else
ddlCountriestr.Items.FindByValue(country2).Selected = True
End If
Dim ddlCountriestrx As DropDownList = CType(e.Row.FindControl("ddlsex"), DropDownList)
Dim cmdk As New SqlCommand("SELECT DISTINCT(Sex) as Sex FROM Sexdata")
ddlCountriestrx.DataSource = Me.ExecuteQuery(cmd, "SELECT")
'ddlCountriestr.DataSource = GetData("SELECT DISTINCT(Stream) FROM Streams")
ddlCountriestrx.DataTextField = "Sex"
ddlCountriestrx.DataValueField = "Sex"
ddlCountriestrx.DataBind()
'Add Default Item in the DropDownList
ddlCountriestrx.Items.Insert(0, New ListItem("Please select"))
'Select the Country of Customer in DropDownList
Dim country2x As String = TryCast(e.Row.FindControl("lblsex"), Label).Text
If country2x = "" Then
Exit Sub
Else
ddlCountriestrx.Items.FindByValue(country2x).Selected = True
End If
Dim cell As TableCell = e.Row.Cells(1)
Dim quantity As String = cell.Text
'Find the DropDownList in the Row
Dim ddlCountries As DropDownList = CType(e.Row.FindControl("ddlclasses"), DropDownList)
ddlCountries.DataSource = GetData("SELECT DISTINCT(Class)as class FROM Classes")
ddlCountries.DataTextField = "Class"
ddlCountries.DataValueField = "Class"
ddlCountries.DataBind()
'Add Default Item in the DropDownList
ddlCountries.Items.Insert(0, New ListItem("Please select"))
'Select the Country of Customer in DropDownList
Dim country As String = TryCast(e.Row.FindControl("lblCountry"), Label).Text
If country = "" Then
Exit Sub
Else
ddlCountries.Items.FindByValue(country).Selected = True
End If
Dim ddlCountriestrd As DropDownList = CType(e.Row.FindControl("ddlstdtype"), DropDownList)
ddlCountriestrd.DataSource = GetData("SELECT DISTINCT(category)as Category FROM Newoldtypes")
ddlCountriestrd.DataTextField = "Category"
ddlCountriestrd.DataValueField = "Category"
ddlCountriestrd.DataBind()
'Add Default Item in the DropDownList
ddlCountriestrd.Items.Insert(0, New ListItem("Please select"))
'Select the Country of Customer in DropDownList
Dim country2d As String = TryCast(e.Row.FindControl("lblstdtype"), Label).Text
If country2d = "" Then
Exit Sub
Else
ddlCountriestrd.Items.FindByValue(country2d).Selected = True
End If
End If
End Sub
Private Function GetData(query As String) As DataSet
' Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As New SqlCommand(query)
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
sda.Fill(ds)
Return ds
End Using
End Using
End Using
End Function
Private Function ExecuteQuery(ByVal cmd As SqlCommand, ByVal action As String) As DataTable
'Dim conString As String = conString
Using con As New SqlConnection(conString)
cmd.Connection = con
Select Case action
Case "SELECT"
Using sda As New SqlDataAdapter()
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
Case "UPDATE"
con.Open()
cmd.ExecuteReader()
con.Close()
Exit Select
End Select
Return Nothing
End Using
End Function
End Class