如何将数据从Default.aspx发布到另一个aspx页面

时间:2014-12-28 19:59:52

标签: c# asp.net

我只是为了学习目的而建立一个练习网站。下面是我使用twiter bootstrap的Default.aspx页面。注意控件这里是HTML而不是asp.net控件。

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>My Practice Website</title>

    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="resources/css/bootstrap.min.css" />
    <script src="resources/js/jquery-2.1.3.min.js"></script>
    <script src="resources/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-6">
                <form method="post" runat="server" >
                    <div class="row">
                        <div class="col-lg-6">
                            <div class="form-group">
                                <label for="txtLName">First Name</label>
                                <input type="text" runat="server" class="form-control" id="txtFName" name="txtFName" placeholder="Enter First Name" />
                            </div>

                            <div class="form-group">
                                <label for="txtUName">Username</label>
                                <div class="input-group">
                                    <span class="input-group-addon">@</span>
                                    <input type="text" runat="server" class="form-control" id="txtUName" name="txtUName" placeholder="Enter User Name" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="txtPassword">Password</label>
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                    <input type="password" runat="server" class="form-control" id="txtPassword" name="txtPassword" placeholder="Enter Password" />
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-6">
                            <div class="form-group">
                                <label for="txtLName">Last Name</label>
                                <input type="text" runat="server" class="form-control" id="txtLName" name="txtLName" placeholder="Enter Last Name" />
                            </div>
                            <div class="form-group">
                                <label for="txtEmail">Email</label>
                                <input type="email" runat="server" class="form-control" id="txtEmail" name="txtEmail" placeholder="Enter Email" />
                            </div>

                            <div class="form-group">
                                <label for="txtConfirmPassword">Confirm Password</label>
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                    <input type="password" runat="server" class="form-control" id="txtConfirmPassword" name="txtConfirmPassword" placeholder="Enter Password" />
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-12">
                            <button type="submit" runat="server" name="btnSubmit" class="btn btn-info pull-right" onserverclick="btnSubmit_click" >
                                <span>Submit</span>
                                <i class="glyphicon glyphicon-log-in"></i>
                            </button>

                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>

我希望将此表单数据发布到另一个页面,比如说User.aspx。如何从Code背后做到这一点?有人请。

2 个答案:

答案 0 :(得分:4)

是的,您可以将其发布到第二页,尝试将表单元素的标记更改为此标记,

<form method="post" action="~/User.aspx">

请注意,普通HTML元素不需要runat="server"属性。之后,您可以通过获取此类对象来收集用户在User.aspx页面上传递的数据,

if(IsPost) {
    // Post Request
    var txtFName = Request["txtFName"];
    // and so on...
}

..这足以将数据从一个页面收集到另一个页面。

答案 1 :(得分:2)

由于您的HTML控件将runat属性设置为server,因此您可以在后面的代码中获取它们的值。你可能想要这样的东西:

public class MyData
{
    public string FirstName { get; set; }
    public string UserName { get; set; } 
    // And so on and so forth for the other properties to save.
}

protected void btnSubmit_click(object sender, EventArgs e)
{  
    MyData md = new MyData();
    md.FirstName = txtFName.Value;
    md.UserName = txtUName.Value;
    // And so on and so forth for the other controls.

    Session["MyData"] = md;  
}

在另一页:

MyData md = (MyData)Session["MyData"];