Private Property currentPageIndex() As Integer
Get
Return Convert.ToInt32(ViewState("currentPage"))
End Get
Set(ByVal value As Integer)
ViewState("currentPage") = value
End Set
End Property
Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
Dim id As String = Request.QueryString("job_id")
Dim NoReply As Int32
If txtDescription.Text = "" Or txtReplyName.Text = "" Then
Page.ClientScript.RegisterStartupScript(Me.GetType(), "Window", "alert('เกิดข้อผิดผลาด กรุณาตรวจสอบข้อมลก่อนทำการบันทึกอีกครั้ง!');", True)
'MsgBox("เกิดข้อผิดผลาดกรุณากรอก เบอร์โทรเป็นตัวเลขเท่านั้น", MsgBoxStyle.Question, "error")
Exit Sub
End If
If _conn.State = ConnectionState.Open Then _conn.Close()
_conn.Open()
_strSQL = "SELECT TOP 1 ReplyNo FROM Reply WHERE job_id = '" & id & "' ORDER BY ReplyNo DESC "
_cmd = New SqlCommand(_strSQL, _conn)
Dim dr As SqlDataReader
dr = _cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
NoReply = dr.GetInt32(0)
NoReply += 1
Else
NoReply = 1
End If
dr.Close()
_strSQL = "INSERT INTO Reply(job_id,ReplyNo,Descripion,ReplyName,ReplyDate,IP)VALUES(@job_Reply_id,@ReplyNo,@Descripion,@ReplyName,@ReplyDate,@IP)"
_cmd = New SqlCommand(_strSQL, _conn)
With _cmd
.Parameters.AddWithValue("job_Reply_id", id)
.Parameters.AddWithValue("ReplyNo", NoReply)
.Parameters.AddWithValue("Descripion", txtDescription.Text.Trim)
.Parameters.AddWithValue("ReplyName", txtReplyName.Text.Trim)
.Parameters.AddWithValue("ReplyDate", DateTime.Now)
.Parameters.AddWithValue("IP", IPAddress)
.ExecuteNonQuery()
_conn.Close()
Page.Response.Redirect(Page.Request.Url.ToString(), True)
End With
End Sub
Function IPAddress() As String
Dim ClientIP As String
ClientIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If ClientIP = "" Then
ClientIP = Request.ServerVariables("REMOTE_ADDR")
End If
Return ClientIP
End Function
Public Sub Binddata()
_strSQL = "SELECT ReplyNo, Descripion, ReplyName, ReplyDate, IP FROM Reply WHERE (job_id = '" & Request.QueryString("job_id") & "') ORDER BY ReplyNo DESC"
_cmd = New SqlCommand(_strSQL, _conn)
_da = New SqlDataAdapter(_cmd)
_da.Fill(_ds, "Reply")
Dim dt As DataTable = _ds.Tables("Reply")
Dim dv As DataView = dt.DefaultView
Dim pgNdx As New PagedDataSource()
pgNdx.DataSource = dv
pgNdx.PageSize = 3
pgNdx.AllowPaging = True
DropDownList1.Items.Clear()
For i As Integer = 0 To (pgNdx.PageCount - 1)
DropDownList1.Items.Add(New ListItem(Convert.ToString(i + 1), Convert.ToString(i + 1)))
'DropDownList1.Items.Add(i + 1)
Next
DropDownList1.SelectedIndex = currentPageIndex
pgNdx.CurrentPageIndex = currentPageIndex
btnGoBack.Enabled = pgNdx.IsLastPage
btnGoNext.Enabled = pgNdx.IsFirstPage
DataList2.DataSource = pgNdx
DataList2.DataBind()
End Sub
Private Sub DataList2_ItemDataBound(sender As Object, e As DataListItemEventArgs) Handles DataList2.ItemDataBound
labReplyNo = CType(e.Item.FindControl("IDReplyNo"), Label)
If labReplyNo IsNot Nothing Then
labReplyNo.Text = e.Item.DataItem("ReplyNo")
End If
Dim labDescripion As Label = CType(e.Item.FindControl("IDDescripion"), Label)
If labDescripion IsNot Nothing Then
labDescripion.Text = e.Item.DataItem("Descripion")
End If
Dim labReplyName As Label = CType(e.Item.FindControl("IDReplyName"), Label)
If labReplyName IsNot Nothing Then
labReplyName.Text = e.Item.DataItem("ReplyName")
End If
Dim labReplyDate As Label = CType(e.Item.FindControl("IDReplyDate"), Label)
If labReplyDate IsNot Nothing Then
labReplyDate.Text = e.Item.DataItem("ReplyDate".ToString)
End If
Dim labIP As Label = CType(e.Item.FindControl("IP"), Label)
If labIP IsNot Nothing Then
labIP.Text = e.Item.DataItem("IP".ToString)
End If
End Sub
Private Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
currentPageIndex = Convert.ToInt32(DropDownList1.SelectedValue) - 1
Binddata()
End Sub
Private Sub formview_Load(sender As Object, e As EventArgs) Handles formview.Load
If (Not Page.IsPostBack) Then
Binddata()
If labReplyNo Is Nothing Then
DropDownList1.Visible = False
btnGoBack.Visible = False
btnGoNext.Visible = False
End If
End If
End Sub
Private Sub btnGoBack_Click(sender As Object, e As EventArgs) Handles btnGoBack.Click
currentPageIndex -= 1
Binddata()
End Sub
Private Sub btnGoNext_Click(sender As Object, e As EventArgs) Handles btnGoNext.Click
currentPageIndex += 1
Binddata()
Dim s As Double
End Sub
End Class