简体   繁体   English

如何在回发ASP.NET之间的列表框中保留值

[英]How to retain values in my listbox between postbacks ASP.NET

I'm fairly new to asp.net, any help would be greatly appreciated. 我对asp.net相当陌生,对您的帮助将不胜感激。 The program is really simple, I have two web pages, one contains a listbox and the other contains a textbox. 该程序非常简单,我有两个网页,一个包含一个列表框,另一个包含一个文本框。 The program is just meant to add items to the listbox from another page. 该程序仅用于将项目从另一个页面添加到列表框中。 My code on WebForm1 , 我在WebForm1上的代码,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestAppState
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)    
            {
                string field1 = (string)(Session["id"]);
                ListBox1.Items.Add(field1);        
            }
            else
            {
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebForm2.aspx");
        }
    }
}

Code on WebForm2 WebForm2上的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Caching;

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

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Application["id"] = txtID.Text;
            Response.Redirect("WebForm1.aspx");
        }
    }
}

You're almost there. 你快到了。 In your btn_AddClick event you need to find all of the ListBox items that have been selected and save their values to the session. 在btn_AddClick事件中,您需要找到所有已选择的ListBox项并将其值保存到会话中。 You could do this in any number of formats, but probably a simple comma separated list would be fine. 您可以采用多种格式来执行此操作,但是用逗号分隔的简单列表可能会很好。

The in your Page_Load event on WebForm2, you can read that session value and either directly data bind the text box with your session value or do whatever you need to do beforehand to manipulate the data. 在WebForm2上的Page_Load事件中,您可以读取该会话值,或者直接将数据与该会话值绑定到文本框中,或者执行需要做的任何事来操纵数据。

In the code-behind for your second form, you could also snatch values directly from the first form by using the Control.FindControl method. 在第二种形式的代码后面,您还可以使用Control.FindControl方法直接从第一种形式中获取值。 Query strings could be yet another option. 查询字符串可能是另一个选择。 Not to knock other answers but sessions can be tricky some times. 不敲其他答案,但是会话有时会很棘手。 Although they are an easy solution, they can be lost depending on your application and in some cases, depending on browser security settings. 尽管它们是一个简单的解决方案,但是根据您的应用程序以及在某些情况下取决于浏览器的安全设置,它们可能会丢失。 This is mainly a concern using HTTPS though. 不过,这主要是使用HTTPS时要考虑的问题。 Another reference for this might be Cross-Page Posting in ASP.NET Web Pages . 对此的另一个参考可能是ASP.NET Web Pages中的跨页发布

There are many different ways to achieve what you need to do. 有许多不同的方法可以实现您需要执行的操作。 My approach would be to embed both pages into one form and manipulate the data with JavaScript or jQuery and use WebMethods to prevent from using postbacks. 我的方法是将两个页面都嵌入一种形式,并使用JavaScript或jQuery处理数据,并使用WebMethods防止使用回发。 It would be a much smoother presentation to the user as well. 对用户而言,这也将是一个更加流畅的呈现。

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

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