asp.net 单击登录,通过 Response 对象将信息传递给 Info.aspx 页面

问题:

设计一个包含两个页面的 ASP.NET 网站。在 Default.aspx 页面中设计一个登录页面,单击登录按钮时,可以通过 Response 对象将信息传递给 Info.aspx 页面。如果用户名为“zhangsan”,密码为“123456”,则显示“欢迎访问本网站”,不显示 “返回”按钮;否则显示“用户名或密码错误!”,单击“返回”按钮后显示 Default.aspx 页面。

代码:

  • Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = TextBox1.Text;
        string password = TextBox2.Text;
        Response.Redirect("Info.aspx?name1=" + name + "&password1=" + password);
    }
}
  • Info.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Info : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var name11 = Request.QueryString["name1"];
        var password11 = Request.QueryString["password1"];
        if (name11 == "zhangsan" && password11 == "123456")
        {
            Response.Write("欢迎" + name11.ToString() + "访问本网站!");
        }
        else
        {
            Response.Write("用户名或密码错误!<br/><a href='Default.aspx'><font color=blue>返回</font></a>");
        }
    }
}

运行结果:

首页

用户名或密码错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的ASP.NET Web Forms学生信息管理系统示例,无需登录: 1. 首先,创建一个ASP.NET Web Forms项目。 2. 在Web应用程序中创建一个名为“Student”(或类似名称)的文件夹。在此文件夹中,创建一个名为“AddStudent.aspx”的新Web表单。 3. 将以下代码添加到AddStudent.aspx文件中: ```html <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddStudent.aspx.cs" Inherits="WebApplication1.Student.AddStudent" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Add Student</title> </head> <body> <form id="form1" runat="server"> <div> <label>First Name:</label> <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> <br /> <label>Last Name:</label> <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> <br /> <label>Age:</label> <asp:TextBox ID="txtAge" runat="server"></asp:TextBox> <br /> <label>Address:</label> <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> <<br /> <asp:Button ID="btnAdd" runat="server" Text="Add Student" OnClick="btnAdd_Click" /> </div> </form> </body> </html> ``` 4. 在“Student”文件夹中创建一个名为“Student.cs”的新类文件,并将以下代码添加到该文件中: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication1.Student { public class Student { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public string Address { get; set; } } } ``` 5. 在“Student”文件夹中创建一个名为“StudentList.aspx”的新Web表单,并将以下代码添加到该文件中: ```html <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StudentList.aspx.cs" Inherits="WebApplication1.Student.StudentList" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Student List</title> </head> <body> <form id="form1" runat="server"> <div> <h2>Student List</h2> <asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="FirstName" HeaderText="First Name" /> <asp:BoundField DataField="LastName" HeaderText="Last Name" /> <asp:BoundField DataField="Age" HeaderText="Age" /> <asp:BoundField DataField="Address" HeaderText="Address" /> </Columns> </asp:GridView> </div> </form> </body> </html> ``` 6. 在“Student”文件夹中创建一个名为“StudentList.aspx.cs”的新类文件,并将以下代码添加到该文件中: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1.Student { public partial class StudentList : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } private void BindGrid() { List<Student> students = (List<Student>)Session["Students"]; if (students != null && students.Count > 0) { gvStudents.DataSource = students; gvStudents.DataBind(); } } } } ``` 7. 添加一个名为“AddStudent.aspx.cs”的新类文件,并将以下代码添加到该文件中: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1.Student { public partial class AddStudent : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnAdd_Click(object sender, EventArgs e) { List<Student> students = (List<Student>)Session["Students"]; if (students == null) { students = new List<Student>(); } Student student = new Student() { FirstName = txtFirstName.Text.Trim(), LastName = txtLastName.Text.Trim(), Age = int.Parse(txtAge.Text.Trim()), Address = txtAddress.Text.Trim() }; students.Add(student); Session["Students"] = students; Response.Redirect("~/Student/StudentList.aspx"); } } } ``` 8. 运行应用程序,并在浏览器中导航到“AddStudent.aspx”页面。在此页面上,输入学生的详细信息,然后单击“Add Student”按钮。学生信息将被添加到应用程序中,并在“StudentList.aspx”页面上显示。 这是一个非常基本的示例,但可以作为起点来开发更复杂的学生信息管理系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值