unable to display minute through Timer - c#

I want to display time in a label. My code is:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="3000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text="Remaining Time:(Sec)"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="100"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
and in aspx page
protected void Timer1_Tick(object sender, EventArgs e)
{
Label2.Text = Convert.ToString((Convert.ToInt32(Label2.Text) - 1));
if (Convert.ToInt32(Label2.Text) == 0)
Response.Redirect("~/Default2.aspx");
}
It works fine, but i want to display the minute instead of the second. Is there any method to do this? Thanks.

Change your Interval to 60000 which should represent one minute.

Try this
protected void Timer1_Tick(object sender, EventArgs e)
{
Label2.Text = Convert.ToString((Convert.ToInt32(Label2.Text) - 1)/60);
if (Convert.ToInt32(Label2.Text) == 0)
Response.Redirect("~/Default2.aspx");
}

Related

How to force stopwatch - start/stop from label1.text?

I am practicing a small project. According to mouse movements, the stop watch has to be started or stop.
ie, once the mouse is inside then start the stopwatch otherwise stop. Is it possible?
my codes: aspx page
<div id="Divn1" onmousemove="MyNewFunction(event)" onmouseout="ClearCoor()" style="width:99%; height:99%; border:solid;background-color:white; position:absolute;">
<p id="Demo1"></p>
<asp:Label ID="Label2" runat="server" Font-Size="XX-Large"></asp:Label>
<div id="Divn2" style="width:50px;height:50px;border-radius:50%;background-color:brown; position:absolute; top:45%; margin-left:47%;">
</div>
<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Font-Size="XX-Large"></asp:Label>
<asp:Timer ID="tm1" Interval="1000" runat="server" ontick="tm1_Tick" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tm1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</div>
And then aspx.cs
public static Stopwatch sw;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sw = new Stopwatch();
sw.Start();
}
}
protected void tm1_Tick(object sender, EventArgs e)
{
long milsecs = sw.Elapsed.Milliseconds;
if (Label2.Text.Trim().Length > 0)
{
Label1.Text = DateTime.Now.ToString("00:ff");
}
sw.Stop();
//Label1.Visible = false;
}
Thanks

How do I update only a panel on check changed?

<asp:UpdatePanel id="up1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:CheckBox ID="chkParent" runat="server" autopostback="true" OnCheckedChanged="chkParent_OnCheckedChanged" />
<asp:DataList ID="ChildList" runat="server" OnItemDataBound="ChildList_OnItemDataBound">
</ContentTemplate>
</asp:UpdatePanel>
protected void chkParent_OnCheckedChanged(object sender, EventArgs e)
{
this.ChildList.DataSource = this.ChildValues;
this.ChildList.DataBind();
}
On check changed of parent, child gets updated but the entire page also loads. Any way to avoid it?

How to update textbox onchange without postback?

Is there a way to update a textbox without using postback? I'd like to populate the txtDFS field with the value of (100 - txtStocked) after the user enters data in the txtStocked field.
The following code works, but it uses postback so the page refreshes. Is there a way to accomplish this without refreshing the page?
<asp:TextBox ID="txtStocked" runat="server" Width="75px" AutoPostBack="true" OnTextChanged="txtStocked_TextChanged"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px"></asp:TextBox>
protected void txtStocked_TextChanged(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
txtDFS.Text = txtStocked.Text;
}
Solved this by using an UpdatePanel. Here is the updated code:
<asp:ScriptManager ID="SCManager" runat="server" />
<asp:UpdatePanel ID="SCPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txtStocked" runat="server" Width="75px" autopostback="true" OnTextChanged="Update_Text"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px" ReadOnly="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
protected void Update_Text(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
SCPanel.Update();
}

Why no render partial with UpdatePanel?

I need partial render with ajax; I don't know what is wrong. ¿What is the problem?
My code:
ascx
<div id="temasatratar" onclick="__doPostBack('UpdatePanel1', '');"><h1>Temas a tratar</h1></div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label runat="server" ID="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
ascx.cs
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
Random rnd = new Random();
int number = rnd.Next(0, 99999);
Label1.Text = "Best "+number;
}
Any suggest?
My application: Sharepoint - Visual web part / C# / Asp.Net / Visual Studio
I would use a fake-button that is invisible as trigger for the UpdatePanel:
<div id="temasatratar" onclick="__doPostBack('fakeButton', '');"><h1>Temas a tratar</h1></div>
<asp:LinkButton ID="fakeButton" style="display:none" runat="server" Text="foo" OnClick="fakeButton_Click" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label runat="server" ID="Label1" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="fakeButton" />
</Triggers>
</asp:UpdatePanel>
Now this click-event is handled in an async postback when the user clicks on the div.
protected void fakeButton_Click(Object sender, EventArgs e)
{
// you are in an asynchronous postback now
}

Keep generic list values after postback error

I'm getting "Type 'ViewStateTest._Default+sInfo' in Assembly... is not marked as serializable" error when trying to keep generic list values after postback, what am I doing wrong here?
code:
public partial class _Default : System.Web.UI.Page
{
List<sInfo> InfoList1 = new List<sInfo>();
sInfo Info1;
struct sInfo
{
public int LSR;
public string BrandAcc;
public string CreateDate;
public string QName;
}
private void CreateArray()
{
Info1.LSR = 2;
Info1.BrandAcc = "AA";
Info1.CreateDate = "12/12/2011";
Info1.QName = "Completed";
InfoList1.Add(Info1);
}
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["arrayListInViewState"] != null)
{
InfoList1 = (List<sInfo>)ViewState["arrayListInViewState"];
}
else
{
// ArrayList isn't in view state, so we need to load it from scratch.
CreateArray();
}
// Code that uses PageArrayList.
Label1.Text = InfoList1[0].LSR.ToString();
Label2.Text = InfoList1[0].BrandAcc;
Label3.Text = InfoList1[0].CreateDate;
Label4.Text = InfoList1[0].QName;
}
void Page_PreRender(object sender, EventArgs e)
{
// Save PageArrayList before the page is rendered.
ViewState.Add("arrayListInViewState", InfoList1);
}
protected void Timer1_Tick(object sender, EventArgs e)
{
}
html code:
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick"></asp:Timer>
<asp:HiddenField ID="hfTableTopInfoCount" runat="server" Value="0" />
<div id="Container">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
[Serializable]
struct sInfo
Objects that go into viewstate need to be marked serialializable.

Categories