R
Ravik Tupja
Hello,
I have been using a datagrid to page through the results of a query.
Coding standards prevent me from setting the properties of the
datagrid and binding data to the datagrid in the Page_Load method. I
instead create and bind data to the datagrid in other methods (such as
the getData method) which are called after the Page_Load method is
executed. My problem is that the PageIndexChanged event handler of
the datagrid does not fire. I can see the first page of the results
just fine. However if I click on the next page link, I get a blank
page with no datagrid. I have checked, and I found out that the
PageIndexChanged event handler does not fire.
Here is the most important part of the aspx page:
<%
setConnectionMgr(objConnectionMgr)
setDict(Dict)
loadUI()
getData()
%>
<form runat="server" id="form1">
<asp:datagrid runat="server"
OnPageIndexChanged="EmployeeInfoDataGrid_PageIndexChanged"
id="EmployeeInfoDataGrid" borderColor="gray" cellspacing="1"
cellpadding="4" width="450" CssClass="InnerTable"
AutoGenerateColumns="false" AllowPaging="true"
AllowCustomPaging="false" EnableViewState="true">
<HeaderStyle CssClass="headerRow"/>
<ItemStyle BackColor="Gainsboro"/>
<AlternatingItemStyle BackColor="#ffffff"/>
</asp:datagrid>
</form>
Here is the code-behind page:
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Public Class ViewTelephoneList
Inherits System.Web.UI.Page
Protected WithEvents LanguageHyperLink As HyperLink
Protected WithEvents LanguageLabel As Label
Protected WithEvents table1 As Table
Protected WithEvents PageLabel As Label
Protected WithEvents EmployeeInfoDataGrid As DataGrid
Protected WithEvents table2 As System.Web.UI.WebControls.Table
Protected WithEvents PreviousHyperLink As HyperLink
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Protected sFileName = "ViewTelePhoneList.aspx"
Protected sSection As String = "People"
Protected iNumItems As Integer = 0
Protected iCounter As Integer = 0
Protected iPageNumber As Integer = 0
Protected iPageSize As Int16 = 0
Protected iTotalRecords As Integer = 0
Protected sActionPage As String = ""
Protected sAddEditPage As String = ""
Protected sPageNameSingular As String = "Contact"
Protected sPageNamePlural As String = "Contacts"
Protected sAlternatingColour As String = ""
Protected sInActiveStr As String = ""
Protected objEmployees As Employees
Protected objArray() As Employee
Protected Dict As Dict
Protected objConnectionMgr As util.ConnectionMgr
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Session("section") = sSection
'Put user code to initialize the page here
End Sub
Protected Sub getData()
Dim sEmployeeFirstName As String =
Request.QueryString("firstname")
Dim sEmployeeLastName As String =
Request.QueryString("lastname")
Dim iLocationId As String = Request.QueryString("locationid")
Dim iDivisionId As String = Request.QueryString("divisionid")
Dim iDirectorateId As String =
Request.QueryString("directorateid")
Dim bConditionFound = False
Dim sSqlString As String
sSqlString = "SELECT * FROM EMPLOYEE"
If (sEmployeeFirstName <> "" Or sEmployeeLastName <> "" Or
iLocationId <> "0" Or iDivisionId <> "0" Or iDirectorateId <> "0")
Then
sSqlString = sSqlString & " WHERE IS_ACTIVE=1 AND"
End If
If (iDirectorateId <> "0" And bConditionFound = True) Then
sSqlString = "SELECT * FROM EMPLOYEE INNER JOIN
DIVISION_STATIC ON EMPLOYEE.EMPLOYEE_DIVISION_ID = DIVISION_STATIC.ID
WHERE DIVISION_STATIC.DIVISION_DIRECTORATE_ID = '" & iDirectorateId &
"'"
ElseIf (iDirectorateId <> "0" And bConditionFound = False)
Then
bConditionFound = True
sSqlString = "SELECT * FROM EMPLOYEE INNER JOIN
DIVISION_STATIC ON EMPLOYEE.EMPLOYEE_DIVISION_ID = DIVISION_STATIC.ID
WHERE DIVISION_STATIC.DIVISION_DIRECTORATE_ID = '" & iDirectorateId &
"'"
End If
If (sEmployeeFirstName <> "" And bConditionFound = True) Then
sSqlString = sSqlString & " AND EMPLOYEE_FIRST_NAME Like
'" & sEmployeeFirstName & "%'"
ElseIf (sEmployeeFirstName <> "" And bConditionFound = False)
Then
bConditionFound = True
sSqlString = sSqlString & " EMPLOYEE_FIRST_NAME Like '" &
sEmployeeFirstName & "%'"
End If
If (sEmployeeLastName <> "" And bConditionFound = True) Then
sSqlString = sSqlString & " AND EMPLOYEE_LAST_NAME Like '"
& sEmployeeLastName & "%'"
ElseIf (sEmployeeLastName <> "" And bConditionFound = False)
Then
bConditionFound = True
sSqlString = sSqlString & " EMPLOYEE_LAST_NAME Like '" &
sEmployeeLastName & "%'"
End If
If (iDivisionId <> "0" And bConditionFound = True) Then
sSqlString = sSqlString & " AND EMPLOYEE_DIVISION_ID ='" &
iDivisionId & "'"
ElseIf (iDivisionId <> "0" And bConditionFound = False) Then
bConditionFound = True
sSqlString = sSqlString & " EMPLOYEE_DIVISION_ID = '" &
iDivisionId & "'"
End If
If (iLocationId <> "0" And bConditionFound = True) Then
sSqlString = sSqlString & " AND EMPLOYEE_LOCATION_ID = '"
& iLocationId & "'"
ElseIf (iLocationId <> "0" And bConditionFound = False) Then
bConditionFound = True
sSqlString = sSqlString & " EMPLOYEE_LOCATION_ID = '" &
iLocationId & "'"
End If
sSqlString = sSqlString & " ORDER BY EMPLOYEE_LAST_NAME"
objEmployees = New Employees(objConnectionMgr)
objArray = objEmployees.getEmployees(sSqlString)
iNumItems = objEmployees.getNumberOfEmployees()
iTotalRecords = objEmployees.getTotalRecords()
Initialize_EmployeeInfoDataGrid()
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Dim objTable As DataTable
Dim objDataRow As DataRow
Dim i As Integer = 0
'objTable = objDataSet.Tables.Add("EmployeeInfo")
objTable = New DataTable("EMPLOYEE")
objTable.Columns.Add("EMPLOYEE_LAST_NAME",
System.Type.GetType("System.String"))
objTable.Columns.Add("EMPLOYEE_FIRST_NAME",
System.Type.GetType("System.String"))
objTable.Columns.Add("EMPLOYEE_PHONE_NUMBER",
System.Type.GetType("System.String"))
objTable.Columns.Add("LABEL_" & Session("Lang"),
System.Type.GetType("System.String"))
objTable.Columns.Add("DIVISION_CODE_" & Session("Lang"),
System.Type.GetType("System.String"))
For i = 0 To iNumItems - 1
objDataRow = objTable.NewRow()
objDataRow("EMPLOYEE_LAST_NAME") =
objArray(i).getEmployeeLastName()
objDataRow("EMPLOYEE_FIRST_NAME") =
objArray(i).getEmployeeFirstName()
objDataRow("EMPLOYEE_PHONE_NUMBER") =
objArray(i).getEmployeePhoneNumber()
objDataRow("LABEL_" & Session("Lang")) =
objArray(i).getLocation().getLabel()
objDataRow("DIVISION_CODE_" & Session("Lang")) =
objArray(i).getdivision().getdivisionCode()
objTable.Rows.Add(objDataRow)
Next
'Bind data to datagrid
EmployeeInfoDataGrid.DataSource = objTable.DefaultView
EmployeeInfoDataGrid.DataBind()
End Sub
Private Sub Initialize_EmployeeInfoDataGrid()
Dim objColumn As BoundColumn
Dim Dict As New Dict()
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLLastName
objColumn.DataField = "EMPLOYEE_LAST_NAME"
EmployeeInfoDataGrid.Columns.Add(objColumn)
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLFirstName
objColumn.DataField = "EMPLOYEE_FIRST_NAME"
EmployeeInfoDataGrid.Columns.Add(objColumn)
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLPhoneNumber
objColumn.DataField = "EMPLOYEE_PHONE_NUMBER"
EmployeeInfoDataGrid.Columns.Add(objColumn)
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLLocationName
objColumn.DataField = "LABEL_" & Session("Lang")
EmployeeInfoDataGrid.Columns.Add(objColumn)
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLGroup
objColumn.DataField = "DIVISION_CODE_" & Session("Lang")
EmployeeInfoDataGrid.Columns.Add(objColumn)
Dim objDGPagerStyle As DataGridPagerStyle =
EmployeeInfoDataGrid.PagerStyle
objDGPagerStyle.NextPageText = "[" & Dict.Nextt & "]"
objDGPagerStyle.PrevPageText = "[" & Dict.Previous & "]"
EmployeeInfoDataGrid.PageSize =
ConfigurationSettings.AppSettings("PAGE_SIZE")
End Sub
Protected Sub setConnectionMgr(ByRef pobjConnectionMgr As
util.ConnectionMgr)
objConnectionMgr = pobjConnectionMgr
End Sub
Protected Sub setDict(ByRef pDict As Dict)
Dict = pDict
End Sub
Protected Sub loadUI()
'load label text
LanguageLabel.Text = Dict.sTBLanguage
'load hyperlink urls
LanguageHyperLink.NavigateUrl =
ConfigurationSettings.AppSettings("Root") & "LanguageToggle.aspx"
PreviousHyperLink.NavigateUrl = "TelephoneList.aspx"
PreviousHyperLink.Text = Dict.sReturnToTelephoneList
End Sub
Protected Sub EmployeeInfoDataGrid_PageIndexChanged(ByVal sender
As Object, ByVal e As DataGridPageChangedEventArgs) Handles
EmployeeInfoDataGrid.PageIndexChanged
EmployeeInfoDataGrid.CurrentPageIndex = e.NewPageIndex
BindData()
End Sub
End Class
Any help would be appreciated.
Ravik Tupja
4th Year Software Engineering
Carleton University
I have been using a datagrid to page through the results of a query.
Coding standards prevent me from setting the properties of the
datagrid and binding data to the datagrid in the Page_Load method. I
instead create and bind data to the datagrid in other methods (such as
the getData method) which are called after the Page_Load method is
executed. My problem is that the PageIndexChanged event handler of
the datagrid does not fire. I can see the first page of the results
just fine. However if I click on the next page link, I get a blank
page with no datagrid. I have checked, and I found out that the
PageIndexChanged event handler does not fire.
Here is the most important part of the aspx page:
<%
setConnectionMgr(objConnectionMgr)
setDict(Dict)
loadUI()
getData()
%>
<form runat="server" id="form1">
<asp:datagrid runat="server"
OnPageIndexChanged="EmployeeInfoDataGrid_PageIndexChanged"
id="EmployeeInfoDataGrid" borderColor="gray" cellspacing="1"
cellpadding="4" width="450" CssClass="InnerTable"
AutoGenerateColumns="false" AllowPaging="true"
AllowCustomPaging="false" EnableViewState="true">
<HeaderStyle CssClass="headerRow"/>
<ItemStyle BackColor="Gainsboro"/>
<AlternatingItemStyle BackColor="#ffffff"/>
</asp:datagrid>
</form>
Here is the code-behind page:
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Public Class ViewTelephoneList
Inherits System.Web.UI.Page
Protected WithEvents LanguageHyperLink As HyperLink
Protected WithEvents LanguageLabel As Label
Protected WithEvents table1 As Table
Protected WithEvents PageLabel As Label
Protected WithEvents EmployeeInfoDataGrid As DataGrid
Protected WithEvents table2 As System.Web.UI.WebControls.Table
Protected WithEvents PreviousHyperLink As HyperLink
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Protected sFileName = "ViewTelePhoneList.aspx"
Protected sSection As String = "People"
Protected iNumItems As Integer = 0
Protected iCounter As Integer = 0
Protected iPageNumber As Integer = 0
Protected iPageSize As Int16 = 0
Protected iTotalRecords As Integer = 0
Protected sActionPage As String = ""
Protected sAddEditPage As String = ""
Protected sPageNameSingular As String = "Contact"
Protected sPageNamePlural As String = "Contacts"
Protected sAlternatingColour As String = ""
Protected sInActiveStr As String = ""
Protected objEmployees As Employees
Protected objArray() As Employee
Protected Dict As Dict
Protected objConnectionMgr As util.ConnectionMgr
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Session("section") = sSection
'Put user code to initialize the page here
End Sub
Protected Sub getData()
Dim sEmployeeFirstName As String =
Request.QueryString("firstname")
Dim sEmployeeLastName As String =
Request.QueryString("lastname")
Dim iLocationId As String = Request.QueryString("locationid")
Dim iDivisionId As String = Request.QueryString("divisionid")
Dim iDirectorateId As String =
Request.QueryString("directorateid")
Dim bConditionFound = False
Dim sSqlString As String
sSqlString = "SELECT * FROM EMPLOYEE"
If (sEmployeeFirstName <> "" Or sEmployeeLastName <> "" Or
iLocationId <> "0" Or iDivisionId <> "0" Or iDirectorateId <> "0")
Then
sSqlString = sSqlString & " WHERE IS_ACTIVE=1 AND"
End If
If (iDirectorateId <> "0" And bConditionFound = True) Then
sSqlString = "SELECT * FROM EMPLOYEE INNER JOIN
DIVISION_STATIC ON EMPLOYEE.EMPLOYEE_DIVISION_ID = DIVISION_STATIC.ID
WHERE DIVISION_STATIC.DIVISION_DIRECTORATE_ID = '" & iDirectorateId &
"'"
ElseIf (iDirectorateId <> "0" And bConditionFound = False)
Then
bConditionFound = True
sSqlString = "SELECT * FROM EMPLOYEE INNER JOIN
DIVISION_STATIC ON EMPLOYEE.EMPLOYEE_DIVISION_ID = DIVISION_STATIC.ID
WHERE DIVISION_STATIC.DIVISION_DIRECTORATE_ID = '" & iDirectorateId &
"'"
End If
If (sEmployeeFirstName <> "" And bConditionFound = True) Then
sSqlString = sSqlString & " AND EMPLOYEE_FIRST_NAME Like
'" & sEmployeeFirstName & "%'"
ElseIf (sEmployeeFirstName <> "" And bConditionFound = False)
Then
bConditionFound = True
sSqlString = sSqlString & " EMPLOYEE_FIRST_NAME Like '" &
sEmployeeFirstName & "%'"
End If
If (sEmployeeLastName <> "" And bConditionFound = True) Then
sSqlString = sSqlString & " AND EMPLOYEE_LAST_NAME Like '"
& sEmployeeLastName & "%'"
ElseIf (sEmployeeLastName <> "" And bConditionFound = False)
Then
bConditionFound = True
sSqlString = sSqlString & " EMPLOYEE_LAST_NAME Like '" &
sEmployeeLastName & "%'"
End If
If (iDivisionId <> "0" And bConditionFound = True) Then
sSqlString = sSqlString & " AND EMPLOYEE_DIVISION_ID ='" &
iDivisionId & "'"
ElseIf (iDivisionId <> "0" And bConditionFound = False) Then
bConditionFound = True
sSqlString = sSqlString & " EMPLOYEE_DIVISION_ID = '" &
iDivisionId & "'"
End If
If (iLocationId <> "0" And bConditionFound = True) Then
sSqlString = sSqlString & " AND EMPLOYEE_LOCATION_ID = '"
& iLocationId & "'"
ElseIf (iLocationId <> "0" And bConditionFound = False) Then
bConditionFound = True
sSqlString = sSqlString & " EMPLOYEE_LOCATION_ID = '" &
iLocationId & "'"
End If
sSqlString = sSqlString & " ORDER BY EMPLOYEE_LAST_NAME"
objEmployees = New Employees(objConnectionMgr)
objArray = objEmployees.getEmployees(sSqlString)
iNumItems = objEmployees.getNumberOfEmployees()
iTotalRecords = objEmployees.getTotalRecords()
Initialize_EmployeeInfoDataGrid()
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Dim objTable As DataTable
Dim objDataRow As DataRow
Dim i As Integer = 0
'objTable = objDataSet.Tables.Add("EmployeeInfo")
objTable = New DataTable("EMPLOYEE")
objTable.Columns.Add("EMPLOYEE_LAST_NAME",
System.Type.GetType("System.String"))
objTable.Columns.Add("EMPLOYEE_FIRST_NAME",
System.Type.GetType("System.String"))
objTable.Columns.Add("EMPLOYEE_PHONE_NUMBER",
System.Type.GetType("System.String"))
objTable.Columns.Add("LABEL_" & Session("Lang"),
System.Type.GetType("System.String"))
objTable.Columns.Add("DIVISION_CODE_" & Session("Lang"),
System.Type.GetType("System.String"))
For i = 0 To iNumItems - 1
objDataRow = objTable.NewRow()
objDataRow("EMPLOYEE_LAST_NAME") =
objArray(i).getEmployeeLastName()
objDataRow("EMPLOYEE_FIRST_NAME") =
objArray(i).getEmployeeFirstName()
objDataRow("EMPLOYEE_PHONE_NUMBER") =
objArray(i).getEmployeePhoneNumber()
objDataRow("LABEL_" & Session("Lang")) =
objArray(i).getLocation().getLabel()
objDataRow("DIVISION_CODE_" & Session("Lang")) =
objArray(i).getdivision().getdivisionCode()
objTable.Rows.Add(objDataRow)
Next
'Bind data to datagrid
EmployeeInfoDataGrid.DataSource = objTable.DefaultView
EmployeeInfoDataGrid.DataBind()
End Sub
Private Sub Initialize_EmployeeInfoDataGrid()
Dim objColumn As BoundColumn
Dim Dict As New Dict()
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLLastName
objColumn.DataField = "EMPLOYEE_LAST_NAME"
EmployeeInfoDataGrid.Columns.Add(objColumn)
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLFirstName
objColumn.DataField = "EMPLOYEE_FIRST_NAME"
EmployeeInfoDataGrid.Columns.Add(objColumn)
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLPhoneNumber
objColumn.DataField = "EMPLOYEE_PHONE_NUMBER"
EmployeeInfoDataGrid.Columns.Add(objColumn)
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLLocationName
objColumn.DataField = "LABEL_" & Session("Lang")
EmployeeInfoDataGrid.Columns.Add(objColumn)
objColumn = New BoundColumn()
objColumn.HeaderText = Dict.sPeopleVTLGroup
objColumn.DataField = "DIVISION_CODE_" & Session("Lang")
EmployeeInfoDataGrid.Columns.Add(objColumn)
Dim objDGPagerStyle As DataGridPagerStyle =
EmployeeInfoDataGrid.PagerStyle
objDGPagerStyle.NextPageText = "[" & Dict.Nextt & "]"
objDGPagerStyle.PrevPageText = "[" & Dict.Previous & "]"
EmployeeInfoDataGrid.PageSize =
ConfigurationSettings.AppSettings("PAGE_SIZE")
End Sub
Protected Sub setConnectionMgr(ByRef pobjConnectionMgr As
util.ConnectionMgr)
objConnectionMgr = pobjConnectionMgr
End Sub
Protected Sub setDict(ByRef pDict As Dict)
Dict = pDict
End Sub
Protected Sub loadUI()
'load label text
LanguageLabel.Text = Dict.sTBLanguage
'load hyperlink urls
LanguageHyperLink.NavigateUrl =
ConfigurationSettings.AppSettings("Root") & "LanguageToggle.aspx"
PreviousHyperLink.NavigateUrl = "TelephoneList.aspx"
PreviousHyperLink.Text = Dict.sReturnToTelephoneList
End Sub
Protected Sub EmployeeInfoDataGrid_PageIndexChanged(ByVal sender
As Object, ByVal e As DataGridPageChangedEventArgs) Handles
EmployeeInfoDataGrid.PageIndexChanged
EmployeeInfoDataGrid.CurrentPageIndex = e.NewPageIndex
BindData()
End Sub
End Class
Any help would be appreciated.
Ravik Tupja
4th Year Software Engineering
Carleton University