简体   繁体   English

使用C#,如何更新另一个类的列表框项?

[英]Using C#, how can I update listbox Items from another class?

Hy, HY,

In my project asp.net (c#), I am using threading to process some messages. 在我的项目asp.net(c#)中,我使用线程来处理一些消息。 I have a display page that contain a listbox with all the actions that are taken in processing the message. 我有一个显示页面,其中包含一个列表框,其中包含处理消息时所采取的所有操作。 And a class where I am processing the message. 还有我正在处理消息的课程。

Default.aspx: Default.aspx的:

        protected void Page_Load(object sender, EventArgs e)
        {
            //starts the thread and call the method from Process.cs
        }

        public void SetListItem(string text)
        {
            myListBox.Items.Add(text);
            myListBox.DataBind();
        }

Process.cs Process.cs

public class Process
{
  public Process()
  {
    Default defaultPage;
    defaultPage.SetListItem("==> Received message!");
  }
}

But the listbox do not show nothing. 但是列表框不显示任何内容。 Does anyone has any ideea where I'm wrong? 有没有人有我错的地方? Thanks. 谢谢。

Jeff 杰夫

Remove the databind 删除数据绑定

 myListBox.DataBind();

You don't need it since you are adding an item to the list collection. 您不需要它,因为您要向列表集合中添加项目。 If you were setting the DataSource property, you would then have to use it. 如果您正在设置DataSource属性,则必须使用它。 This is all depending on where in the life-cycle your update is firing as well. 这完全取决于您的更新正在触发的生命周期中的位置。 It could be that your update is being replaced by code somewhere else in the process as well. 可能是您的更新也被进程中的其他位置的代码替换。

In truth, I would take this out of being a separate thread. 实际上,我将其排除在单独的线程之外。 Since you need it to be updated before the page submits information to the browser, you either need to keep it in the same thread to make sure it completes, or you're going to need to have some sort of a check at the end of your page executing process to make sure it has finished. 由于您需要在页面向浏览器提交信息之前更新它,您需要将其保存在同一个线程中以确保它完成,或者您需要在结束时进行某种检查。您的页面执行过程以确保它已完成。 It is possible your thread is completing after the page has finished processing. 页面处理完成后,您的线程可能正在完成。

An asp.net page, such as Default generates html which is returned to the requesting browser. 一个asp.net页面(例如Default生成html,并将其返回到发出请求的浏览器。 When this html has been generated and sent to the browser, there is no way you can add items on the server side to whatever is displayed in the browser. 生成此html并将其发送到浏览器后,您无法将服务器端的项目添加到浏览器中显示的内容。

So spawning threads that do some work is not necesarily a good idea for your current scenario. 因此,产生一些功能的产生线程对于您当前的场景来说并不是一个好主意。

Klaus is right. 克劳斯是对的。 Once the page has been drawn it's just sitting there. 绘制页面后,就坐在那里。

So, to do what you want to do, running Process in a seperate thread: Write your output message in Process to a data store, even if that's just appending to a text file. 因此,要执行您想做的事情,请在一个单独的线程中运行Process:即使您的输出消息只是追加到文本文件中,也可以将Process中的输出消息写入数据存储中。 Then, set your page to automatically refresh every X time, and refill your listbox on load with the content of that datastore. 然后,将页面设置为每X次自动刷新一次,并在加载时用该数据存储区的内容重新填充列表框。

Later on, when everything's working, you could use Ajax to make just the listbox refresh, instead of the whole page. 稍后,当一切正常时,您可以使用Ajax只进行列表框刷新,而不是整个页面。

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

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