<%@ Page Title="" Language="VB" Debug="true" MasterPageFile="masterform.master" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Dim strSQL As String
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("database/form.mdb") & ";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
If Not Page.IsPostBack() Then
GridViewBindData()
GridView1BindData()
End If
End Sub
Sub GridViewBindData()
strSQL = "SELECT * FROM typeform WHERE Idtypeform = " & Request.QueryString("Idtypeform") & " "
Dim dtReader As OleDbDataReader
objCmd = New OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to GridView ***'
myGridView.DataSource = dtReader
myGridView.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub GridView1BindData()
Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim dtAdapter As New OleDbDataAdapter
Dim ds As New DataSet
Dim strConnString, strSQL As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/form.mdb") & ";Jet OLEDB:Database Password=;"
strSQL = "SELECT * FROM form WHERE Idtypeform = " & Request.QueryString("Idtypeform") & " ORDER BY Idform ASC"
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
'*** BindData to GridView ***'
GridView1.DataSource = ds
GridView1.DataBind()
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Private Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim Nametypeform As HyperLink = CType(e.Row.FindControl("Nametypeform"), HyperLink)
If Not IsNothing(Nametypeform) Then
Nametypeform.Text = e.Row.DataItem("Nametypeform")
Nametypeform.NavigateUrl = "editform.aspx?Idtypeform=" & e.Row.DataItem("Idtypeform")
End If
Dim hplEdit As HyperLink = CType(e.Row.FindControl("hplEdit"), HyperLink)
If Not IsNothing(hplEdit) Then
hplEdit.NavigateUrl = "editform.aspx?Idtypeform=" & e.Row.DataItem("Idtypeform")
End If
Dim lnkDelete As LinkButton = CType(e.Row.FindControl("lnkDelete"), LinkButton)
If Not IsNothing(lnkDelete) Then
lnkDelete.Attributes.Add("OnClick", "return confirm('คุณต้องการลบแบบฟอร์มใช่หรือไม่');")
End If
End Sub
Private Sub myGridView_RowDataBound1(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim Nameform As HyperLink = CType(e.Row.FindControl("Nameform"), HyperLink)
If Not IsNothing(Nameform) Then
Nameform.Text = e.Row.DataItem("Nameform")
Nameform.NavigateUrl = e.Row.DataItem("fileform")
End If
Dim lnkDelete As LinkButton = CType(e.Row.FindControl("lnkDelete"), LinkButton)
If Not IsNothing(lnkDelete) Then
lnkDelete.Attributes.Add("OnClick", "return confirm('คุณต้องการลบไฟล์แบบฟอร์มใช่หรือไม่');")
End If
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting
strSQL = "DELETE FROM form WHERE Idform = " & GridView1.DataKeys.Item(e.RowIndex).Value & ""
objCmd = New OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
GridViewBindData()
GridView1BindData()
myGridView.ShowFooter = True
End Sub
Protected Sub myGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles myGridView.RowUpdating
'*** CustomerID ***'
Dim txtNametypeform As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditNametypeform"), TextBox)
'*** Name ***'
strSQL = "UPDATE typeform SET Nametypeform = '" & txtNametypeform.Text & "' " & _
" WHERE Idtypeform = " & myGridView.DataKeys.Item(e.RowIndex).Value & ""
objCmd = New OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
GridViewBindData()
GridView1BindData()
GridView1.ShowFooter = True
End Sub
Protected Sub myGridView_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles myGridView.RowEditing
myGridView.EditIndex = e.NewEditIndex
myGridView.ShowFooter = False
GridViewBindData()
GridView1BindData()
GridView1.ShowFooter = True
End Sub
Protected Sub myGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) Handles myGridView.RowCancelingEdit
myGridView.EditIndex = -1
GridViewBindData()
GridView1BindData()
GridView1.ShowFooter = True
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
'*** CustomerID ***'
Dim txtNameform As TextBox = CType(GridView1.Rows(e.RowIndex).FindControl("editNameform"), TextBox)
'*** Name ***'
strSQL = "UPDATE form SET Nameform = '" & txtNameform.Text & "' " & _
" WHERE Idform = " & GridView1.DataKeys.Item(e.RowIndex).Value & ""
objCmd = New OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
'*** If Select File Upload ***'
Dim filPicture As HtmlInputFile = CType(GridView1.Rows(e.RowIndex).FindControl("filPicture"), HtmlInputFile)
Dim strFileName As String
If Trim(filPicture.PostedFile.FileName) <> "" Then
strFileName = System.IO.Path.GetFileName(filPicture.Value)
filPicture.PostedFile.SaveAs(Server.MapPath("file/FireDrill52/" & strFileName))
strSQL = "UPDATE form SET Fileform = 'file/FireDrill52/" & strFileName & "' " & _
" WHERE Idform = " & GridView1.DataKeys.Item(e.RowIndex).Value & ""
objCmd = New OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
End If
GridView1.EditIndex = -1
GridViewBindData()
GridView1BindData()
GridView1.ShowFooter = True
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
GridView1.ShowFooter = True
GridViewBindData()
GridView1BindData()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
GridView1.ShowFooter = True
GridViewBindData()
GridView1BindData()
End Sub
Sub GridView1_RowCommand(ByVal source As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Add" Then
Dim inputNameform As TextBox = CType(GridView1.FooterRow.FindControl("inputNameform"), TextBox)
Dim filPicture As HtmlInputFile = CType(GridView1.FooterRow.FindControl("filPicture"), HtmlInputFile)
Dim strFileName As String
If Trim(filPicture.PostedFile.FileName) <> "" Then
strFileName = System.IO.Path.GetFileName(filPicture.Value)
filPicture.PostedFile.SaveAs(Server.MapPath("file/FireDrill52/" & strFileName))
strSQL = "INSERT INTO form (Idtypeform,Nameform,Fileform) " & _
" VALUES ('" & Request.QueryString("Idtypeform") & "','" & inputNameform.Text & "','file/FireDrill52/" & strFileName & "') "
objCmd = New OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
GridViewBindData()
GridView1BindData()
End If
End If
End Sub
Sub ShowPageCommand(ByVal s As Object, ByVal e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
GridView1BindData()
End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<form id="form1" runat="server" enctype="multipart/form-data">
<asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False"
onRowDataBound="myGridView_RowDataBound" DataKeyNames="Idtypeform">
<Columns>
<asp:TemplateField Visible = "false" >
<ItemTemplate>
<asp:Label id="Idtypeform" Visible = "False" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ชื่อแบบฟอร์ม">
<ItemTemplate>
<asp:HyperLink id="Nametypeform" runat="server"></asp:HyperLink>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditNametypeform" size="92" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Nametypeform") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" CancelText="ยกเลิก" EditText="แก้ไข" UpdateText="แก้ไข" HeaderText="แก้ไขแบบฟอร์ม" />
</Columns>
</asp:GridView>
<asp:GridView id="GridView1" runat="server" AutoGenerateColumns="False"
onRowDataBound="myGridView_RowDataBound1" DataKeyNames="Idform"
ShowFooter="True" OnRowCommand="GridView1_RowCommand"
PageSize="15" OnPageIndexChanging="ShowPageCommand" AllowPaging="True">
<Columns>
<asp:TemplateField Visible = "false">
<ItemTemplate>
<asp:Label id="Idform" Visible = "False" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ชื่อไฟล์แบบฟอร์ม">
<ItemTemplate>
<asp:HyperLink id="Nameform" runat="server"></asp:HyperLink>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id="editIdform" runat="server" Visible = "False" text='<%# DataBinder.Eval(Container, "DataItem.Idform") %>'></asp:Label>
<asp:TextBox id="editNameform" runat="server" size="50" text='<%# DataBinder.Eval(Container, "DataItem.Nameform") %>'></asp:TextBox>
<input id="filPicture" type="file" runat="server" />
</EditItemTemplate>
<FooterTemplate >
ชื่อไฟล์แบบฟอร์ม : <asp:TextBox id="inputNameform" runat="server" size="30"></asp:TextBox><br />
อัพโหลดแบบฟอร์ม : <input id="filPicture" type="file" runat="server" />
<asp:Button id="btnAdd" runat="server" Text="เพิ่มไฟล์" CommandName="Add"></asp:Button>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" CancelText="ยกเลิก" EditText="แก้ไข" UpdateText="แก้ไข" HeaderText="แก้ไขแบบฟอร์ม" />
<asp:TemplateField HeaderText="ลบไฟล์แบบฟอร์ม">
<ItemTemplate>
<asp:LinkButton id="lnkDelete" CommandName="Delete" runat="server">ลบ</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</asp:Content>