1. 程式人生 > >產品GridView列表購物車實現(跟上一個結合)

產品GridView列表購物車實現(跟上一個結合)


Partial Class Goods
    Inherits System.Web.UI.Page
    Private sds As SqlDataSource = New SqlDataSource(Dao.ConnectionString, "select * from t_goods")

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("username") = Nothing Then '沒有登陸就不可以訪問
            Response.Redirect("~/Default.aspx") '跳轉
            Return '後面的都不執行
        End If
    End Sub

    Protected Sub gv_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.PreRender '馬上呈現gv的時候觸發
        Me.gv.DataKeyNames = New String() {"goodsguid", "goodsinserttime", "goodsupdatetime"} '資料鍵,gridview的資料鍵不允許編輯
        Me.gv.AllowPaging = True '啟用分頁
        Me.gv.AutoGenerateDeleteButton = True '顯示刪除按鈕
        Me.gv.AutoGenerateEditButton = True '顯示編輯按鈕
        Me.gv.AutoGenerateSelectButton = True '顯示選擇按鈕
        Me.gv.AlternatingRowStyle.BackColor = Drawing.Color.AliceBlue '迭代行顏色
        Me.gv.RowStyle.BackColor = Drawing.Color.AntiqueWhite '行顏色
        Me.gv.SelectedRowStyle.BackColor = Drawing.Color.Aqua '選擇行顏色
        Me.gv.EmptyDataText = "資料庫表內沒有資料" '資料來源為空時顯示的資料 
        Me.gv.DataSource = sds '選擇gv的資料來源
        Me.gv.DataBind() '繫結gv
    End Sub

    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
        Me.gv.EditIndex = e.NewEditIndex '編輯行可用
    End Sub

    Protected Sub gv_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gv.RowCancelingEdit
        Me.gv.EditIndex = -1 '取消編輯
    End Sub

    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        Dim goodsguid As String = Me.gv.DataKeys(e.RowIndex).Values("goodsguid").ToString().Trim() '如果只有一個數據鍵可以用這一句Me.gv.DataKeys(e.RowIndex).Value.ToString().Trim()
        sds.DeleteCommand = "delete from t_goods where goodsguid = @goodsguid" '刪除命令
        sds.DeleteParameters.Add("goodsguid", goodsguid) '引數注入(預處理)
        sds.Delete() '執行刪除動作
        Me.gv.EditIndex = -1 '取消編輯
    End Sub

    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Dim goodsname As String = CType(Me.gv.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text.Trim().ToLower() '取出gv裡面的資料強制轉換成TextBox再取出裡面的Text屬性進行去除空格和大小轉小寫操作
        Dim goodscode As String = CType(Me.gv.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text.Trim().ToLower()
        Dim goodsunit As String = CType(Me.gv.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text.Trim().ToLower()
        Dim goodscate As String = CType(Me.gv.Rows(e.RowIndex).Cells(5).Controls(0), TextBox).Text.Trim().ToLower()
        Dim goodsprice As String = CType(Me.gv.Rows(e.RowIndex).Cells(6).Controls(0), TextBox).Text.Trim().ToLower()
        Dim goodsguid As String = Me.gv.DataKeys(e.RowIndex).Values("goodsguid").ToString().Trim().ToLower() '如果只有一個數據鍵可以用這一句Me.gv.DataKeys(e.RowIndex).Value.ToString().Trim()
        sds.UpdateCommand = "update t_goods set goodsname = @goodsname, goodscode = @goodscode, goodsunit = @goodsunit, goodscate = @goodscate, goodsprice = @goodsprice, goodsupdatetime = getdate() where goodsguid = @goodsguid"
        sds.UpdateParameters.Add("goodsname", goodsname)
        sds.UpdateParameters.Add("goodscode", goodscode)
        sds.UpdateParameters.Add("goodsunit", goodsunit)
        sds.UpdateParameters.Add("goodscate", goodscate)
        sds.UpdateParameters.Add("goodsprice", goodsprice) '不可以用Val()函式轉換為double型別,因為有可能損失精度
        sds.UpdateParameters.Add("goodsguid", goodsguid)
        sds.Update()
        Me.gv.EditIndex = -1
    End Sub

    Protected Sub gv_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles gv.SelectedIndexChanging
        Dim goodsguid As String = Me.gv.Rows(e.NewSelectedIndex).Cells(1).Text.Trim().ToLower() '取出gv裡面的資料強制轉換成TextBox再取出裡面的Text屬性進行去除空格和大小轉小寫操作
        Dim goodsname As String = Me.gv.Rows(e.NewSelectedIndex).Cells(2).Text.Trim().ToLower()  '取出gv裡面的資料強制轉換成TextBox再取出裡面的Text屬性進行去除空格和大小轉小寫操作
        Dim goodscode As String = Me.gv.Rows(e.NewSelectedIndex).Cells(3).Text.Trim().ToLower()
        Dim goodsunit As String = Me.gv.Rows(e.NewSelectedIndex).Cells(4).Text.Trim().ToLower()
        Dim goodscate As String = Me.gv.Rows(e.NewSelectedIndex).Cells(5).Text.Trim().ToLower()
        Dim goodsprice As String = Me.gv.Rows(e.NewSelectedIndex).Cells(6).Text.Trim().ToLower()
        sds.InsertCommand = "insert into t_shopping (goodsguid, goodsname, goodscode, goodsunit, goodscate, goodsprice) values (@goodsguid, @goodsname, @goodscode, @goodsunit, @goodscate, @goodsprice)"
        sds.InsertParameters.Add("goodsguid", goodsguid)
        sds.InsertParameters.Add("goodsname", goodsname)
        sds.InsertParameters.Add("goodscode", goodscode)
        sds.InsertParameters.Add("goodsunit", goodsunit)
        sds.InsertParameters.Add("goodscate", goodscate)
        sds.InsertParameters.Add("goodsprice", goodsprice)
        sds.Insert()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.gv.EditIndex = -1
    End Sub
End Class