提交文本框的值而不重新加载页面
我正在开发一个基于 ASP.NET 的 TicTacToe 游戏。我遇到的问题是: 该游戏在两个用户之间进行。当第一个玩家在文本框中输入“x”时,我希望“x”显示在第二个玩家的计算机上,而无需重新加载页面。 我不知道某些代码是否有帮助,但这是我在不重新加载的情况下完成的方法(用户必须手动重新加载页面...愚蠢):
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();
}
然后,在页面预渲染上:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Application.Lock();
if(Application["TopLeft"] != "0")
{
this.TopLeft.Text = Application["TopLeft"].ToString();
}
...
等等... 我将非常感谢任何可以提供帮助的人!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用 AJAX 来执行此操作。我建议查看 jQuery 提供的一些 AJAX 功能,但您也可以查看 Microsoft 的 AJAX 工具包。
以下是 jQuery 中 AJAX 的文档:
我觉得这比微软提供的开箱即用的“轻”得多。您可以在此处找到有关 Microsoft AJAX 工具包的更多信息:
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:
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:
您询问的是部分页面更新。
首先,您需要将客户端
TextBox
或需要重新加载的其他控件放入 更新面板。然后,您需要调用 UpdatePanel.Update< /a> 在您需要时更新这些控件。
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.
查看 AJAX。这将需要客户端脚本来提交和检测更新,而无需提交或更新整个页面。
但请注意,这是一个相当高级的主题,不仅仅是您可以添加的一小段代码。我会推荐一本好的 AJAX/JavaScript/jQuery 书籍。
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.