简体   繁体   中英

ASP.NET TextBox won't add text from C#

I am trying to do a multi Client - Server application. After I did some of the basic controls and functions with Windows Forms I thought to add a WebPage to my Client side.

I created a new project and edited it and it works ok, the page is connecting to the server and it receives the messages I send from the web page.

The problem I get when I need to post messages on a textbox on the web page. I searched on some pages here, on the internet and I can't find a good solution for my problem. I alose used the Page.IsPostBack but it didn't work, then I added an UpdatePanel because the page was refreshing when I clicked the buttons but that didn't work either... Now I am out of ideas.

Can anybody suggest how should I do this ? My code behind is C# and I don't know how to parse these details to JavaScript or jQuery, so any of you have some details on how to that it will also be appreciated.

Thanks in advance.

And also I will post anything needed if there is important for this question.

EDIT (Added code):

public void btnSend_Click(object sender, EventArgs e)
        {
            if (tbSendMessage.Text.Length > 0)
            {
                string message = tbSendMessage.Text;
                byte[] outStream = Encoding.ASCII.GetBytes(message + "$");
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
                tbSendMessage.Text = string.Empty;
            }
        }

 private void getMessage()
        {
            while (true)
            {
                try
                {
                    serverStream = clientSocket.GetStream();
                    int buffSize = 0;
                    byte[] inStream = new byte[70000];
                    buffSize = clientSocket.ReceiveBufferSize;
                    serverStream.Read(inStream, 0, buffSize);
                    string returndata = Encoding.ASCII.GetString(inStream);
                    //tbReceivedMessages.Text += returndata + "\n";
                    ShowMessage(returndata);
                }
                catch (Exception ex)
                {
                    ShowAlert("Connection lost.\n" + ex.Message);
                    //ShowMessage("Conexiunea cu serverul s-a pierdut.\n" );
                    serverStream.Close();
                    return;
                }
            }
        }

private void ShowMessage(string message)
        {
            sb.AppendLine(message);
            tbReceivedMessages.Text += sb;          
        }

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %></h2>
    <p>
        <asp:UpdatePanel ID="UpdatePanelConnect" runat="server">
            <ContentTemplate>
                <asp:Label ID="lblUsername" runat="server" Text="Enter username:"></asp:Label>
                <asp:TextBox ID="tbUsername" runat="server"></asp:TextBox>
                <asp:Button ID="btnConnect" runat="server" OnClick="btnConnect_Click" Text="Connect" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </p>
    <p>&nbsp;</p>
    <p>
        <asp:UpdatePanel ID="UpdatePanelConnected" runat="server">
            <ContentTemplate>
                <asp:TextBox 
                    ID="tbReceivedMessages" 
                    runat="server" 
                    Height="250px" 
                    TextMode="MultiLine"
                     Width="250px" 
                    MaxLength="2000000" 
                    ReadOnly="True"></asp:TextBox>
    </p>
    <p>&nbsp;</p>
    <p>
                <asp:TextBox ID="tbSendMessage" runat="server"></asp:TextBox>
                <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" Text="Send" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </p>

</asp:Content>

If you want two server controls to communicate each other via Ajax, you will need to place them in same UpdatePanel .

FYI: If you are new to ASP.Net Web Form, do not use UpdatePanel yet. Instead, make it work with regular post back.

<asp:UpdatePanel ID="UpdatePanelConnect" runat="server">
    <ContentTemplate>
        <asp:Label ID="lblUsername" runat="server" Text="Enter username:"></asp:Label>
        <asp:TextBox ID="tbUsername" runat="server"></asp:TextBox>
        <asp:Button ID="btnConnect" runat="server" OnClick="btnConnect_Click" Text="Connect" />
        <asp:TextBox
            ID="tbReceivedMessages"
            runat="server"
            Height="250px"
            TextMode="MultiLine"
            Width="250px"
            MaxLength="2000000"
            ReadOnly="True"></asp:TextBox>
        </p>
        <p>&nbsp;</p>
        <p>
            <asp:TextBox ID="tbSendMessage" runat="server"></asp:TextBox>
            <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" Text="Send" />
    </ContentTemplate>
</asp:UpdatePanel>

public void btnSend_Click(object sender, EventArgs e)
{
    if (tbSendMessage.Text.Length > 0)
    {
        string message = tbSendMessage.Text;
        // This code won't work.
        /*byte[] outStream = Encoding.ASCII.GetBytes(message + "$");
        serverStream.Write(outStream, 0, outStream.Length);
        serverStream.Flush();
        tbSendMessage.Text = string.Empty;*/
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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