Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Partial Class material
Inherits System.Web.UI.Page
Public Msg As String = ""
Dim dt As DataTable
Dim dt2 As DataTable
Dim dt3 As DataTable
Dim dtRev As DataTable
Dim dthisrev As DataTable
Public Sub ShowMsg(ByVal txt As String)
Msg = txt
End Sub
Public Function DisplayAJAXMessage(ByVal page As Control, ByVal msg As String) As String 'function alert
Dim myScript As String = [String].Format("alert('{0}');", msg)
ScriptManager.RegisterStartupScript(page, page.[GetType](), "MyScript", myScript, True)
Return msg
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Me.IsPostBack Then
dt = CType(Session("data"), DataTable)
dtRev = CType(Session("matRev"), DataTable)
dthisrev = CType(Session("hisRev"), DataTable)
Else
'Dim sql1 As New CSQLServer
Me.GetMyDataAll_mat()
Me.MyDataBind(dg1, dt, "id_mat")
Call GetMyDataToDropDown("tbl_employee", "fullname", "id_emp", "id_emp", ddlemp, dt2)
Call GetMyDataToDropDown("tbl_material", "name_mat", "id_mat", "id_mat", ddlmat, dt3)
Call GetMyDataToDropDown("tbl_employee", "fullname", "id_emp", "id_emp", ddlemprev, dt2)
txtidmat.Text = ddlmat.Items(0).Value.ToString '// เพื่อให้หน้ารับอุปกรณ์ แสดงรหัสอุปกรณ์รายการแรกเลย
'// create colunm for dtrev
If (Me.Session("matRev") Is Nothing) Then
dtRev = New DataTable()
dtRev.Columns.Add(New DataColumn("id_mat", GetType(System.String)))
dtRev.Columns.Add(New DataColumn("name_mat", GetType(System.String)))
dtRev.Columns.Add(New DataColumn("rev_code_mat", GetType(System.String)))
dtRev.Columns.Add(New DataColumn("date_mat", GetType(System.String)))
dtRev.Columns.Add(New DataColumn("emp_rev", GetType(System.String)))
dtRev.Columns.Add(New DataColumn("qty_mat", GetType(System.String)))
dtRev.Columns.Add(New DataColumn("mat_from", GetType(System.String)))
dtRev.Columns.Add(New DataColumn("sn", GetType(System.String)))
dtRev.Columns.Add(New DataColumn("id_emp", GetType(System.String)))
Me.Session("matRev") = dtRev
Else
dtRev = CType(Me.Session("matRev"), DataTable)
End If
'// load data from tbl_rev_material
If (Me.Session("hisRev") Is Nothing) Then
Call GetHisRevAll_mat()
Else
dthisrev = CType(Me.Session("hisRev"), DataTable)
End If
'dg1.Columns(8).Visible = False
txtHisSearch.Enabled = False
btnHisSearch.Enabled = False
'// add event confirm msgbox to button
btnDelRev.Attributes.Add("onclick", "javascript:return " & "confirm('คุณต้องการลบรายการที่เลือกใช่หรือไม่?')")
btnhisDel.Attributes.Add("onclick", "javascript:return " & "confirm('คุณต้องการลบรายการที่เลือกใช่หรือไม่?')")
'// select first view
MulV1.ActiveViewIndex = 0
End If
End Sub
Public Sub GetMyDataAll_mat(Optional ByVal iSort As String = "id_mat")
Dim mat As New Cmaterial
dt = mat.get_all_material2()
ViewState("Sort") = iSort
dg1.EditItemIndex = -1
dg1.CurrentPageIndex = 0 'กลับไปที่หน้าแรก
Session("data") = dt
mat = Nothing
End Sub
Private Sub GetHisRevAll_mat()
dthisrev = New DataTable()
Dim mat As New Cmaterial
dthisrev = mat.get_all_HisRev
Call MyDataBind(dgvhisrev, dthisrev, "id_rev")
Session("hisRev") = dthisrev
mat = Nothing
End Sub
Public Sub MyDataBind(ByVal objDataGrid As DataGrid, ByVal objDataTable As DataTable, ByVal DataKey As String)
objDataGrid.DataSource = objDataTable
objDataGrid.DataKeyField = DataKey
objDataGrid.SelectedIndex = -1
objDataGrid.DataBind()
End Sub
Protected Sub btnAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAll.Click
MulV1.ActiveViewIndex = 0
End Sub
Protected Sub btnRev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRev.Click
MulV1.ActiveViewIndex = 1
If dtRev.Rows.Count > 0 Then
dgRev.Visible = True
End If
End Sub
Protected Sub btnHisRev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHisRev.Click
MulV1.ActiveViewIndex = 2
End Sub
Protected Sub dg1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg1.CancelCommand
dg1.EditItemIndex = -1
Me.MyDataBind(dg1, dt, "id_mat")
End Sub
Protected Sub dg1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg1.DeleteCommand
Dim dr As DataRow = dt.Rows(e.Item.DataSetIndex)
dr.Delete()
Dim mymat As New Cmaterial
mymat.mat_id = CStr(dg1.DataKeys(e.Item.ItemIndex))
Dim ck As Boolean = mymat.delete_material()
If ck = True Then
dr.AcceptChanges()
Me.MyDataBind(dg1, dt, "id_mat")
Else
dr.RejectChanges()
ShowMsg("ไม่สามารถลบข้อมูลได้")
End If
End Sub
Protected Sub dg1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg1.EditCommand
Dim row() As DataRow = dt.Select("id_mat='" & e.Item.Cells(0).Text & "'")
Me.txtid.Text = e.Item.Cells(0).Text
Me.txtid.ReadOnly = True
Me.txtName.Text = e.Item.Cells(1).Text
Me.txtqty.Text = e.Item.Cells(2).Text
Me.txtmodifyin.Text = CStr(IIf(e.Item.Cells(3).Text = " ", "", e.Item.Cells(3).Text))
Me.txtmodifyout.Text = CStr(IIf(e.Item.Cells(4).Text = " ", "", e.Item.Cells(4).Text))
Me.ddlemp.SelectedValue = e.Item.Cells(5).Text
Me.txtnote.Text = row(0)("note_mat").ToString
lblEdit.Text = "แก้ไขข้อมูลอุปกรณ์รหัส : " & e.Item.Cells(0).Text
Session("SelectID") = e.Item.Cells(0).Text
Session("Option") = "Edit"
MulV1.ActiveViewIndex = 3 '// Goto vEdit for Update
End Sub
Protected Sub dg1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg1.ItemDataBound
'// Show LightBox when click
Dim hplDetail As HyperLink = CType(e.Item.FindControl("hplDetail"), HyperLink)
If Not IsNothing(hplDetail) Then
hplDetail.NavigateUrl = "LightboxDetails.aspx?id_mat=" & e.Item.Cells(0).Text & "&height=180&width=350"
hplDetail.CssClass = "thickbox"
hplDetail.Attributes.Add("title", e.Item.Cells(1).Text)
End If
'// show Messagebox Confirm before Delete
Dim imgDel As ImageButton = CType(e.Item.FindControl("imgDel"), ImageButton)
If Not IsNothing(imgDel) Then
imgDel.Attributes.Add("onclick", "javascript:return " & "confirm('คุณต้องการลบข้อมูล " & e.Item.Cells(1).Text & " ใช่หรือไม่?')")
End If
End Sub
Protected Sub btnCancelEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelEdit.Click
MulV1.ActiveViewIndex = 0
Session.Remove("Option")
End Sub
Protected Sub btnSummitEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSummitEdit.Click
Dim mymat As New Cmaterial
Select Case CType(Session("Option"), String)
Case "Edit"
With mymat
.mat_id = CType(Session("SelectID"), String)
.mat_name = txtName.Text
.mat_note = txtnote.Text
.mat_id_emp = CType(ddlemp.SelectedValue, String)
Dim ck As Boolean
ck = mymat.update_detail_material()
If ck = False Then
Call DisplayAJAXMessage(Page, "แก้ไขข้อมูลอุปกรณ์ " & .mat_id & " ไม่สำเร็จ!!!")
GoTo EVENT_FAIL
End If
End With
Case "Add"
With mymat
.mat_id = txtid.Text
.mat_name = txtName.Text
.mat_note = txtnote.Text
.mat_qty = 0
.mat_id_emp = CType(ddlemp.SelectedValue, String)
Dim ck As Integer
ck = mymat.add_material()
If ck = 1 Then
Call DisplayAJAXMessage(Page, "เพิ่มข้อมูลอุปกรณ์ " & .mat_id & " ไม่สำเร็จ!!!")
GoTo EVENT_FAIL
End If
If ck = 2 Then
Call DisplayAJAXMessage(Page, "มีข้อมูลอุปกรณ์รหัส " & .mat_id & " อยู่แล้วกรุณาตรวจสอบ!!!")
GoTo EVENT_FAIL
End If
End With
End Select
EVENT_SUCCESS:
'// Refresh Data
'Call GetMyDataAll_mat()
'Call MyDataBind(dg1, dt, "id_mat")
'// clear mem
mymat = Nothing
Session.Remove("SelectID")
Session.Remove("Option")
MulV1.ActiveViewIndex = 0
EVENT_FAIL:
'// Nothing
End Sub
Protected Sub btnNewMat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewMat.Click
Me.txtid.Text = ""
Me.txtid.ReadOnly = False
Me.txtName.Text = ""
Me.txtqty.Text = "0"
Me.txtmodifyin.Text = ""
Me.txtmodifyout.Text = ""
Me.ddlemp.SelectedIndex = 0
Me.txtnote.Text = ""
lblEdit.Text = "เพิ่มข้อมูลชนิดอุปกรณ์ใหม่ "
Session("Option") = "Add"
MulV1.ActiveViewIndex = 3 '// Goto vEdit for Insert
End Sub
Protected Sub ddlmat_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlmat.SelectedIndexChanged
txtidmat.Text = CType(ddlmat.SelectedValue, String)
End Sub
Private Sub clear_vRev()
Me.txtcoderev.Text = ""
Me.txtdaterev.Text = ""
Me.txttimerev.Text = ""
Me.txtqtyrev.Text = ""
Me.txtfrom.Text = ""
Me.txtsn.Text = ""
dtRev.Rows.Clear()
dgRev.DataSource = dtRev
dgRev.DataBind()
Session.Remove("Option")
MulV1.ActiveViewIndex = 0
End Sub
Protected Sub btnCencelRev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCencelRev.Click
Call clear_vRev()
End Sub
Protected Sub btnSummitRev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSummitRev.Click
Dim mat As New Cmaterial
Dim sql As New CSQLServer
Dim ck As Boolean
If dtRev.Rows.Count <= 0 Then GoTo event_error
For i As Integer = 0 To dtRev.Rows.Count - 1
mat.mat_id = dgRev.Items(i).Cells(0).Text
mat.mat_lastmodify_in = CDate(txtdaterev.Text & " " & txttimerev.Text)
mat.mat_id_emp = dgRev.Items(i).Cells(8).Text
mat.mat_name = dgRev.Items(i).Cells(1).Text
'mat.mat_serialNo = dgRev.Items(i).Cells(7).Text
mat.mat_from = dgRev.Items(i).Cells(5).Text
mat.mat_code_rev = dgRev.Items(i).Cells(6).Text
ck = mat.update_qty_material(sql, CType(dgRev.Items(i).Cells(3).Text, Integer), "+", i, dtRev.Rows.Count - 1)
If ck = False Then
Call ShowRevMsg("เกิดข้อผิดพลาดในการเพิ่มข้อมูล!! ข้อมูลยังไม่ได้รับการบันทึก")
GoTo event_error
End If
Next
Me.GetMyDataAll_mat()
Me.MyDataBind(dg1, dt, "id_mat")
Call clear_vRev()
event_error:
'// clear mem
mat = Nothing
sql = Nothing
End Sub
Private Sub ShowRevMsg(ByVal txt As String)
lbRevStatus.Text = txt
lbRevStatus.Visible = True
End Sub
Protected Sub btnAddRev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRev.Click
Try
lbRevStatus.Visible = False
If Me.txtqtyrev.Text = "" Then
Call ShowRevMsg("กรุณาระบุจำนวน" & " ")
Exit Sub
End If
If Me.txtdaterev.Text = "" Then
Call ShowRevMsg("กรุณาระบุวันที่รับ" & " ")
Exit Sub
End If
If Me.txttimerev.Text = "" Then
Call ShowRevMsg("กรุณาระบุเวลาที่รับ" & " ")
Exit Sub
End If
If Me.txtfrom.Text = "" Then
Call ShowRevMsg("กรุณาระบุผู้ส่ง" & " ")
Exit Sub
End If
If Me.txtcoderev.Text = "" Then
Call ShowRevMsg("กรุณาเลขที่ใบส่งของ" & " ")
Exit Sub
End If
With dgRev
If .Items.Count > 0 Then
For i As Integer = .Items.Count - 1 To 0 Step -1
If (txtidmat.Text = .Items(i).Cells(0).Text) Then
Call ShowRevMsg("มีรายการอยู่แล้ว" & " ")
Exit Sub
End If
Next
End If
End With
Dim dr As DataRow = dtRev.NewRow()
dr(0) = txtidmat.Text
dr(1) = ddlmat.SelectedItem.Text
dr(2) = txtcoderev.Text
dr(3) = txtdaterev.Text & " " & txttimerev.Text
dr(4) = ddlemprev.SelectedItem.Text
dr(5) = txtqtyrev.Text
dr(6) = txtfrom.Text
dr(8) = ddlemprev.SelectedItem.Value.ToString
dtRev.Rows.Add(dr)
dgRev.DataSource = dtRev
dgRev.DataBind()
Catch
ShowMsg(Err.Description)
End Try
End Sub
Protected Sub btnDelRev_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Try
With dgRev
If .Items.Count > 0 Then
For i As Integer = .Items.Count - 1 To 0 Step -1
Dim ck As CheckBox = CType(.Items(i).Cells(8).FindControl("ckbox"), CheckBox)
If ck.Checked = True Then
dtRev.Rows.RemoveAt(i)
End If
Next
End If
dgRev.DataSource = dtRev
dgRev.DataBind()
End With
Catch
End Try
End Sub
Protected Sub dgRev_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgRev.DataBinding
If (dtRev.Rows.Count) <= 0 Then
dgRev.Visible = False
Else
dgRev.Visible = True
End If
End Sub
Protected Sub btnselall_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnselall.Click
Call Check_all(dgRev, "ckbox", 8)
End Sub
Protected Sub btnunselall_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnunselall.Click
Call Uncheck_all(dgRev, "ckbox", 8)
End Sub
Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
Me.txtdaterev.Text = Format(CDate(Calendar1.SelectedDate), "dd/MM/yyyy")
End Sub
Protected Sub dgvhisrev_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgvhisrev.EditCommand
dgvhisrev.EditItemIndex = e.Item.ItemIndex 'user select row
End Sub
Protected Sub dgvhisrev_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgvhisrev.PageIndexChanged
dgvhisrev.CurrentPageIndex = e.NewPageIndex
dgvhisrev.EditItemIndex = -1
dgvhisrev.SelectedIndex = -1
Me.MyDataBind(dgvhisrev, dthisrev, "id_rev")
End Sub
Protected Sub ddlchoice_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If ddlchoice.Text <> "------ทั้งหมด------" Then
txtHisSearch.Enabled = True
btnHisSearch.Enabled = True
Else
txtHisSearch.Enabled = False
btnHisSearch.Enabled = False
Dim mat As New Cmaterial
dthisrev = mat.get_all_HisRev
Call MyDataBind(dgvhisrev, dthisrev, "id_rev")
Session.Remove("hisRev")
Session("hisRev") = dthisrev
End If
End Sub
Protected Sub btnHisSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHisSearch.Click
Dim mat As New Cmaterial
Select Case ddlchoice.Text
Case "รหัสอุปกรณ์" : dthisrev = mat.get_filter_HisRev("id_mat", txtHisSearch.Text)
Case "วันที่รับ" : dthisrev = mat.get_filter_HisRev("date_rev", txtHisSearch.Text)
Case "บริษัทผู้ส่ง" : dthisrev = mat.get_filter_HisRev("from_rev", txtHisSearch.Text)
Case "หมายเลขใบส่งของ" : dthisrev = mat.get_filter_HisRev("code_doc_rev", txtHisSearch.Text)
End Select
Call MyDataBind(dgvhisrev, dthisrev, "id_rev")
Session.Remove("hisRev")
Session("hisRev") = dthisrev
mat = Nothing
End Sub
Protected Sub btnhisCheckall_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnhisCheckall.Click
Call Check_all(dgvhisrev, "ck_hisselect", 8)
End Sub
Protected Sub btnhisUncheclall_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnhisUncheclall.Click
Call Uncheck_all(dgvhisrev, "ck_hisselect", 8)
End Sub
Protected Sub btnhisDel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim mat As New Cmaterial
Try
With dgvhisrev
If .Items.Count > 0 Then
For i As Integer = .Items.Count - 1 To 0 Step -1
Dim ck As CheckBox = CType(.Items(i).Cells(8).FindControl("ck_hisselect"), CheckBox)
Dim id As String = .Items(i).Cells(0).Text
mat.mat_id = .Items(i).Cells(1).Text
mat.mat_name = .Items(i).Cells(2).Text
mat.mat_qty = CType(.Items(i).Cells(3).Text, Integer)
mat.mat_id_emp = .Items(i).Cells(9).Text
mat.mat_date_rev = CDate(.Items(i).Cells(5).Text)
mat.mat_from = .Items(i).Cells(6).Text
mat.mat_code_rev = .Items(i).Cells(7).Text
If ck.Checked = True Then
If mat.delete_hisrev(id) = False Then
lblStatusHis.Text = "เกิดข้อผิดพลาดในการลบข้อมูล !!!"
Else
lblStatusHis.Text = ""
End If
End If
Next
End If
Call GetHisRevAll_mat()
End With
mat = Nothing
Catch
mat = Nothing
End Try
End Sub
Private Sub Check_all(ByVal objDataGrid As DataGrid, ByVal ID As String, ByVal cellnumber As Integer)
Try
With objDataGrid
For i As Integer = 0 To .Items.Count - 1
DirectCast(.Items(i).Cells(cellnumber).FindControl(ID), CheckBox).Checked = True
Next
End With
Catch
Dim strerr As String = Err.Description
End Try
End Sub
Private Sub Uncheck_all(ByVal objDataGrid As DataGrid, ByVal ID As String, ByVal cellnumber As Integer)
Try
With objDataGrid
For i As Integer = 0 To .Items.Count - 1
DirectCast(.Items(i).Cells(cellnumber).FindControl(ID), CheckBox).Checked = False
Next
End With
Catch
Dim strerr As String = Err.Description
End Try
End Sub
Protected Sub MulV1_ActiveViewChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MulV1.ActiveViewChanged
If MulV1.ActiveViewIndex = 0 Then
Call GetMyDataAll_mat()
Call MyDataBind(dg1, dt, "id_mat")
End If
End Sub
End Class
Tag : .NET, Ajax, jQuery, Web (ASP.NET), VB.NET, VS 2005 (.NET 2.x)
Protected Sub dg1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg1.DeleteCommand
Dim dr As DataRow = dt.Rows(e.Item.DataSetIndex)
dr.Delete()
Dim mymat As New Cmaterial
mymat.mat_id = CStr(dg1.DataKeys(e.Item.ItemIndex))
Dim ck As Boolean = mymat.delete_material()
If ck = True Then
dr.AcceptChanges()
Me.MyDataBind(dg1, dt, "id_mat")
Else
dr.RejectChanges()
ShowMsg("ไม่สามารถลบข้อมูลได้")
End If
End Sub