简体   繁体   English

在页面之间传递对象

[英]Passing object between pages

I have encountered a little problem and need some help 我遇到了一个小问题,需要一些帮助

What I would like to do is pass an object from one page and pass it to another page. 我想做的是从一个页面传递一个对象并将其传递到另一页面。

This is no problem with C# as the code would look like. C#就像代码一样,这没有问题。

namespace WindowsFormsApplication1
{
public class Class1
{
    private String firstName;
    private String surName;
    private int age;

    public Class1()
    {

    }

    public String FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public String SurName
    {
        get { return surName; }
        set { surName = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }
}

And The forms look like 和形式看起来像

private void button1_Click(object sender, EventArgs e)
    {
        Class1 class1 = new Class1();
        class1.FirstName = textBoxFirstname.Text;
        class1.SurName = textBoxSurname.Text;
        class1.Age =  int.Parse(textBoxAge.Text);

        Form2 form2 = new Form2(class1);
        form2.Show();
    }

And

public partial class Form2 : Form
{
    private Class1 class1;

    public Form2(Class1 class1)
    {
        InitializeComponent();
        this.class1 = class1;

        label2.Text = (class1.FirstName + " " + class1.SurName);
        label4.Text = Convert.ToString(class1.Age);
    }
}

This is what I have come up with so far 到目前为止,这是我想出的

And These are the pages 这些是页面

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

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Class1 class1 = new Class1();
        class1.FirstName = TextBoxFirstName.Text;
        class1.SurName = TextBoxSurname.Text;
        class1.Age = int.Parse(TextBoxAge.Text);

        //Missing code
    }
    }

And this is the 2nd form 这是第二种形式

<body>
<form id="form1" runat="server">
<div>

    <asp:Label ID="Label1" runat="server" Text="Details"></asp:Label><br />
    <asp:Label ID="Label2" runat="server" Text="Name"></asp:Label>
    <asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>

    <br />

    <asp:Label ID="Label4" runat="server" Text="Name"></asp:Label>
    <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
    <br />

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

and here is the back code 这是后台代码

public partial class WebForm2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //missing code to display label 3

        //Missing code to display label 5
    }
}

Use Sessions : 使用会话

Save your informations you need (eg a instance of your Class1) in a session and reuse it in another page: 在会话中保存您需要的信息(例如Class1的实例),然后在另一个页面中重复使用它:

On Page1 in Button_Click : Button_Click第1页上:

Session["YourSessionName"] = class1Instance;

On Page2 in Page_Load : Page2上在Page_Load

if (Session["YourSessionName"] != null) //Check if session is empty
{
    Class1 class1InstanceOnPage2 = (Class1)Session["YourSessionName"];
}

And then you can reuse all your stuff from the class. 然后,您可以重用该类中的所有内容。 This also works with strings etc. 这也适用于字符串等。

But this only applies to ASP.NET and not to WinForms. 但这仅适用于ASP.NET,不适用于WinForms。

I don't understand why you mix it up, its not possible. 我不明白您为什么要混合使用,不可能。

In ASP.Net, you do not pass objects directly into other pages. 在ASP.Net中,您不会将对象直接传递到其他页面。 In a sense, the pages are disconnected from each but joined only by user input. 从某种意义上说,页面是彼此断开的,但是只能通过用户输入来连接。

To pass data, rather than objects (which is the correct way to think of it), you can pass it via the querystring, if it is not private and you are not worried about tampering, via the Session object, if it needs to be private or via a database where it is stored and then retrieved in another page. 要传递数据,而不是对象(这是正确的思考方式),可以通过查询字符串传递它(如果它不是私有的,并且您不必担心通过Session对象进行篡改)(如果需要的话)私有或通过存储它的数据库,然后在另一个页面中检索它。

Web is different than a native app! 网络与本地应用不同!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM