从asp.net中的文本框中读取数据并将其显示在另一个网页中

时间:2016-02-24 07:12:00

标签: html asp.net forms

我是ASP.net的新手。我正在尝试使用基本的Web表单。 我创建了代码来从用户获取名字和姓氏。当用户点击提交时,我需要将数据显示在另一个页面上。有人可以为我编写应该在输出网页上编写的代码吗?

我的代码是:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My First web page</title>
</head>
<body>
    <form id="form1" runat="server" action="WebFormOutput.aspx" >
    <div style="position:absolute">
    <input type="text" name="txtFirstName" placeholder="Enter Your First Name" />
    <input type="text" name="txtLastName" placeholder="Enter Your Last Name"/>
        <input id="Submit1" type="submit" value="submit" />
    </div>
    </form>
</body>
</html>

谢谢

1 个答案:

答案 0 :(得分:3)

考虑以下示例...

1.创建一个名为default.aspx的网页(如下所示),

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="HP_Master_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label>
        <asp:TextBox ID="txtFN" runat="server"></asp:TextBox>
         <asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label>
                 <asp:TextBox ID="txtLN" runat="server"></asp:TextBox>

        <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

并在Default.aspx.cs中,如下所示

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["FN"] = txtFN.Text;
        Session["LN"] = txtLN.Text;
        Response.Redirect("Default2.aspx");
    }
}

我所做的是    1.处理页面,并从工具栏,Label1,Label2,txtFN,txtLN和Button

添加服务器控件

2.添加了onclick事件到按钮,并在后面的代码中创建了两个会话来携带在两个文本框Session [“FN”]和Session [“LN”]中输入的值,然后页面被重定向到按钮上的另一页“Default2.aspx”单击

3.默认两个服务器控件LblFN和最初不包含任何值的lllLN

4.单击上一页中的按钮后立即加载页面时,在当前页面页面加载事件中,标签lblFN和lblLN的text属性设置为存储在Session变量中的值。代码如下:

default2.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="HP_Master_Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblFN" runat="server" Text=""></asp:Label>
        <asp:Label ID="lblLN" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

default2.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class HP_Master_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblFN.Text = Session["FN"].ToString();
        lblLN.Text = Session["LN"].ToString();
    }
}