简体   繁体   English

asp.net文本框未更新背后的表单代码

[英]asp.net textbox not updating form code behind

So I do want to update my TextBox called "ServerDataArea" in a thread running in code behind. 因此,我确实想在后面的代码中运行的线程中更新名为“ ServerDataArea”的文本框。

form: 形成:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:TextBox id="ServerDataArea" TextMode="multiline" Columns="50" Rows="15" runat="server" Enabled="false" />
        <br />
        <asp:TextBox ID="Message" runat="server" ></asp:TextBox>
        <asp:Button ID="SendMessage" runat="server" OnClick="appendMessage_Click" Text="Send" />
    </ContentTemplate>
</asp:UpdatePanel>

This is how my code behind does to update this textbox: 这是我后面的代码如何更新此文本框的方式:

public delegate void MyDelegate(string text);
private MyDelegate d;

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        d = new MyDelegate(MyMethod);
    }

    // create connection
    // ...

    // create thread
    if(Session["thread"] == null)
    {
        Session["thread"] = new Thread(new ThreadStart(RunReceiver));
    }

    Thread receiverThread = (Thread)Session["thread"];

    if (!receiverThread.IsAlive)
    {
        receiverThread.Start();
        Debug.WriteLine("Receiver thread started!");
    }
}

/// <summary>
/// receive messages from the server
/// </summary>
private void RunReceiver()
{
    byte[] data = null;

    while (con.GetClient().Connected)
    {
        // if stream contains data to recieve
        if (con.GetStream().DataAvailable)
        {
            Debug.WriteLine("client receiving some data");

            // create new buffer to store received data
            data = new Byte[256];

            // read data from stream
            Int32 bytes = con.GetStream().Read(data, 0, data.Length);

            // convert to readable text
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

            // display message in chat
            d(responseData);
        }

        Thread.Sleep(400);
    }
}

private void MyMethod(string text)
{
    ServerDataArea.Text += text + "\r\n";
}

the Debug.WriteLine(ServerDataArea.Text) does show me what should be visible in the textbox but it is not visible and I just can not figure out how to make it visible.. Debug.WriteLine(ServerDataArea.Text)确实向我显示了在文本框中应该可见的内容,但是它不可见,我只是想不出如何使其可见。

EDIT : (Removed bunk) Your code is running on a seperate thread from the controls UI thread, and the UI will only allow updates via the controls UI thread. 编辑 :(已删除的程序)您的代码在与控件UI线程不同的线程上运行,并且UI仅允许通过控件UI线程进行更新。

Try updating the textbox value outside of the new thread you've created. 尝试在创建的新线程之外更新文本框值。

This is a difficult problem to solve so I suggest you achieve what you want a different way. 这是一个很难解决的问题,所以我建议您以其他方式实现想要的目标。

Taken from here "Technically, you shouldn't be accessing a control from any other thread except the one it was created on. 摘自此处:技术上讲,您不应从任何其他线程访问控件,除非该控件是在其上创建的。

Accessing controls from a secondary thread is not guaranteed. 不保证从辅助线程访问控件。 It might work sometimes but you shouldn't rely on it. 有时它可能会起作用,但是您不应该依赖它。

Instead, you should define a delegate and use the delegate to marshal the calls from the secondary thread to the main thread when accessing your controls. 相反,您应该定义一个委托,并在访问控件时使用该委托来封送从辅助线程到主线程的调用。 I'm not sure if this would speed up the update but here's how you would update a textbox's text from another thread (VB alert): 我不确定这是否可以加快更新速度,但是这是您如何从另一个线程更新文本框的文本(VB警报):

EDIT 2 : Converted vb function to C# 编辑2 :将vb函数转换为C#

public delegate void SetTextDelegate(string sText);

private SetTextDelegate SetText = new SetTextDelegate(SetTextBoxText);

private void SetTextBoxText(string sText)
{
    tbStatus.Text = sText;
}

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

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