Search code examples
c#asp.netvisual-studio-2010reloadtic-tac-toe

Submit value of a TextBox without reloading page


I'm working on an ASP.NET based TicTacToe game. The problem I have with it is that: The game is played between two users. When the first one types 'x' in the TextBox I want the 'x' to be shown on the second player's computer without reloading the page. I don't know if some code will help but here is the way I did it without reloading(the user must reload the page manually... dumb):

protected void TopLeft_TextChanged(object sender, EventArgs e)
    {
        Application.Lock();
        GameBoard gameBoard = new GameBoard();
        gameBoard.board[0, 0] = char.Parse(this.TopLeft.Text);
        Application["TopLeft"] = gameBoard.board[0, 0];
        Application.UnLock();
    }

And then, on page pre render:

protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        Application.Lock();
        if(Application["TopLeft"] != "0")
        {
            this.TopLeft.Text = Application["TopLeft"].ToString();
        }
        ...

And so on... I'd be very thankfull to anyone who can help!


Solution

  • You are asking about Partial Page Update.

    First, you need to place the client TextBox or what ever other controls that you need to reload inside an UpdatePanel.

    Then, you need to call the UpdatePanel.Update to update those controls whenever you need.