Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Server=localhost\SQLExpress;Database=Myweb;Trusted_Connection=True;"
objConn = New SqlConnection(strConnString)
objConn.Open()
IF Not Page.IsPostBack() Then
BindData()
End IF
End Sub
Sub BindData()
strSQL = "SELECT * FROM PLASMA"
Dim dtReader As SqlDataReader
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to GridView ***'
myGridView.DataSource = dtReader
myGridView.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Sub modEditCommand(sender As Object, e As GridViewEditEventArgs)
myGridView.EditIndex = e.NewEditIndex
myGridView.ShowFooter = False
BindData()
End Sub
Sub modCancelCommand(sender As Object, e As GridViewCancelEditEventArgs)
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
End Sub
Sub modDeleteCommand(sender As Object, e As GridViewDeleteEventArgs)
strSQL = "DELETE FROM PLASMA WHERE PartID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
BindData()
End Sub
Sub myGridView_RowCommand(source As Object, e As GridViewCommandEventArgs)
If e.CommandName = "Add" Then
'*** PartID ***'
Dim txtPartID As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartID"), TextBox)
'*** PartNo ***'
Dim txtPartNo As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartNo"), TextBox)
'*** PartSort ***'
Dim ddlAddPartSort As DropDownList = CType(myGridView.FooterRow.FindControl("ddlAddPartSort"), DropDownList)
'*** PartName ***'
Dim txtPartName As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartName"), TextBox)
'*** PartModel ***'
Dim txtPartModel As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartModel"), TextBox)
'*** CustomerPrice ***'
Dim txtCustomerPrice As TextBox = CType(myGridView.FooterRow.FindControl("txtAddCustomerPrice"), TextBox)
'*** PartSub ***'
Dim txtPartSub As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartSub"), TextBox)
strSQL = "INSERT INTO PLASMA (PartNo,PartSort,PartName,PartModel,CustomerPrice,PartSub) " & _
" VALUES ('" & txtPartNo.Text & "','" & ddlAddPartSort.SelectedItem.Value & "','" & txtPartName.Text & "' " & _
" ,'" & txtPartModel.Text & "','" & txtCustomerPrice.Text & "','" & txtPartSub.Text & "') "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
BindData()
End If
End Sub
Sub modUpdateCommand(s As Object, e As GridViewUpdateEventArgs)
'*** PartID ***'
Dim txtPartID As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartID"), TextBox)
'*** PartNo ***'
Dim txtPartNo As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartNo"), TextBox)
'*** PartSort ***'
Dim ddlAddPartSort As DropDownList = CType(myGridView.Rows(e.RowIndex).FindControl("ddlAddPartSort"), DropDownList)
'*** PartName ***'
Dim txtPartName As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartName"), TextBox)
'*** PartModel ***'
Dim txtPartModel As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartModel"), TextBox)
'*** CustomerPrice ***'
Dim txtCustomerPrice As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditCustomerPrice"), TextBox)
'*** PartSub ***'
Dim txtPartSub As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartSub"), TextBox)
strSQL = "UPDATE PLASMA SET PartID = '" & txtPartID.Text & "' " & _
" ,PartNo = '" & txtPartNo.Text & "' " & _
" ,PartSort = '" & ddlAddPartSort.SelectedItem.Value & "' " & _
" ,PartName = '" & txtPartName.Text & "' " & _
" ,PartModel = '" & txtPartModel.Text & "' " & _
" ,CustomerPrice = '" & txtCustomerPrice.Text & "' " & _
" ,PartSub = '" & txtPartSub.Text & "' " & _
" WHERE PartID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
End Sub
คือจะให้คอลัมน์ PartModel เป็นคอลัมน์ที่สามารถ
คลิกเพื่อให้แสดงรูปภาพขึ้นมา ไม่รู้ว่าใช้เป็น link แบบนี้ได้เลยรึป่าว
แล้วต้องเขียนโค้ดยังไงดึงรูปขึ้นมาคะ??
Tag : .NET, Ms SQL Server 2005, Web (ASP.NET), VB.NET, VS 2005 (.NET 2.x)
ทำใน RowDataBound() ครับ แล้วสร้าง Control เป็น Image ไว้ครับ
Code (VB.NET)
Dim hplView As HyperLink = DirectCast(e.Row.FindControl("hplView"), Label)
If hplView IsNot Nothing Then
hplView.NavigateUrl = DataBinder.Eval(e.Row.DataItem, "Picture").ToString()
End If