ASP.NET/AJAX - Timer not working correct - c#

I'm trying to make an webapplication where you see an Ajax countdown timer. Whenever I push a button the countdown should go back to 30 and keep counting down.
Now the problem is whenever I push the button the timer keeps counting down for a second or 2 and most of the time after that the timer keeps standing on 30 for to long.
WebForm code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="geen verbinding"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
</form>
Code Behind:
static int timer = 30;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = timer.ToString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
timer--;
}
protected void Button1_Click(object sender, EventArgs e)
{
timer = 30;
}
Hope somebody knows what the problem is and if there is anyway to fix this.
Thanks in advance!

since timer is processing the page asynchronously, and the button click event takes time for processing on the server, the timer event still fires in between the button click event and hence the timer keeps counting back for a second or two. Use Java script to set the label to 30 on client side as soon as the reset button is clicked. Upon timer click event, decrement the label value (not the timer) and assign to the label the new value. No need for timer int variable. Also on page load, assign the label value only if the page is not postback (i.e. IsPostback is false) as we want to load label value only on first time the page is rendered. Rest of the time, the timer click event will assign the value.

The problem was that Visual Studio hosted it on localhost. If you use ip-adress 127.0.0.1 instead of local host within the URL it worked fast. I guess this won't be a problem on faster machines, sadfully I had none at that time.
EDIT: Adding a bounty on this question was a mistake, sorry about that.

Related

OnCheckedChanged fires one time, when I click twice very fast

I'm using checkbox on asp.net webforms. But it fires one time, when I click twice very fast.
This is my code.
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<asp:CheckBox ID="chk1" runat="server" meta:resourcekey="chk1" Text="Yes" CssClass="NormalBold" AutoPostBack="true" OnCheckedChanged="chk1_CheckedChanged"/>
</asp:UpdatePanel>
This is server code.
protected void chk1_CheckedChanged(object sender, EventArgs e)
{
var x = chk1.Checked;
}
How it works now. For example, my checkbox is unchecked. I click twice very fast. As a result checkbox unchecked again, but chk1_CheckedChanged method performs only one time, and chk1.Checked value equals true.
Any ideas?

Update an update panel within loop ASP.Net Web Forms C#

I have been trying to find a way to relay incremental feedback of a process to the UI via an update panel. A basic example of what I am trying to implement is as follows:
protected void DoSomething(object sender, EventArgs e) {
for(int i=0; i < 10; i++){
//Pretend to do something intensive
Thread.Sleep(1000);
//Output the progress of the process to a label
Label.Text = i.ToString();
}
}
The aspx markup in my example is simply:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
<asp:Button ID="Button1" runat="server" OnClick="DoSomething" Text="Push Me"/>
However when Button1 is pressed the UpdatePanel does not update incrementally, instead it only displays the number 9 when the loop completes.
I believe this is because no postback is triggered until the server side code completes (i.e. when the loop stops looping).
Is there a way I can achieve the desired functionality using server side code and update panels?
Thanks in advance! :)
What you can do is, create a timer object in design mode (or punch it in your html code) and also add a async trigger to that timer. This link will help you a great deal!
http://msdn.microsoft.com/en-us/library/vstudio/bb386404(v=vs.100).aspx
In other words, the timer will trigger update in panel in that time interval you set up :)
In code behind, every timed event,y ou update your update panel w/ incremental value.

asp.net loads to the first tab when i make action in another tab

I have three tabs like this:
When I press on the last table, there is a code behind executing to get data from database. I have done all of that.
my problem
in the last tab there is a button, when I press it. it loads the whole page and then returns to the first tab open.
what I want
when I pressing that button in the last tab, I want to get the data and present it, whit out making the page goes to the first tab.
i am sure it is something about post back but i don't know anything about it
Edit
this is my code in c#
protected void BookingForDate_Click(object sender, EventArgs e)
{
//do something in database and fill the table
}
Edit2
protected void Page_Load(object sender, EventArgs e)
{
// go to database and get data to fill it in the first tab
}
Here is a post that should get you started with update panels
http://msdn.microsoft.com/en-us/library/bb386454.aspx
Here is the minimum you need on the page with it writing out the datetime. so you can see it works
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<!-- Other content in the panel. -->
<%=DateTime.Now.ToString() %><br />
<asp:Button ID="Button1" Text="Refresh Panel" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>

Timer in UpdatePanel causes some sort of full postback

I have some sort of a strange problem, I have an update panel which is triggerd by a timer.
Also I have on this page is a function that calls the DB and retrieves data from it. the function does not being called from the update panel or even related to it.
the problem is that I see in my log file that every time there is a tick and the update panel being updated there is also a call to my DB server (this function is in the page_Load section ) to retrieve the data again. but the page doesn't seem to do a full postback (It stays the same and doesn't looks like being reloaded)
my code:
<asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
</Triggers>
<ContentTemplate>
<span id="s1" runat="server"></span>
</ContentTemplate>
</asp:UpdatePanel>
and code behind:
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
DateTime dt = TimeConvertor.getCurrentGameTime();
s1.InnerText = String.Format("Current game time: {0:dd/MM/yyyy HH:mm}", dt);
}
The function to retrieve data from the DB is in the page_load of the page.
Any help is appreciated
Thank you
Doron
Using the Update panel doesn't refresh whole page, only controls inside update panel, but when Partial Update is executed all the server page life-cycle events occur, and view-state and form data are preserved, but when rendering page only part of update panel is rendered and returned to user.
Go to this link Partial page rendering
and scroll to the section background.

Cause an async postback when a listbox is double-clicked

I have an ASP.NET listbox, lstActivities. To edit an item in the list, users can either click lnkButton or double-click on the listbox. I achieve this with:
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack) return;
var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
lstActivities.Attributes.Add("ondblclick", refDblClick);
}
protected override void Render(HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation(lnkButton.UniqueID, "dblClick");
base.Render(writer);
}
I would like to change this so that the postback is asynchronous, using AJAX. At the moment, the listbox and button are in an UpdatePanel so there is an async postback when the button is clicked. But when the listbox is double-clicked, a full postback occurs.
<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
runat="server">
<ContentTemplate>
<asp:ListBox ID="lstActivities" runat="server"></asp:ListBox>
<asp:LinkButton ID="lnkButton" runat="server" OnClick="lnkButton_Click">
Edit</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
How can I make the double-click refresh only the UpdatePanel?
A few things to try:
<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lstActivities" />
<asp:AsyncPostBackTrigger ControlID="lnkButton" />
</Triggers>
.........
</asp:UpdatePanel>
or
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack) return;
var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
lstActivities.Attributes.Add("ondblclick", refDblClick);
ScriptManager1.RegisterAsyncPostBackControl(lstActivities);
}
I just a ran a quick test with the code you provided and I do get "partial postbacks" (for lack of a better term since updatepanels always do full postbacks) on both, the clicking of the button and the double clicking of the list.
If you set other panels in that page to UpdateMode="Conditional" as you are doing with your UpdatePanel "up", then only elements inside "up" will be updated. If you don't specify the update mode on the other panels, then they will always be updated on postback, because, again, update panels ALWAYS do full postbacks; what they really do is partial refreshes of the page.
Linking MSDN documentation regarding UpdatePanel as I think is a very helpful read.
I tried the solutions suggested, but with no luck. It's quite a complicated page with lots of UpdatePanels so hard to work out the exact problem.
In the end I went for jQuery:
$(document).ready(function () {
$(document).delegate('#ctl00_body_lstActivities', 'dblclick', function () {
eval($('#ctl00_body_lnkButton').attr('href'));
});
});

Categories