实验07数据绑定控件.docx
《实验07数据绑定控件.docx》由会员分享,可在线阅读,更多相关《实验07数据绑定控件.docx(14页珍藏版)》请在冰豆网上搜索。
实验07数据绑定控件
实验七数据绑定控件
学号:
20131020260 姓名:
廖宇 专业:
信管
实验时间:
2016年5月18日 实验地点:
宿舍
一、实验目的
掌握GridView控件的使用。
掌握DetailView控件的使用。
熟练运用SQLDataSource控件。
二、实验内容和要求
(1)使用GridView控件绑定留言板信息。
A、打开visualstudio2010,按实验一的方法创建一个ASP.NETWeb的空网站,命名为“课堂训练7-1”
B、右击网站名称,选择“添加新项”命令。
在“添加新项”对话框中选择web网页,单击添加按钮,默认名称为Default.aspx。
C、设计web窗体,切换到设计视图,向页面中添加1个gridview控件,并设置相关属性,根据实验手册58页编辑gridview控件的列字段,更改gridview的样式为“大洋洲”。
D、编辑窗体加载事件代码
protectedvoidPage_Load(objectsender,EventArgse)
{
stringsqlconn="DataSource=.;Database=GuestBook;IntegratedSecurity=True";
SqlConnectionmyConnection=newSqlConnection(sqlconn);
myConnection.Open();
SqlCommandmyCommand=newSqlCommand("select*fromGuestBookInfo",myConnection);
SqlDataAdapterAdapter=newSqlDataAdapter();
Adapter.SelectCommand=myCommand;
DataSetmyDs=newDataSet();
Adapter.Fill(myDs);
myConnection.Close();
GridView1.DataSource=myDs.Tables[0].DefaultView;
DataBind();
}
E、编辑当前页索引正在更新时的事件代码如下:
protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse)
{
GridView1.PageIndex=e.NewPageIndex;
DataBind();
}
}
F、浏览建立的web窗体进行测试。
(2)实现GridView控件编辑留言板信息的功能。
A、打开visualstudio2010,按实验一的方法创建一个ASP.NETWeb的空网站,命名为“课堂训练7-2”
B、右击网站名称,选择“添加新项”命令。
在“添加新项”对话框中选择web网页,单击添加按钮,默认名称为Default.aspx。
C、设计web窗体,切换到设计视图,向页面中添加1个gridview控件,并设置相关属性。
D、编辑相关代码如下:
publicpartialclass_Default:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!
IsPostBack)
{
Bind();
}
}
protectedvoidGridView1_SelectedIndexChanged(objectsender,EventArgse)
{
}
protectedvoidGridView1_RowEditing(objectsender,GridViewEventArgse)
{
GridView1.EditIndex=e.NewEditIndex;
DataBind();
}
protectedvoidGridView1_RowCancelingEdit(objectsender,GridViewCancelEditEventArgse)
{
GridView1.EditIndex=-1;
GridView1.DataBind();
}
protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse)
{
stringsqlconn="DataSource=.;Database=GuestBook;IntegratedSecurity=True";
stringname=((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
stringdetail=((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
stringtime=((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
stringback=((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
intid=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
stringstr="updateGuestBookInfosetName='"+name+"',Detail='"+detail+"',Time='"+time+"',Back='"+back+"'whereID='"+id+"'";
SqlConnectionmyConnection=newSqlConnection(sqlconn);
myConnection.Open();
SqlCommandmyCommand=newSqlCommand(str,myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
GridView1.EditIndex=-1;
Bind();
}
privatevoidBind()
{
thrownewNotImplementedException();
stringsqlconn="DataSource=.;Database=GuestBook;IntegratedSecurity=True";
SqlConnectionmyConnection=newSqlConnection(sqlconn);
myConnection.Open();
SqlCommandmyCommand=newSqlCommand("select*fromGuestBookInfo",myConnection);
SqlDataAdapterAdapter=newSqlDataAdapter();
Adapter.SelectCommand=myCommand;
DataSetmyDs=newDataSet();
Adapter.Fill(myDs);
myConnection.Close();
GridView1.DataSource=myDs.Tables[0].DefaultView;
DataBind();
myConnection.Close();
}
}
F、建立web窗体进行测试。
(3)实现GridView控件删除留言板信息的功能。
A、打开visualstudio2010,按实验一的方法创建一个ASP.NETWeb的空网站,命名为“课堂训练7-3”
B、右击网站名称,选择“添加新项”命令。
在“添加新项”对话框中选择web网页,单击添加按钮,默认名称为Default.aspx。
C、设计web窗体,切换到设计视图,向页面中添加1个gridview控件,并设置相关属性。
D、编写加载页执行的事件代码,以及当gridview控件内生成delete事件时触发的代码;
publicpartialclass_Default:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!
IsPostBack)
{
Bind();
}
}
protectedvoidGridView1_RowDeleting(objectsender,GridViewDeleteEventArgse)
{
stringsqlconn="DataSource=.;Database=GuestBook;IntegratedSecurity=True";
intid=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
stringstr="deletefromGuestBookInfowhereID='"+id+"'";
SqlConnectionmyConnection=newSqlConnection(sqlconn);
myConnection.Open();
SqlCommandmyCommand=newSqlCommand(str,myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
Bind();
}
privatevoidBind()
{
stringsqlconn="DataSource=.;Database=GuestBook;IntegratedSecurity=True";
SqlConnectionmyConnection=newSqlConnection(sqlconn);
myConnection.Open();
SqlCommandmyCommand=newSqlCommand("select*fromGuestBookInfo",myConnection);
SqlDataAdapterAdapter=newSqlDataAdapter();
Adapter.SelectCommand=myCommand;
DataSetmyDs=newDataSet();
Adapter.Fill(myDs);
GridView1.DataSource=myDs.Tables[0].DefaultView;
GridView1.DataBind();
myConnection.Close();
}
F、建立浏览web窗体,测试。
(4)使用GridView控件、DetailView控件、SQLDataSource控件一起实现添加留言信息的功能。
A、打开visualstudio2010,按实验一的方法创建一个ASP.NETWeb的空网站,命名为“课堂训练7-4”
B、右击网站名称,选择“添加新项”命令。
在“添加新项”对话框中选择web网页,单击添加按钮,默认名称为Default.aspx。
C、设计web窗体,切换到设计视图,向页面中添加1个gridview控件,1个detailsview控件和sqldatasource控件,并设置相关属性如实验手册63页。
D、编写在gridview中选择行时,在该选择操作完成后触发的事件代码如下:
protectedvoidGridView1_SelectedIndexChanged(objectsender,EventArgse)
{
this.DetailsView1.PageIndex=this.GridView1.SelectedRow.DataItemIndex;
}
E、浏览建立的窗体进行测试。
操作过程中注意要截图。
记录操作步骤并上交实验报告。
三、实验步骤:
实验步骤请自己记录,要求有截图。
小结:
四、主要实验仪器及材料
安装有Windows2000/XP/7系统的计算机,安装有VisualStudio2010等。
五、实验出现的问题及解决方法
六、实验总结
1、记录做实验过程中的体会
2、提出对该实验的意见和改进建议