Imports System.Data
Imports System.Data.SqlClient
Partial Class webboard
Inherits System.Web.UI.Page
Dim conn As SqlConnection
Dim cmd As SqlCommand
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim con_db As String = "server=TAK\SQLEXPRESS; uid=sa; pwd=1234; database=web;"
conn = New SqlConnection(con_db)
conn.Open()
Dim check_admin As String = "select * from admin WHERE admin_user ='" & Session("admin_user") & "' and admin_pass ='" & Session("admin_pass") & "'"
cmd = New SqlCommand(check_admin, conn)
Dim chk As Integer = cmd.ExecuteScalar
If chk >= 1 Then
category_admin()
Else
Response.Write("no")
End If
End Sub
Sub category_admin()
Dim sql As String = "select * from category"
cmd = New SqlCommand(sql, conn)
Dim reader As SqlDataReader
reader = cmd.ExecuteReader
DataList1.DataSource = reader
DataList1.RepeatColumns = "3"
DataList1.DataBind()
reader.Close()
reader = Nothing
End Sub
Public Sub Datalist1_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs) Handles DataList1.ItemDataBound
Dim icon_category As Image = CType(e.Item.FindControl("icon_category"), Image)
If Not IsNothing(icon_category) Then
icon_category.ImageUrl = e.Item.DataItem("icon_category_resize")
End If
Dim lbname_category As Label = CType(e.Item.FindControl("lbname_category"), Label)
If Not IsNothing(lbname_category) Then
lbname_category.Text = e.Item.DataItem("category_name")
End If
Dim lbdetail_category As Label = CType(e.Item.FindControl("lbdetail_category"), Label)
If Not IsNothing(lbdetail_category) Then
lbdetail_category.Text = e.Item.DataItem("category_detail")
End If
Dim edit As LinkButton = CType(e.Item.FindControl("edit"), LinkButton)
If Not IsNothing(edit) Then
edit.CommandName = "Edit"
'edit.PostBackUrl = "webboard.aspx?category_id=" & e.Item.DataItem("category_id")
End If
Dim img_edit As Image = CType(e.Item.FindControl("img_edit"), Image)
If Not IsNothing(img_edit) Then
img_edit.ImageUrl = "/image/Olympiad Icon 13.png"
End If
Dim lbid_category As Label = CType(e.Item.FindControl("lbid_category"), Label)
If Not IsNothing(lbid_category) Then
lbid_category.Text = e.Item.DataItem("category_id")
End If
Dim bt_update As LinkButton = CType(e.Item.FindControl("bt_update"), LinkButton)
If Not IsNothing(bt_update) Then
bt_update.CommandName = "Update"
End If
End Sub
Public Sub Datalist1_EditCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs) Handles DataList1.EditCommand
DataList1.EditItemIndex = e.Item.ItemIndex
category_admin()
End Sub
Public Sub Datalist1_UpdateCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs) Handles DataList1.UpdateCommand
Dim txname As TextBox = CType(e.Item.FindControl("txname"), TextBox)
Dim lbid_category As Label = CType(e.Item.FindControl("lbid_category"), Label)
Dim up_category As String = "UPDATE category SET category_name = @name WHERE category_id ='1'"
cmd = New SqlCommand(up_category, conn)
cmd.Parameters.AddWithValue("name", txname.Text)
cmd.ExecuteNonQuery()
End Sub
Public Sub btlogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btlogin.Click
Dim sql As String = "select * from admin WHERE admin_user ='" & tbxuser.Text & "' AND admin_pass='" & tbxpass.Text & "' "
cmd = New SqlCommand(sql, conn)
Dim check_admin As Integer = cmd.ExecuteScalar
If check_admin >= 1 Then
Session("admin_user") = tbxuser.Text
Session("admin_pass") = tbxpass.Text
Page.RegisterClientScriptBlock("OnUnLoad", "<script>window.location='webboard.aspx'</script>")
Else
Page.RegisterClientScriptBlock("OnClick", "<script>alert('ข้อมูลไม่ถูกต้อง')</script>")
Page.RegisterClientScriptBlock("OnUnLoad", "<script>window.location='webboard.aspx'</script>")
End If
End Sub
End Class