asp.net最新

更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
使用Session对象在两个页面之间传送密码的值。 (一) 页面设计:设计两个Web页面,SessionDemo.aspx和Welcome.aspx,如图1和图2所示。在SessionDemo.aspx页面设计视图中拖放控件,完成页面设计,如图1所示。 <form id="form1" runat="server"> <div> 用户名:<asp:TextBox ID="txtUser" runat="server"></asp:TextBox> <br /> 密码:<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox> <br /> <br /> <asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" /> <asp:Button ID="btnReset" runat="server" Text="重置" /> </div> </form> (二) 实现页面的功能:在SessionDemo.aspx页面中,单击"登录"按钮后,程序首先判断用户名和密码是否为空,只要有一个为空,就会弹出一个提示对话框,提示用户"用户名或密码不能为空!",如果都不为空,则把用户名和密码框里的值分别存到两个Session变量里,然后跳转到欢迎页面Welcome.aspx;在Welcome.aspx中获取并显示前一个页面用Session变量保存的用户名和密码。 两个Web页面的设计已完成,请编写程序代码,完善页面的程序功能。 (1) 编写SessionDemo.aspx页面的btnLogin_Click事件过程代码,请完善该事件代码。 protected void btnLogin_Click(object sender, EventArgs e) { if (txtUser.Text != "" && txtPwd.Text != "") { Session["username"] = txtUser.Text; Session["password"] = txtPwd.Text; Response.Redirect("Welcome.aspx"); } else Response.Write("<script language=javascript>alert('用户名或密码不能为空!');</script>"); } (2) 编写Welcome.aspx页面的Page_Load事件过程代码,请完善该事件代码。 protected void Page_Load(object sender, EventArgs e) { if (Session["username"] != null && Session["password"] != null) { string name = Session["username"].ToString(); string pwd = Session["password"].ToString(); Response.Write("欢迎" + name + "光临本站,请记住你的密码:" + pwd); } } (含图)(含图)
使用SqlDataReader对象读取数据库中的数据。 在网站项目中添加一个名为DataReaderDemo.aspx的Web页面,在DataReaderDemo.aspx.cs文件中定义数据库连接对象,编写页面的Page_Load事件,实现查询Student数据库中的StuInfo数据表,并显示表中所有信息,如图1所示。 static string ConStr = "Server=.; Database=Student; Uid=sa; pwd=123456"; //创建数据连接对象 SqlConnection conn = new SqlConnection(ConStr); protected void Page_Load(object sender, EventArgs e) { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from StuInfo"; //声明一个名为 dr 的 SqlDataReader 类型变量,并将其初始化为 null SqlDataReader dr = null; try { if (conn.State == ConnectionState.Closed) conn.Open(); //执行SQL命令,并获取查询结果 dr = cmd.ExecuteReader(); //依次读取查询结果的字段名称,并以表格的形式显示 Response.Write("<table border='1'><tr align='center'>"); for (int i = 0; i < dr.FieldCount; i++) { Response.Write("<td>" + dr.GetName(i) + "</td>"); } Response.Write("</tr>"); //如果DataRead对象成功获得数据,返回true,否则返回false while (dr.Read()) { //依次读取查询结果的字段值,并以表格的形式显示 Response.Write("<tr>"); for (int j = 0; j < dr.FieldCount; j++) { Response.Write("<td>" + dr.GetValue(j) + "</td>"); } Response.Write("</tr>"); } Response.Write("</table>"); } catch (Exception ex) { Response.Write("SqlDataReader读取出错,原因:" + ex.Message); } finally { if (dr.IsClosed == false) //关闭DataReader对象 dr.Close(); if (conn.State == ConnectionState.Open) //关闭Connection对象 conn.Close(); } } (含图)