Imports System.Data
Imports System.Data.SqlClient
Partial Public Class web_test
Inherits System.Web.UI.Page
Dim cons As String = Class1.Constr3
Dim DA As SqlDataAdapter
Dim DS As DataSet
Dim DT As DataTable
Dim DV As DataView
Dim com As SqlCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim Sql As String = "SELECT * FROM product ORDER BY ProductID"
Dim DA As New SqlDataAdapter(Sql, Cons)
Dim DS As New DataSet
DA.Fill(DS)
GridView1.DataSource = DS
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Try
Dim x() As String = Split(e.CommandArgument, ";")
If e.CommandName = "Mylink" Then
Dim sql As String = "Select ProductID,Productname,Price from product Where ProductID =" & x(0) & "Order by Productname"
Dim DA As New SqlDataAdapter(sql, cons)
Dim DS As New DataSet
DA.Fill(DS)
'DR("ProductID") = DS
'DR("ProductName") = DS
'DR("Price") = DS
' DR("ProductID") = 1
'DR = DT.NewRow()
'DT.Rows.Add(DR)
'Dim DV3 As New DataView(DT)
'DV3.RowStateFilter = DataViewRowState.Added
'If DV3.Count = 0 Then
' dr("ProductID") = DA
' Dim iCount As Integer = DT.Compute("max(ProductID)", "")
' Label1.Text = iCount
' dr("ProductID") = iCount + 1
' dr("Price") = 0
' 'Dr!unitsinstock = 0
'DT.Rows.Add()
'Session("eid") = GridView1.Rows(e.CommandArgument).Cells(0).Text
'// ลองทำจนมั่วไปหมดเเล้วครับรบกวนช่วยทีนะครับ
Me.GridView2.DataBind()
GridView2.DataSource = DS
GridView2.DataBind()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim lnkAddToCart As LinkButton = DirectCast(e.Row.FindControl("เลือก"), LinkButton)
If Not IsNothing(lnkAddToCart) Then
lnkAddToCart.Text = "เลือก"
lnkAddToCart.CommandName = "Mylink"
lnkAddToCart.CommandArgument = e.Row.DataItem("ProductID")
End If
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET, VS 2010 (.NET 4.x)
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Try
If e.CommandName = "Mylink" Then
if viewstate("SelectedID") IsNot Nothing Then
viewstate("SelectedID") &= "," & e.CommandArgument
Else
viewstate("SelectedID") = e.CommandArgument
End If
Dim sql As String = "Select ProductID,Productname,Price from product "
sql &= " Where ProductID In(" & viewstate("SelectedID") & ")"
sql &= " Order by Productname"
' ได้ sql มาแล้ว คราวนี้
' ลองเอา sql นี้ไปอ่านข้อมูลมาแสดงใน GridView2 เองนะครับ
End If
Catch ex As Exception
End Try
End Sub