This is just the sample code which I am trying to achieve and it cannot be performed by Javascript.
Used an Updatepanel and trying to update the progress bar that it shows the progress while function is in execution
But when doing this, updatepanel starts from zero when it is refreshed
(It's not smooth continuous transition, rather it starts from zero and reaches to specified point everytime when timer-tick event occurs)
ASPX
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer runat="server" ID="Timer1" Interval="1000" Enabled="False"
ontick="Timer1_Tick" />
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<br />
<div id="div3" runat="server" class="progress progress-danger progress-striped progress progress_sm active " style="width: 100%;">
<div id="e3" class="bar" runat="server" role="progressbar">
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
JavaScript
var percentage;
function updateProgress2(percentage) {
var $bar1 = $("#e3");
//var $bar1 = $('.bar');
var pp = percentage + "%";
$bar1.width(pp);
$bar1.text(pp + "%");
}
C#
static int count2, i;
protected void Button1_Click(object sender, EventArgs e)
{
i=0;
count2=20;
Button1.Enabled = false;
Timer1.Enabled = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "lol", "updateProgress2('" + count2 + "')", true);
i++;
count2 += 10;
if (count2 == 90)
{
Timer1.Enabled = false;
Button1.Enabled = true;
}
}
i want see your 'count2' property.
i can use this 'count2' property with session.
this code work correctly.
private int count2
{
get
{
if (Session["count2"] == null)
Session["count2"] = 0;
return int.Parse(Session["count2"].ToString());
}
set
{
Session["count2"] = value;
}
}
Related
There is a simple code in which I added a timer and generated tick event but the event is not firing.
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick" Enabled="True" EnableViewState="True" ViewStateMode="Inherit" ClientIDMode="Inherit"></asp:Timer>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
setImageurl();
//Timer1.Tick += new EventHandler(Timer1_Tick);
//Timer1.Interval = 1000;
}
}
void setImageurl()
{
Random _rand = new Random();
int i = _rand.Next(1, 4);
Image1.ImageUrl = "~/ImagesSlideshow/" + i.ToString() + ".jpg";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
setImageurl();
}
I dont know why this is happening. Help me out.
Set EnableCdn="true" on the ScriptManager and it will work, just tried it on my side:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableCdn="true"></asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000"></asp:Timer>
<asp:Image ID="Image1" runat="server" />
</form>
As a general rule of thumb you have to set Timer1.Enabled=true for a Timer control to fire it's Tick() event.
i wanted to use a timer in a program i'm working on, but it always stops after 1 tick !! can you give me any tips to make it repeat unstoppably (or until i want it to) please ??
this is my code:
int i = 20;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "(20)";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
i--;
Label1.Text = "(" + i + ")";
if (i == 0)
{
Session["Profession"] = "Visiteur";
Response.Redirect("Acceuil.aspx");
}
Edit: my HTML code :
<form id="form1" runat="server">
<div><center>
<span class="style1">message</span><br
class="style1" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick" >
</asp:Timer>
<asp:Timer ID="Timer2" runat="server" Interval="1000">
</asp:Timer>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"
PostBackUrl="~/Acceuil.aspx">Retour</asp:LinkButton>
<br />
</center>
</div>
</form>
Edit2: what i want to do is the normal redirecting code, count to 20 and refreshing every second (to show to user how much secs left) and at the end it redirects to a new page, but it wasn't working, so i was wondering why ??
and i think i got my answer from #Andrei Rînea, and thank you everyone for your help : )
Edit3: Solved and here is the code :
static int i = 20;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
Label1.Text = "20";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
i--;
Label1.Text = i.ToString();
if (i == 0)
{
i = 20;
Session["Profession"] = "Visiteur";
Response.Redirect("Acceuil.aspx");
}
}
Thanks everyone again : )
Please use the following code :
static int i = 20;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
Label1.Text = "(20)";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
i--;
Label1.Text =i.ToString();
if (i == 0)
{
Session["Profession"] = "Visiteur";
Response.Redirect("Acceuil.aspx");
}
}
your HTML code should be like :
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="1000">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
I'm going to assume here that you are wishing to have a process that ticks away on your server in the background.
I'm also going to assume you've tried to add this to your Webform.
If so, the issue you have encountered, is that your Webform object only exists for the short time that it is processing your request, after which it is disposed of - including your timer.
If I'm correct, you'd probably like to take a look at Quartz.Net:
http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net
You are trying to run client side code on the server. You need to do this in JavaScript instead.
The C# code will be run just before rendering the page and not longer. You probably believe the code will run as long as the visitor is on the page which is not the case.
Hi i am loading huge data by asp.net Ajax on button click in grid view showing loading message on update prgress ...in update progress time i have to disable my BtnLoadReport Button.
<td>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:UpdateProgress ID="updProgress"
AssociatedUpdatePanelID="UpdatePanel1"
runat="server" oninit="updProgress_Init" onprerender="updProgress_PreRender">
<ProgressTemplate>
<span style="color:green; font-style:italic;">Loading, please wait...</span>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div id="DemoId">
<asp:Button ID="BtnLoadReport" runat="server" onclick="BtnLoadReport_Click"
Text="LoadReport" Width="176px" ClientIDMode="Static" OnClientClick="return clickOnLoadReport();" />
</div>
<asp:GridView ID="GridView1" runat="server" Height="170px"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="438px">
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BtnLoadReport" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
my script to disable button
<script type="text/javascript">
var updateProgress = null;
function clickOnLoadReport() {
// This will disable all the children of the div
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm.get_isInAsyncPostBack()==false) {
var nodes = document.getElementById("DemoId").getElementsByTagName('*');
for (var i = 0; i < nodes.length; i++) {
nodes[i].disabled = true;
// nodes[i].off("click");
}
}
}
</script>
but it is not disabling my button for long it is disabling it for just seconds
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(this.BtnLoadReport);
}
protected void BtnLoadReport_Click(object sender, EventArgs e)
{
// System.Threading.Thread.Sleep(3000);
UpdatePanel1.Update();
DataSet dataSet = new DataSet();
dataSet.ReadXml(#"C\Data.Xml");
GridView1.DataSource = dataSet.Tables[0];
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void updProgress_Init(object sender, EventArgs e)
{
BtnLoadReport.Enable = false;
}
protected void updProgress_PreRender(object sender, EventArgs e)
{
BtnLoadReport.Enable
= true;
}
this above thing i tried to disable my button BtnLoadReport on Update progress still not working
Thanks in Advance
function clickOnLoadReport() {
var requestManager = Sys.WebForms.PageRequestManager.getInstance();
requestManager.add_initializeRequest(CancelPostbackForSubsequentSubmitClicks);
function CancelPostbackForSubsequentSubmitClicks(sender, args) {
if (requestManager.get_isInAsyncPostBack() &
args.get_postBackElement().id == 'BtnLoadReport') {
args.set_cancel(true);
document.getElementById("BtnLoadReport").setAttribute("disabled", "disabled");
//alert('A previous request is still in progress that was issued on clicking ' + args.get_postBackElement().id);
}
}
}
i made changes in my javascript function it solve my problem
All,
I have an Update Panel control that contains a label control. I also have a Timer Control whose interval is set to 1 sec. The timer control is suppose to set the text of the label control every second with the value of a public property that changes after each iteration of the loop.
However the resulting functionality is that after the entire loop completes then the UI is updated. I'd like to know what would need to be done/coded to make sure the label control gets updated with the value of the _servername property after every iteration of the loop?
Heres my code:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="tmr" runat="server" Interval="1000" ontick="tmr_Tick">
</asp:Timer>
<div>
<asp:UpdatePanel ID="udp1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tmr" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblInserts" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress runat="server" ID="udprg1" AssociatedUpdatePanelID="udp1"
DisplayAfter="10">
<ProgressTemplate>
<img src="media/ajaxloaderBlueLoading.gif" alt="ProgressImage" />
</ProgressTemplate>
</asp:UpdateProgress>
<div id="_asyncCallsMadeDiv"></div>
</div>
</form>
//CODE BEHIND
public string ServerName
{
get { return _serverName; }
set { _serverName = value;}
}
protected void tmr_Tick(object sender, EventArgs e)
{
lblInserts.Text = ServerName;
}
protected void btnUpload_Click(object sender, EventArgs e)
{
//Loop through data
while((line = rdr.ReadLine()) != null)
{
string [] arrayline = line.Split(',');
ServerStatus s = new ServerStatus
{
ServerName = arrayline[0],
Purpose = arrayline[1],
Primary = arrayline[2],
Secondary = arrayline[3],
OS = arrayline[4],
MachineType = arrayline[5],
Comments = arrayline[6],
VMTools = arrayline[7],
TimeSettings = arrayline[8],
LastPatchDate = arrayline[9],
CARemoval = arrayline[10],
PLUpdate = arrayline[11],
DefaultGatewayChange = arrayline[12]
};
_serverName = arrayline[0];
}
The onTick property of your timer control is set to a method that doesn't exist. You can rename tmr_Tick method in your code behind to tmrUdp1_Tick or set the onTick property to tmr_Tick
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!
Why don't you implement the Timer entirely on the ClientSide? Can you explain why it has to be a callback? What does it need to do on the Server?
<script type="text/javascript" language="javascript">
var timer = 0;
function resetTimer() {
timer = 0;
timerTick();
}
function timerTick() {
var target = document.getElementById("timerResult");
target.innerHTML = timer++;
window.setTimeout("timerTick()", 1000);
}
</script>
And in the body tag...
<body onload="timerTick();">
And somewhere to display
<div id="timerResult" >timerResult</div>
You justy have to add a button chich calls resetTimer(). I'll leave that to you