1. 程式人生 > >利用GridView實現購物車功能

利用GridView實現購物車功能

Partial Class Shopping
    Inherits System.Web.UI.Page
    Private sds As SqlDataSource = New SqlDataSource(Dao.ConnectionString, "select shoppingguid, goodsname, goodscode, goodsunit, goodscate, goodsprice, shoppingshuliang, goodsinserttime, goodsupdatetime from t_shopping")

    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() {"shoppingguid", "goodsname", "goodscode", "goodsunit", "goodscate", "goodsprice", "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 shoppingguid As String = Me.gv.DataKeys(e.RowIndex).Values("shoppingguid").ToString().Trim() '如果只有一個數據鍵可以用這一句Me.gv.DataKeys(e.RowIndex).Value.ToString().Trim()
        sds.DeleteCommand = "delete from t_shopping where shoppingguid = @shoppingguid" '刪除命令
        sds.DeleteParameters.Add("shoppingguid", shoppingguid) '引數注入(預處理)
        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 shoppingshuliang As String = CType(Me.gv.Rows(e.RowIndex).Cells(7).Controls(0), TextBox).Text.Trim().ToLower()
        Dim shoppingguid As String = Me.gv.DataKeys(e.RowIndex).Values("shoppingguid").ToString().Trim() '如果只有一個數據鍵可以用這一句Me.gv.DataKeys(e.RowIndex).Value.ToString().Trim()
        sds.UpdateCommand = "update t_shopping set shoppingshuliang = @shoppingshuliang where shoppingguid = @shoppingguid"
        sds.UpdateParameters.Add("shoppingshuliang", shoppingshuliang) '引數注入(預處理)
        sds.UpdateParameters.Add("shoppingguid", shoppingguid) '引數注入(預處理)
        sds.Update() '執行更新動作
        Me.gv.EditIndex = -1 '取消編輯
    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