开发者

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 开发者_C百科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!


You will need to use AJAX to do this. I recommend looking at some of the AJAX capabilities that jQuery offers but you can also look at the AJAX Toolkit from Microsoft.

Here is documentation for AJAX in jQuery:

  • http://api.jquery.com/jQuery.ajax/

I feel this is much "lighter" than what Microsoft offers out of the box. You can find out more about the Microsoft AJAX toolkit here:

  • http://www.asp.net/ajax/ajaxcontroltoolkit/samples/


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.


Check out AJAX. This will require client scripting to submit and detect updates without submitting or updating the entire page.

Note, however, that this is a fairly advanced topic and will not simply be a little snippet of code you can add. I would recommend a good AJAX/JavaScript/jQuery book.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜