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.
Related
I have some long code on server side that runs on Page_Load(). My problem is that users don't see nothing until this code is over
I want to show them some loading picture so that they will know that something is happening. I tried to put some JavaScript alert() on page but it didn't helped. I tried to changed html from server-side but of course it only shows the changes after job is done.
protected void Page_Load(object sender, EventArgs e)
{
// show user a loading image
// start heavy coding
}
You can use AJAX or UpdatePanel to separate loading and user-visualizing processes.
In this case you just need pageLoad() - js function, which will call proper, for example, web-service-method and shows loading image. OnSuccess\OnError events if ajax call will help you to handle the result of this call
So, I've come across an issue where my favorite radio station plays a song I don't know while I'm driving. They don't have one of those pages that shows a list of songs that they've played; however, they do have a "Now Playing" section on their site that shows what's currently playing and by who. So, I am trying to write a small program that will poll the site ever 2 minutes to retrieve the name of the song and the artist. Using Chrome dev tools, I can see the song title and artist in the source. But when I view the page source, it doesn't show up. They are using a javascript to run display that info. I've tried the following:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(#"http://www.thebuzz.com/main.html");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
do
{
// Do nothing while we wait for the page to load
}
while (webBrowser1.ReadyState == WebBrowserReadyState.Loading);
var test = webBrowser1.DocumentText;
textBox1.Text = test.ToString();
}
Essentially, I'm loading it into a WebBrowser and trying to get the source this way. But I'm still not getting the part after the javascript is run. Is there a way to actually retrieve the rendered HTML after the fact?
EDIT
Also, is there a way in the WebBrowser to allow scripts to run? I get popups asking me if I want to allow them to run. I don't want to suppress them, I need them to run.
As Jay Tomten said in the comments, you're trying to fix the result of your problem, not the cause. The cause of the problem is that they're using Javascript to update that part of the page. Instead of working around that by letting the Javascript do its update and then reading what it wrote, ask yourself where the Javascript is getting the info from and whether you can go to the same place. Open up something that lets you see web traffic - Fiddler, or Chrome's dev console, for example. Watch for POST calls. One of them will likely be an AJAX request in which the Javascript on the page is getting the current song. Note the URL, inspect the call to see what parameters it sends and what data it gets back. You can use Postman or something like it to assemble a POST request and work out how the Javascript on that site is getting its data, and then write a little code to make your own call to that URL and parse what comes back.
I want to autopost a page in Page_Unload. When I write Response.Redirect, I got error.
Want to achieve show Data List. I'm databinding it, but it shows after refreshing the page.
Can somebody help me?
protected void Page_Unload(object sender, EventArgs e)
{
...
DataList1.DataBind();
//autopostback in this line
}
You can't do anything like that in the Unload event.
Whe the Unload event happens, the page has already been rendered and sent to the browser, so it's too late to do anything to change the response.
Besides, making a postback from server code doesn't make sense, as that would simply create an eternal loop without anything ever being sent back to the browser. If you want to make a postback when something happens in the browser, you would do that using Javascript, not in the server code.
use the PreRender event instead of UnLoad.
Note: I know its old question but I believe some one would get use of this answer.
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.
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();