实验9 图书后台管理模块设计
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验9 图书后台管理模块设计9.1实验目的
1、回顾前面学过的控件的应用方法
2、掌握图书信息的新增、修改和删除方法
9.2实验内容
1、通过本实验加强已学控件的应用方法
2、练习实现图书信息新增、修改、删除功能
3、练习图书后台管理模块的设计方法
9.3实验步骤
9.3.1控件设置
在窗体上放置一个HyperLink用来添加图书信息,GridView展示信息。
图6-1 功能设计图
选择gridview 控件,选中编辑列
设置绑定列
图6-2 HyperLink
添加链接网页 图6-3 编辑gridview 列
设置数据关键字DataKeyNames 为id(主键索引
)
9.3.2数据浏览代码编写
Dim conn As
OleDbConnection
图6-4 绑定列
图6-5 设置gridview 要显示的按钮及显示名称
图6-6 设置主键索引
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
conn = New
OleDbConnection("Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=tuanshuguanli;Data Source=.")
If Not IsPostBack Then
DataBinds()
End If
End Sub
'数据绑定自定义的事件
Sub DataBinds()
'建立Command对象
Dim cmd As New OleDbCommand("select * from bookinfo", conn)
'建立DataAdapter对象
Dim adp As New OleDbDataAdapter(cmd)
'建立DataSet对象
Dim ds As New DataSet()
'填充DataSet对象
adp.Fill(ds, "link")
ds.Tables("link").DefaultView.Sort = sortString
'绑定数据对象
GridView1.DataSource = ds.Tables("link").DefaultView '指定数据源
GridView1.DataBind() '执行绑定End Sub
9.3.3添加信息代码编写
在添加信息页面上添加如下控件,为简化都是TextBox
编写提交按钮代码:
Protected Sub Button1_Click(ByVal sender As Object , ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As OleDbConnection = New
OleDbConnection("Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=tuanshuguanli;Data Source=.") Dim strSql As String
strSql = "insert into bookinfo(Title,Author,Company,ISBN,Price) values('" + Title.Text.Trim + "','" + Author.Text + "','" + Company.Text + "','" + ISBN.Text + "','" + Price.Text + "')"
图6-7 运行结果
图6-8 信息添加控件设置
Dim cmd As OleDbCommand = New OleDbCommand(strSql, conn)
conn.Open()
If cmd.ExecuteNonQuery() Then
Response.Write("成功")
Else
Response.Write("失败")
End If
conn.Close()
End Sub
9.3.4修改、删除、更新代码编写
添加RowEditing 事件
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex
DataBinds()
End Sub
添加RowUpdating 事件
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim strTitle, strauthor, strcompany, strISBN, strprice As String
strTitle = CType(GridView1.Rows(e.RowIndex).Cells(1).Controls(0), TextBox).Text.ToString().Trim()
strauthor = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text.ToString().Trim()
strcompany = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text.ToString().Trim()
strISBN = CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text.ToString().Trim()
strprice = CType(GridView1.Rows(e.RowIndex).Cells(5).Controls(0), TextBox).Text.ToString().Trim()
Dim strSql As String
strSql = "update bookinfo set title='"+ strTitle + "',author='"+ strauthor + "',company='"+ strcompany + "',ISBN='" + strISBN + "',price='" + strprice + "'" strSql += " where id='"+ GridView1.DataKeys(e.RowIndex).Value.ToString() + "'"
conn.Open()
'建立Command对象
Dim cmd As New OleDbCommand(strSql, conn)
If cmd.ExecuteNonQuery() Then
Response.Write("修改成功")
Else
Response.Write("修改失败")
End If
conn.Close()
GridView1.EditIndex = -1
DataBinds()
End Sub
添加取消编辑RowCancelingEdit 事件
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles
GridView1.RowCancelingEdit
GridView1.EditIndex = -1
DataBinds()
End Sub
图6-9 编辑事件运行结果
添加删除RowDeleting事件
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting Dim strSql As String
strSql = "delete from bookinfo"
strSql += " where id='"+ GridView1.DataKeys(e.RowIndex).Value.ToString() + "'"
conn.Open()
'建立Command对象
Dim cmd As New OleDbCommand(strSql, conn)
If cmd.ExecuteNonQuery() Then
Response.Write("删除成功")
Else
Response.Write("删除失败")
End If
conn.Close()
GridView1.EditIndex = -1
DataBinds()
End Sub
9.4思考题
1、如何用单独的页面实现修改图书信息的功能?
2、如何使用验证控件对一些信息进行有效性验证?。