Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports Telerik.Web.UI
Imports System.Data.SqlClient
Imports System.IO
Partial Class Default2
    Inherits System.Web.UI.Page
    Dim count As Integer = 0
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'Me.RadToolTipManager1.TargetControls.Clear()
            'Dim expression As New GridSortExpression()
            'expression.FieldName = "StaffName"
            'expression.SortOrder = GridSortOrder.Ascending
            'Me.StaffGrid.MasterTableView.SortExpressions.AddSortExpression(expression)
            'Me.StaffGrid.MasterTableView.Rebind()

        End If
    End Sub

    Protected Sub RadToolTipManager1_AjaxUpdate(ByVal sender As Object, ByVal e As Telerik.Web.UI.ToolTipUpdateEventArgs)
        Me.UpdateToolTip(e.Value, e.UpdatePanel)
    End Sub
    Sub UpdateToolTip(ByVal elementID As String, ByVal panel As UpdatePanel)
        Dim ctrl As Control = Page.LoadControl("~\StaffProfile.ascx")
        panel.ContentTemplateContainer.Controls.Add(ctrl)
        Dim details As StaffProfile = DirectCast(ctrl, StaffProfile)
        details.UserID = CType(elementID, Integer)
    End Sub
    Protected Sub StaffGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles StaffGrid.ItemCommand
        'If (e.CommandName = "Filter") Then
        '    For Each column As GridColumn In e.Item.OwnerTableView.Columns
        '        column.CurrentFilterValue = String.Empty
        '        column.CurrentFilterFunction = GridKnownFunction.NoFilter
        '    Next
        'End If
    End Sub
    Protected Sub StaffGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles StaffGrid.ItemDataBound
        If e.Item.ItemType = GridItemType.AlternatingItem OrElse e.Item.ItemType = GridItemType.Item Then
            Dim requestRowView As DataRowView = TryCast(e.Item.DataItem, DataRowView)
            If requestRowView IsNot Nothing Then
                Dim reqID As Integer = CInt(requestRowView("UserID"))
                Dim gridItem As GridDataItem = TryCast(e.Item, GridDataItem)
                Dim target As New ToolTipTargetControl(gridItem.ClientID, CType(reqID, String), True)
                RadToolTipManager1.TargetControls.Add(target)
            End If
        End If
    End Sub
    Protected Sub StaffGrid_PageIndexChanged(ByVal source As Object, ByVal e As Telerik.Web.UI.GridPageChangedEventArgs) Handles StaffGrid.PageIndexChanged
        ''RadToolTipManager1.TargetControls.Clear()
    End Sub


    Protected Sub StaffGrid_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles StaffGrid.NeedDataSource
        Dim dt As New Data.DataTable()
        dt.Columns.Add("UserID", GetType(String))
        dt.Columns.Add("StaffName", GetType(String))
        dt.Columns.Add("Title", GetType(String))
        dt.Columns.Add("Ext", GetType(String))
        dt.Columns.Add("Office", GetType(String))
        dt.Columns.Add("CostCenter", GetType(String))

        Dim i As Integer = 0
        While i <= 800
            Dim dr As Data.DataRow = dt.NewRow()
            dr(0) = 123456
            dr(1) = "Name" + i.ToString
            dr(2) = "Title" + i.ToString
            dr(3) = "Ext" + i.ToString
            dr(4) = "Office" + i.ToString
            dr(5) = "CostCenter" + i.ToString

            dt.Rows.Add(dr)
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While

        StaffGrid.DataSource = dt

    End Sub
    Protected Sub RadAjaxManager1_AjaxRequest(ByVal sender As Object, ByVal e As AjaxRequestEventArgs) Handles RadAjaxManager1.AjaxRequest
        StaffGrid.PageSize = 14 + StaffGrid.PageSize
        StaffGrid.Rebind()
    End Sub

    Protected Sub txtSearch_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        Dim txt As TextBox = TryCast(DirectCast(sender, TextBox), TextBox)
        Dim list As RadComboBox = DirectCast(((txt.NamingContainer).FindControl("ddlSearch")), RadComboBox)

        Dim [option] As String
        If list.SelectedValue = "StaffName" OrElse list.SelectedValue = "Title" OrElse list.SelectedValue = "Office" OrElse list.SelectedValue = "CostCenter" Then
            [option] = " LIKE "
            '[option] = " = "
        Else
            [option] = " LIKE "

        End If

        Dim filterExpression As String

        If [option] = " = " Then
            filterExpression = "(" + list.SelectedValue + [option] + txt.Text + ")"
        Else
            filterExpression = "(" + list.SelectedValue + [option] + "'%" + txt.Text + "%'" + ")"
        End If

        StaffGrid.MasterTableView.FilterExpression = filterExpression
        StaffGrid.MasterTableView.Rebind()
    End Sub

    Protected Sub btnShowAll_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnShowAll.Click
        StaffGrid.MasterTableView.FilterExpression = ""
        StaffGrid.MasterTableView.Rebind()
    End Sub

End Class
