Partial Class Goods
Inherits System.Web.UI.Page
Private WithEvents GridView1 As GridView = New GridView()
Private Shared ReadOnly ConnectionString As String = ConfigurationManager.ConnectionStrings("mydata").ConnectionString
'Public Shared ReadOnly ConnectionString As String = "Data Source=.;Initial Catalog=master;Integrated Security=True"
Private sds As SqlDataSource = New SqlDataSource(ConnectionString, "select count(*) from t_user where username = @username and userpass = @userpass and b = 1")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.Cookies("username") Is Nothing Or Request.Cookies("userpass") Is Nothing Then
Response.Redirect("~/Default.aspx")
End If
Dim username As String = Request.Cookies("username").Value
Dim userpass As String = Request.Cookies("userpass").Value
sds.SelectParameters.Add("username", username) '预处理
sds.SelectParameters.Add("userpass", userpass)
Dim dv As System.Data.DataView = sds.Select(DataSourceSelectArguments.Empty)
If dv(0)(0) = 0 Then
Response.Redirect("~/Default.aspx")
End If
Me.Master.FindControl("form1").Controls.Add(GridView1)
End Sub
Protected Sub GridView1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Init
Me.GridView1.AllowPaging = True
Me.GridView1.AutoGenerateDeleteButton = True
Me.GridView1.AutoGenerateEditButton = True
Me.GridView1.AutoGenerateSelectButton = True
Me.GridView1.Caption = "商品管理"
Me.GridView1.DataKeyNames = New String() {"p_goods_id"}
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
Me.GridView1.PageIndex = e.NewPageIndex
Me.GridView1.EditIndex = -1
Me.GridView1.Caption = "第" & e.NewPageIndex + 1 & "页"
End Sub
Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender
sds.SelectCommand = "select * from t_goods order by goodsname asc"
Me.GridView1.DataSource = sds
Me.GridView1.DataBind()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
Me.GridView1.EditIndex = -1
Me.GridView1.Caption = "取消编辑"
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim p_goods_id As String = Me.GridView1.DataKeys(e.RowIndex).Values("p_goods_id").ToString()
sds.DeleteCommand = "delete from t_goods where p_goods_id = @p_goods_id"
sds.DeleteParameters.Add("p_goods_id", p_goods_id) '预处理
Dim i As Integer = sds.Delete()
If i = 0 Then
Return
End If
Me.GridView1.Caption = "删除成功"
Me.GridView1.EditIndex = -1
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
Me.GridView1.EditIndex = e.NewEditIndex
Me.GridView1.Caption = "编辑用户"
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim tb_fenlei As TextBox = DirectCast(Me.GridView1.Rows(e.RowIndex).Cells(1).Controls(0), TextBox)
Dim tb_bianma As TextBox = DirectCast(Me.GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox)
Dim tb_goodsname As TextBox = DirectCast(Me.GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox)
Dim tb_danwei As TextBox = DirectCast(Me.GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox)
Dim tb_danjia As TextBox = DirectCast(Me.GridView1.Rows(e.RowIndex).Cells(5).Controls(0), TextBox)
Dim p_goods_id As String = Me.GridView1.DataKeys(e.RowIndex).Values("p_goods_id").ToString()
sds.UpdateCommand = "update t_goods set fenlei = @fenlei, bianma = @bianma, goodsname = @goodsname, danwei = @danwei, danjia = @danjia where p_goods_id = @p_goods_id"
sds.UpdateParameters.Add("fenlei", tb_fenlei.Text)
sds.UpdateParameters.Add("bianma", tb_bianma.Text)
sds.UpdateParameters.Add("goodsname", tb_goodsname.Text)
sds.UpdateParameters.Add("danwei", tb_danwei.Text)
sds.UpdateParameters.Add("danjia", tb_danjia.Text)
sds.UpdateParameters.Add("p_goods_id", p_goods_id)
Dim i As Integer = sds.Update()
If i = 0 Then
Return
End If
Me.GridView1.Caption = "修改成功"
Me.GridView1.EditIndex = -1
End Sub
Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging
Dim p_user_id As String = Me.GridView1.DataKeys(e.NewSelectedIndex).Values("p_goods_id").ToString()
Me.GridView1.Caption = p_user_id
End Sub
End Class
http://localhost:49431/WebSite/Goods.aspx.vb后台控件绑定
最新推荐文章于 2020-12-24 08:05:48 发布