⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 new.aspx.vb

📁 这是用ASP.NET和SQLserver编写的购物网站的程序
💻 VB
字号:
Imports System.Data.SqlClient

Public Class _new
    Inherits System.Web.UI.Page
    Dim Cstring As String = ConfigurationSettings.AppSettings("ConnectionString")
    Protected WithEvents DList2 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    Dim nwindConn As SqlConnection = New SqlConnection(Cstring)
#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents DList1 As System.Web.UI.WebControls.DropDownList

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        If Not IsPostBack Then
            Try
                If (Session("userpower") <> 2) Then
                    Response.End()
                End If
            Catch ex As Exception
                Response.Write("您不是管理者,不能打开该页,<a href='managerlogin.aspx'>管理员登录</a>")
                Response.End()
            End Try
            Session("userpower") = -1
            DList2.Items.Clear()
            DList2.Items.Add("产品类型编号")
            DList2.Items.Add("产品类型名")

        End If
    End Sub

 

    Private Sub DList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DList1.SelectedIndexChanged
        If DList1.SelectedIndex = 0 Then
            DList2.Items.Clear()
            DList2.Items.Add("产品类型编号")
            DList2.Items.Add("产品类型名")
        Else
            DList2.Items.Clear()
            DList2.Items.Add("产品编号")
            DList2.Items.Add("产品名")
            DList2.Items.Add("产品类型")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If DList1.SelectedIndex = 0 Then
            DataGrid1.DataKeyField = "产品类型号"
        Else
            DataGrid1.DataKeyField = "产品编号"
        End If
        Dim s1 As String
        Dim s2 As String
        Dim s3 As String = TextBox1.Text
        Dim cmd As New SqlCommand
        nwindConn.Open()
        If DList1.SelectedIndex = 0 Then
            s1 = "select productid 产品类型号, name 产品类型名,decn 产品描述 from product where  "
            If DList2.SelectedIndex = 0 Then
                s2 = "productid"
            Else
                s2 = "name"
            End If
        Else
            s1 = "select itemid 产品编号, itemname 产品名,productid 产品类型编号,listprice 价格,qty 库存量 from item where "
            If DList2.SelectedIndex = 0 Then
                s2 = "itemid"
            ElseIf DList2.SelectedIndex = 1 Then
                s2 = "itemname"
            Else
                cmd.CommandText = "select productid from product where name like '%" & TextBox1.Text & "%'"
                cmd.Connection = nwindConn
                s3 = cmd.ExecuteScalar()
                s2 = "productid"
            End If
        End If
        Dim CommandText As String = s1 & s2 & " like '%" & s3 & "%'"
        Dim adp As New SqlClient.SqlDataAdapter(CommandText, nwindConn)
        Dim ds As New DataSet
        adp.Fill(ds)
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()
        nwindConn.Close()
    End Sub

    Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged


    End Sub

    Private Sub DataGrid1_PageIndexChanged1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        Dim s1 As String
        Dim s2 As String
        Dim s3 As String = TextBox1.Text
        Dim cmd As New SqlCommand
        nwindConn.Open()
        If DList1.SelectedIndex = 0 Then
            DataGrid1.DataKeyField = "产品类型号"
        Else
            DataGrid1.DataKeyField = "产品编号"
        End If
        If DList1.SelectedIndex = 0 Then
            s1 = "select productid 产品类型号, name 产品类型名,decn 产品描述 from product where  "
            If DList2.SelectedIndex = 0 Then
                s2 = "productid"
            Else
                s2 = "name"
            End If
        Else
            s1 = "select itemid 产品编号, itemname 产品名,productid 产品类型编号,listprice 价格,qty 库存量 from item where "
            If DList2.SelectedIndex = 0 Then
                s2 = "itemid"
            ElseIf DList2.SelectedIndex = 1 Then
                s2 = "itemname"
            Else
                cmd.CommandText = "select productid from product where name like '%" & TextBox1.Text & "%'"
                cmd.Connection = nwindConn
                s3 = cmd.ExecuteScalar()
                s2 = "productid"
            End If
        End If
        Dim CommandText As String = s1 & s2 & " like '%" & s3 & "%'"
        Dim adp As New SqlClient.SqlDataAdapter(CommandText, nwindConn)
        Dim ds As New DataSet
        adp.Fill(ds)
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()
        nwindConn.Close()

    End Sub



    Private Sub DataGrid1_DeleteCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
        nwindConn.Open()
        Dim s1 As String
        Dim id As String = DataGrid1.DataKeys(e.Item.ItemIndex)
        Dim cmd As New SqlCommand
        If DList1.SelectedIndex = 0 Then
            s1 = "delete product where productid='"
        Else
            s1 = "delete item where itemid='"
        End If
        Try
            cmd.CommandText = s1 & id & "'"
            cmd.Connection = nwindConn
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            Response.Write("<script language='JavaScript'>alert('该产品类型有对应的产品,暂时不能删除');</script>")
        End Try
        Dim s2 As String
        Dim s3 As String = TextBox1.Text
        If DList1.SelectedIndex = 0 Then
            s1 = "select productid 产品类型号, name 产品类型名,decn 产品描述 from product where  "
            If DList2.SelectedIndex = 0 Then
                s2 = "productid"
            Else
                s2 = "name"
            End If
        Else
            s1 = "select itemid 产品编号, itemname 产品名,productid 产品类型编号,listprice 价格,qty 库存量 from item where "
            If DList2.SelectedIndex = 0 Then
                s2 = "itemid"
            ElseIf DList2.SelectedIndex = 1 Then
                s2 = "itemname"
            Else
                cmd.CommandText = "select productid from product where name like '%" & TextBox1.Text & "%'"
                cmd.Connection = nwindConn
                s3 = cmd.ExecuteScalar()
                s2 = "productid"
            End If
        End If
        Dim CommandText As String = s1 & s2 & " like '%" & s3 & "%'"
        Dim adp As New SqlClient.SqlDataAdapter(CommandText, nwindConn)
        Dim ds As New DataSet
        adp.Fill(ds)
        DataGrid1.DataSource = ds
        If DataGrid1.PageCount > 1 And DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1) And DataGrid1.Items.Count = 1 Then
            DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
        End If
        DataGrid1.DataBind()
        nwindConn.Close()
    End Sub

    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
        DataGrid1.EditItemIndex = e.Item.ItemIndex
        nwindConn.Open()
        Dim cmd As New SqlCommand
        Dim s1 As String
        Dim s2 As String
        Dim s3 As String = TextBox1.Text
        If DList1.SelectedIndex = 0 Then
            s1 = "select productid 产品类型号, name 产品类型名,decn 产品描述 from product where  "
            If DList2.SelectedIndex = 0 Then
                s2 = "productid"
            Else
                s2 = "name"
            End If
        Else
            s1 = "select itemid 产品编号, itemname 产品名,productid 产品类型编号,listprice 价格,qty 库存量 from item where "
            If DList2.SelectedIndex = 0 Then
                s2 = "itemid"
            ElseIf DList2.SelectedIndex = 1 Then
                s2 = "itemname"
            Else
                cmd.CommandText = "select productid from product where name like '%" & TextBox1.Text & "%'"
                cmd.Connection = nwindConn
                s3 = cmd.ExecuteScalar()
                s2 = "productid"
            End If
        End If
        Dim CommandText As String = s1 & s2 & " like '%" & s3 & "%'"
        Dim adp As New SqlClient.SqlDataAdapter(CommandText, nwindConn)
        Dim ds As New DataSet
        adp.Fill(ds)
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()
        nwindConn.Close()

    End Sub

    Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
        Dim Id As String = DataGrid1.DataKeys(e.Item.ItemIndex)
        Dim cmd As New SqlCommand
        nwindConn.Open()
        If DList1.SelectedIndex = 0 Then
            Dim textproductid, textproductname, textdecn As TextBox
            textproductid = e.Item.Cells(2).Controls(0)
            textproductname = e.Item.Cells(3).Controls(0)
            textdecn = e.Item.Cells(4).Controls(0)

            Try
                cmd.CommandText = "update product set productid='" & textproductid.Text & "',name='" & textproductname.Text & "',decn='" & textdecn.Text & "' where productid= '" & Id & "'"
                cmd.Connection = nwindConn
                cmd.ExecuteNonQuery()
                DataGrid1.EditItemIndex = -1
            Catch ex As Exception
                Response.Write("<script language='JavaScript'>alert('类型表更新有误,无法完成');</script>")
            End Try

        Else
            Dim textitemid, textitemname, textproductid, textlistprice, textqty As TextBox
            textitemid = e.Item.Cells(2).Controls(0)
            textitemname = e.Item.Cells(3).Controls(0)
            textproductid = e.Item.Cells(4).Controls(0)
            textlistprice = e.Item.Cells(5).Controls(0)
            textqty = e.Item.Cells(6).Controls(0)
            Try
                cmd.CommandText = "update item set itemid='" & textitemid.Text & "',itemname='" & textitemname.Text & "',productid='" & textproductid.Text & "',listprice='" & textlistprice.Text & "',qty='" & textqty.Text & "' where itemid='" & Id & "'"
                cmd.Connection = nwindConn
                cmd.ExecuteNonQuery()
                DataGrid1.EditItemIndex = -1
            Catch ex As Exception
                Response.Write("<script language='JavaScript'>alert('产品表更新有误,无法完成');</script>")
            End Try
        End If
        Dim s1 As String
        Dim s2 As String
        Dim s3 As String = TextBox1.Text
        If DList1.SelectedIndex = 0 Then
            s1 = "select productid 产品类型号, name 产品类型名,decn 产品描述 from product where  "
            If DList2.SelectedIndex = 0 Then
                s2 = "productid"
            Else
                s2 = "name"
            End If
        Else
            s1 = "select itemid 产品编号, itemname 产品名,productid 产品类型编号,listprice 价格,qty 库存量 from item where "
            If DList2.SelectedIndex = 0 Then
                s2 = "itemid"
            ElseIf DList2.SelectedIndex = 1 Then
                s2 = "itemname"
            Else
                cmd.CommandText = "select productid from product where name like '%" & TextBox1.Text & "%'"
                cmd.Connection = nwindConn
                s3 = cmd.ExecuteScalar()
                s2 = "productid"
            End If
        End If
        Dim CommandText As String = s1 & s2 & " like '%" & s3 & "%'"
        Dim adp As New SqlClient.SqlDataAdapter(CommandText, nwindConn)
        Dim ds As New DataSet
        adp.Fill(ds)
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()
        nwindConn.Close()

    End Sub

    Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
        DataGrid1.EditItemIndex = -1
        Dim cmd As New SqlCommand
        nwindConn.Open()
        Dim s1 As String
        Dim s2 As String
        Dim s3 As String = TextBox1.Text
        If DList1.SelectedIndex = 0 Then
            s1 = "select productid 产品类型号, name 产品类型名,decn 产品描述 from product where  "
            If DList2.SelectedIndex = 0 Then
                s2 = "productid"
            Else
                s2 = "name"
            End If
        Else
            s1 = "select itemid 产品编号, itemname 产品名,productid 产品类型编号,listprice 价格,qty 库存量 from item where "
            If DList2.SelectedIndex = 0 Then
                s2 = "itemid"
            ElseIf DList2.SelectedIndex = 1 Then
                s2 = "itemname"
            Else
                cmd.CommandText = "select productid from product where name like '%" & TextBox1.Text & "%'"
                cmd.Connection = nwindConn
                s3 = cmd.ExecuteScalar()
                s2 = "productid"
            End If
        End If
        Dim CommandText As String = s1 & s2 & " like '%" & s3 & "%'"
        Dim adp As New SqlClient.SqlDataAdapter(CommandText, nwindConn)
        Dim ds As New DataSet
        adp.Fill(ds)
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()
        nwindConn.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Session("userpower") = 2
        Response.Redirect("manager.aspx")
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -