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!
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.
Related
I a have a web form with few textboxes,dropdowns and finally towards the end of the page is 4 custom ajax editors. So on page load the focus is always inside the last editor and no way it comes to the first text box or top of page.On each page load the cursor goes inside the last editor control.How to bring the focus inside the first text box?
Below are the few methods i tried
1.<body onload="document.body.scrollTop = 0;">
2. void Page_Init(object sender, EventArgs e)
{
SetFocus(txtReqtitle);
}
for the above while loading the page i could see the focus goes to the desired text box and then it comes to the last custom control.
3. if(!Page.ClientScript.IsStartupScriptRegistered("scrFocus"))
{
string strScript="var txtBox=document.getElementById('" + txtReqtitle.ClientID.ToString() +"');txtBox.focus();";
ClientScript.RegisterStartupScript(this.GetType(),"scrFocus", strScript,true);
}
4.
function setFocus() {
document.getElementById("txtReqtitle").focus();
}
5. ScriptManager.GetCurrent(this.Page).SetFocus(txtReqtitle);
Any ideas? Thanks..
Seems focus property is not working in load event because of ajax editors
Please add txtReqtitle.Focus() in Page Prerendercomplete() event and let me know whether it works
found the solution which worked for me .would like to share it .. its simple ..just make the autofocus=false for the control.
using jquery
$(document).ready(function(){
$('#<%= txtReqtitle.ClientID %>').focus();
});
I was looking for an alternative that may be used to activate a multiview control of other page without passing a querystring/session variable.
Basically, my Home.aspx page has a link that takes us to a specific page say "NewPage.aspx". The NewPage.aspx page has a multiview control that has three child views.
I want to click on the link of the Home.aspx and go to NewPage.aspx with MultiView1.ActiveViewIndex=1. Please remember that I do not want to pass any querystring variable as that link already contains some encrypted data as querystring and adding another variable can cause the data to corrupt. Maintaining a session isn't a solution as well because the application is quite big.
Any inbuilt method that can activate that view? (I don't seem to be talking practical but any help is really appreciated)
If you are asking about how navigate to this page I would wire up a button event (I prefer to do so in OnInit). Something like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.btnlinkclick.Click += new EventHandler(btnlinkclick_Click);
}
void btnlinkclick_Click(object sender, EventArgs e)
{
this.MultiView1.ActiveViewIndex = 1;
}
This should work for you.
i am trying to build a timer in my asp.net website.
so, when you click on a button, lets say hes id = "Button1." it will make the button disable(with enable = false command) and after few\4secconds it will enable=true again.
i have tired to look around how to to so, but all guilds online are very old.. and nothing is realyl helpfull.
This is the full code name(just for the exple):
protected void Button1_Click(object sender, EventArgs e)
{
}
Thanks alot! please help me :).
Have a look at the following answer it should be what you are after:
Disable an asp.net dynamic button click event during postback and enable it afterwards
you cant do that in the server side. youll have to do it in client with setTimeout Js function with 4000 ms.
you have to understand that when you see the page - ther server doesnt know the page anymore. and he cant post back by himself.
Morning all.
How are we? Excellent I hope.
I have the following little bits of code that I am using to check/uncheck a couple of radio buttons on my page:
<script type="text/javascript">
function SetOption1RadioButton() {
document.getElementById('<%=radOption1.ClientID%>').checked = true;
document.getElementById('<%=radOption2.ClientID%>').checked = false;
</script>
I then assign this to a few controls in the code behind and add this to the page load:
protected void Page_Load(object sender, EventArgs e)
{
SetJavascriptAttributes();
}
public void SetJavascriptAttributes()
{
ddl1.Attributes.Add("onFocus", "SetOption1RadioButton()");
ddl1.Attributes.Add("onClick", "SetOption1RadioButton()");
}
Now this works great on the initial load - lovely stuff. When I click on ddl1, the radio button is set accordingly. However, when the update panel posts back, the javascript no longer works.
I'm thinking this is because the html is sent back and the javascript is no longer there.
How do I go about ensuring that, despite an update panel post back occurring, the javascript stays around a little longer and performs as it should?
Any help or suggestions gratefully received.
Since you're using updatepanel's I assume you already have an istance of the ScriptManager on your page. I suggest you use the ScriptManager to register any client scripts, this will insure they get called on partial postbacks:
ScriptManager.RegisterStartupScript();
ScriptManager.RegisterExpandoAttribute();
Simple one here... is there a clean way of preventing a user from double-clicking a button in a web form and thus causing duplicate events to fire?
If I had a comment form for example and the user types in "this is my comment" and clicks submit, the comment is shown below... however if they double-click, triple-click or just go nuts on the keyboard they can cause multiple versions to be posted.
Client-side I could quite easily disable the button onclick - but I prefer server-side solutions to things like this :)
Is there a postback timeout per viewstate that can be set for example?
Thanks
I dont think that you should be loading the server for trivial tasks like these. You could try some thing jquery UI blocking solution like this one. Microsoft Ajax toolkit should also have some control which does the same. I had used it a long time ago, cant seem to recall the control name though.
With jQuery you can make use of the one event.
Another interesting read is this: Build Your ASP.NET Pages on a Richer Bedrock.
Set a session variable when the user enters the page like Session["FormXYZSubmitted"]=false.
When the form is submitted check that variable like
if((bool) Session["FormXYZSubmitted"] == false) {
// save to db
Session["FormXYZSubmitted"] = true;
}
Client side can be tricky if you are using Asp.Net validation.
If you have a master page, put this in the master page:
void IterateThroughControls(Control parent)
{
foreach (Control SelectedButton in parent.Controls)
{
if (SelectedButton is Button)
{
((Button)SelectedButton).Attributes.Add("onclick", " this.disabled = true; " + Page.ClientScript.GetPostBackEventReference(((Button)SelectedButton), null) + ";");
}
if (SelectedButton.Controls.Count > 0)
{
IterateThroughControls(SelectedButton);
}
}
}
Then add this to the master page Page_Load:
IterateThroughControls(this);
I have had the same scenario. The solution of one of my coworkers was to implement a kind of Timer in Javascript, to avoid considering the second click as a click.
Hope that helps,
Disable the button on click, utilize jquery or microsoft ajax toolkit.
Depending on how important this is to you, could create an array of one time GUID's which you remove from the array once the update has been processed (ie posted back in viewstate/hidden field)
If the guid is not in the array on postback, the request is invalid.
Substitute database table for array in a clustered environment.