1、ASP实验报告四ASP.NET程序设计实验报告四实验题目:数据源控件与数据绑定控件指导老师:马浩专业班级:计算机科学与技术系1105班 姓 名:王媛(2011100529) 2014年5月24日一、实验题目 数据源的控件与数据绑定控件二、实验目的1、掌握在SQL Server 2005中创建数据的创建、备份。.2、掌握数据源控件sqldatasource的设置和使用。3、掌握常用数据绑定控件gridview、formview、listview的使用。4、掌握以上控件实现对数据库的增删改查。三、实验代码及截图实验代码:1、idviewtest.aspxports System.DataImpor
2、ts System.Data.SqlClientPartial Class _1101_gridviewtest Inherits System.Web.UI.Page Dim conn As SqlConnection Dim connstr As String = Data Source=.;Initial Catalog=aspnet;Integrated Security=SSPI;Pooling=False Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.L
3、oad If Not IsPostBack Then ViewState(sortorder) = id ViewState(orderdire) = asc bind() End If End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Response.Redirect(adduser.aspx) End Sub Protected Sub GridView1_RowDeleting(ByVal sender As Obj
4、ect, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting Dim conn As New SqlConnection() conn = New SqlConnection(connstr) Dim cmd As New SqlCommand() conn.ConnectionString = Data Source=.;Initial Catalog=aspnet;Integrated Security=SSPI;Pooling=False Dim sqlst
5、r = delete from student where id= & GridView1.DataKeys(e.RowIndex).Value.ToString() & conn.Open() cmd.Connection = conn cmd.CommandText = sqlstr cmd.ExecuteNonQuery() conn.Close() bind() End Sub Public Sub bind() Dim conn As New SqlConnection() conn = New SqlConnection(connstr) Dim sqlstr As String
6、= select * from student conn.ConnectionString = Data Source=.;Initial Catalog=aspnet;Integrated Security=SSPI;Pooling=False Dim ds As New DataSet() conn.Open() Dim objadapter As New SqlDataAdapter(sqlstr, conn) objadapter.Fill(ds, student) Dim view As DataView = ds.Tables(student).DefaultView Dim so
7、rt As String = ViewState(sortorder).ToString & & ViewState(orderdire).ToString view.Sort = sort GridView1.DataSource = view GridView1.DataBind() conn.Close() End Sub Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridVie
8、w1.RowEditing GridView1.EditIndex = e.NewEditIndex bind() Dim drop_str As String = CType(GridView1.Rows(e.NewEditIndex).FindControl(HiddenField1), HiddenField).Value.ToString CType(GridView1.Rows(e.NewEditIndex).FindControl(DropDownList1), DropDownList).Text = drop_str End Sub Protected Sub GridView
9、1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim xuehao As TextBox = GridView1.Rows(e.RowIndex).Cells(1).Controls(0) Dim name As TextBox = GridView1.Rows(e.RowIndex).Cells(2).Controls(0) Dim addr As String = CType(G
10、ridView1.Rows(e.RowIndex).Cells(3).Controls(1), DropDownList).SelectedValue.ToString Dim tel As TextBox = GridView1.Rows(e.RowIndex).Cells(4).Controls(0) Dim sqlstr As String = update student set xuehao= & xuehao.Text.Trim() & ,name= & name.Text.Trim() & ,addr= & addr.Trim() & ,tel= & tel.Text.Trim(
11、) & where id= & GridView1.DataKeys(e.RowIndex).Value.ToString() & Dim conn As New SqlConnection() conn = New SqlConnection(connstr) Dim cmd As New SqlCommand() conn.ConnectionString = Data Source=.;Initial Catalog=aspnet;Integrated Security=SSPI;Pooling=False Dim sqlstr = delete from student where i
12、d= & GridView1.DataKeys(e.RowIndex).Value.ToString() & conn.Open() cmd.Connection = conn cmd.CommandText = sqlstr cmd.ExecuteNonQuery() conn.Close() GridView1.EditIndex = -1 bind() End Sub Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewC
13、ancelEditEventArgs) Handles GridView1.RowCancelingEdit GridView1.EditIndex = -1 bind() End Sub Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging GridView1.PageIndex = e.NewPageIndex End Su
14、b Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged bind() End Sub Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting Dim spage As
15、 String = e.SortExpression If ViewState(sortorder) = spage Then If ViewState(orderdire) = asc Then ViewState(orderdire) = desc Else ViewState(orderdire) = asc End If Else ViewState(sortorder) = e.SortExpression End If bind() End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal
16、e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Alternate Then CType(e.Row.Cells(6).Controls(0), LinkButton).Attributes.Add(on
17、click, javascript:return confirm(你确认删除 & e.Row.Cells(2).Text & 吗?) End If End If Dim i As Integer For i = 0 To GridView1.Rows.Count If e.Row.RowType = DataControlRowType.DataRow Then e.Row.Attributes.Add(onmouseover, c=this.style.backgroundColor;this.style.backgroundColor=#00A9FF) e.Row.Attributes.A
18、dd(onmouseout, this.style.backgroundColor=c) End If Next i End Sub Protected Sub quanxuan_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles quanxuan.CheckedChanged Dim i As Integer For i = 0 To GridView1.Rows.Count - 1 Dim cbox As CheckBox = GridView1.Rows(i).FindControl(Ch
19、eckBox1) If quanxuan.Checked Then cbox.Checked = True Else cbox.Checked = False End If Next End Sub Protected Sub delete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles delete.Click conn = New SqlConnection(connstr) Dim cmd As SqlCommand Dim i As Integer For i = 0 To GridView1.Row
20、s.Count - 1 Dim cbox As CheckBox = GridView1.Rows(i).FindControl(CheckBox1) If cbox.Checked Then Dim sqlstr As String = delete from student where id= & GridView1.DataKeys(i).Value.ToString & cmd = New SqlCommand(sqlstr, conn) conn.Open() cmd.ExecuteNonQuery() conn.Close() End If Next bind() End Sub
21、Protected Sub cancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cancel.Click quanxuan.Checked = False Dim i As Integer For i = 0 To GridView1.Rows.Count - 1 Dim cbox As CheckBox = GridView1.Rows(i).FindControl(CheckBox1) cbox.Checked = False Next End Sub Protected Sub GridVie
22、w1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging Response.Redirect(xiangxi.aspx?id= & GridView1.DataKeys(e.NewSelectedIndex).Value.ToString & ) End Sub Protected Sub GridView1_SelectedIndexChanged(B
23、yVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged End SubEnd Class2、duser.aspxImports System.DataImports System.Data.SqlClientPartial Class _1101_adduser Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventAr
24、gs) Handles Button1.Click Try Dim conn As New SqlConnection() Dim cmd As New SqlCommand() conn.ConnectionString = Data Source=.;Initial Catalog=aspnet;Integrated Security=SSPI;Pooling=False Dim sqlstr = insert into student(id,xuehao,name,addr,tel) values( & TextBox1.Text & , & TextBox2.Text & , & Te
25、xtBox3.Text & , & TextBox4.Text & , & TextBox5.Text & ) conn.Open() cmd.Connection = conn cmd.CommandText = sqlstr cmd.ExecuteNonQuery() conn.Close() Response.Redirect(gridviewtest.aspx) Catch ex As Exception Response.Write(用户为主键,不能重复出现) End Try End Sub Protected Sub Page_Load(ByVal sender As Object
26、, ByVal e As System.EventArgs) Handles Me.Load End SubEnd Class3、iangxi.aspxImports System.DataImports System.Data.SqlClientPartial Class _1101_xiangxi Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Try Dim conn As New SqlConn
27、ection() Dim cmd As New SqlCommand() Dim rend As SqlDataReader conn.ConnectionString = Data Source=.;Initial Catalog=aspnet;Integrated Security=SSPI;Pooling=False Dim sqlstr = select * from student where id= & Request(id) conn.Open() cmd.Connection = conn cmd.CommandText = sqlstr rend = cmd.ExecuteR
28、eader() rend.Read() Label1.Text = 学号: & rend.Item(xuehao) & Label1.Text += 姓名: & rend.Item(name) & Label1.Text += 家庭地址:& rend.Item(addr) & Label1.Text += 电话:& rend.Item(tel) & conn.Close() Catch ex As Exception Response.Write(用户信息查询败) End Try End SubEnd Class截图:五、 实验总结通过本次实验,我掌握了在SQL Server 2005中创建数据的创建、备份;.掌握了数据源控件sqldatasource的设置和使用;掌握了常用数据绑定控件gridview、formview、listview的使用;以及以上控件实现对数据库的增删改查。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1