I find a code for setting countdown timer but it only shows minutes not seconds when page is load and also I want that when user click on button then countdown should start and also time (e.g 10:59) should be shown on the clicked button.
Following is the code:
aspx code (ASP.net C#)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Reservation.aspx.cs" Inherits="Reservation" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManagerTimer" runat="server"></asp:ScriptManager>
<asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"></asp:Timer>
<asp:UpdatePanel id="updPnl"
runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnTimer" runat="server" BackColor="#05CC00" Height="35px" Text="Reserve" Width="89px" style="border-radius:8px" OnClick="btnTimer_Click"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
aspx.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Reservation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!ScriptManagerTimer.IsInAsyncPostBack)
Session["timeout"] = DateTime.Now.AddMinutes(10).ToString();
}
protected void timer1_tick(object sender, EventArgs e)
{
if (0 > DateTime.Compare(DateTime.Now,
DateTime.Parse(Session["timeout"].ToString())))
{
btnTimer.Text = ((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).Minutes).ToString();
}
}
}
if (0 > DateTime.Compare(DateTime.Now,
DateTime.Parse(Session["timeout"].ToString())))
{
btnTimer.Text = ((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).Minutes).ToString("HH:mm:ss");
}
TimeSpan.ToString() offers a variety of string formats. More info : MSDN
:)
Related
I use this code for populate my own Day, Month and Year Date Selector using three DropDownList in ASP.Net
Now I need set as default value for each DDL the vakue of DateTime.MinValue, that is 01/01/0001 , because the value of date to be recorded in the database is not always available :
in DDL ddlday I need as default value : 01;
in DDL ddlMonth I need as default value : January;
in DDL ddlYear I need as default value : 0001;
Using this code I don't have error but :
in DDL ddlday I have as default value : 01;
in DDL ddlMonth I have as default value : january;
in DDL ddlYear I have as default value : 2019;
Please can you help me ?
My code below.
code-behind
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DateTime dateofBirth = DateTime.MinValue;
ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
{
MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
MonthNumber = a
});
ddlMonth.DataBind();
ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
ddlYear.DataBind();
ddlday.SelectedValue = dateofBirth.Day.ToString();
ddlMonth.SelectedValue = dateofBirth.Month.ToString();
ddlYear.SelectedValue = dateofBirth.Year.ToString();
ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
ddlday.DataBind();
}
}
protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
ddlday.DataBind();
}
}
Markup
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="sc" runat="server">
</asp:ScriptManager>
<table>
<tr>
<td>
<asp:UpdatePanel ID="updpnlDay" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlday" runat="server">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlMonth" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</td>
<td>
<asp:UpdatePanel ID="updpnlMonth" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" DataTextField="MonthName"
DataValueField="MonthNumber" OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td>
<asp:DropDownList ID="ddlYear" runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
but your ddlYear doesn't have value 1, only years in the range from 1920 to 2019.
Did you mean to include min year in the range?
ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse().Append(1);
In my project there are one video tag and two buttons. I am trying to do that when I click button1 then video starts playing and when I will click another button then another video starts playing. These two videos are in video folder inside my project.
here is the test.aspx file:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="css/test.css" media="all" />
</head>
<body>
<form id="form1" runat="server">
<div>
<video id="videoplr" width="500" controls="controls" runat="server"></video>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
</div>
</form>
</body>
</html>
Here is the code behind c# file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
videoplr.Attributes["src"] = "C:\\Users\\Punam\\Desktop\\Project\\videos\\personal_talk.MP4";
}
protected void Button2_Click(object sender, EventArgs e)
{
videoplr.Attributes["src"] = "C:\\Users\\Punam\\Desktop\\Project\\videos\\Tags.MP4";
}
}
When I am clicking buttons then video is not playing. I am new bie in asp.net.
Yesterday I wrote simle example of updating UpdatePanel on timer.tick event. I noticed that on timer.tick event code of my form class is running from the beginning. Why? How to avoid it?
WebForm1.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestApp.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False"
UpdateMode="Conditional" onload="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs:
namespace TestApp
{
public partial class WebForm1 : System.Web.UI.Page
{
static Random rnd = new Random();
int b = rnd.Next(100); // always 1 value
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
UpdatePanel1.Update();
}
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
int a = rnd.Next(100); // changing value
Label1.Text = a.ToString() + " - changing value<br />" + b.ToString() + " - static value";
}
}
}
Why: Because the timer is enabled when the page loads. When the time you sit elapsed it will cause a postback to the server which will reload your page again, and this is why you see the debugger goes at the beginning of you form.
How to avoid it: Simply set Enabled property to false on page load, and then set it to true whenever you want the timer to start.
Documentation from MSDN "The Tick event is raised when the number of milliseconds specified in the Interval property has elapsed either since the Web page was rendered or since the previous Tick event."
before I begin, there is another question with a similar title and it is unsolved but my situation is pretty different since I am using Ajax.
I recently added a label to my Ajax UpdateProgress control and for some reason my asp.net page is not reading it. My original goal was for the text of the label to be constantly be updating while the long method runs. I am using code behind, and I believe the label is declared. I will post my .cs page if anyone would like to read through it (its not too long), All my other labels work perfectly and even if I take the label OUT of the ajax control it will work fine (not the text updates though). Is there a certain Ajax label I need to use?
Im pretty confused why the error is occurring. The exact error message states : "The name 'lblProgress' does not exist in the current context. Im using c#, ajax controls, a asp.net page, and visual studio. This program uploads a file to a client and stores the information in a database. If anyone can help I would really appreciate it. Thanks in advance!
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Threading;
public partial class SendOrders : System.Web.UI.Page
{
protected enum EDIType
{
Notes,
Details
}
protected static string NextBatchNum = "1";
protected static string FileNamePrefix = "";
protected static string OverBatchLimitStr = "Batch file limit has been reached. No more batches can be processed today.";
protected void Page_Load(object sender, EventArgs e)
{
Initialize();
}
protected void Page_PreRender(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
PutFTPButton.Enabled = false;
lblProgress.Visible = true;
lblProgress.Text = "Preparing System Checks...";
Thread.Sleep(3000);
Button btn = (Button)sender;
KaplanFTP.BatchFiles bf = new KaplanFTP.BatchFiles();
KaplanFTP.Transmit transmit = new KaplanFTP.Transmit();
if (btn.ID == PutFTPButton.ID)
{
lblProgress.Text = "Locating Files...";
//bf.ReadyFilesForTransmission();
DirectoryInfo dir = new DirectoryInfo(#"C:\Kaplan");
FileInfo[] BatchFiles = bf.GetBatchFiles(dir);
bool result = transmit.UploadBatchFilesToFTP(BatchFiles);
lblProgress.Text = "Sending Files to Kaplan...";
if (!result)
{
ErrorLabel.Text += KaplanFTP.errorMsg;
return;
}
bf.InsertBatchDataIntoDatabase("CTL");
bf.InsertBatchDataIntoDatabase("HDR");
bf.InsertBatchDataIntoDatabase("DET");
bf.InsertBatchDataIntoDatabase("NTS");
List<FileInfo> allfiles = BatchFiles.ToList<FileInfo>();
allfiles.AddRange(dir.GetFiles("*.txt"));
bf.MoveFiles(allfiles);
lblProgress.Text = "Uploading File Info to Database...";
foreach (string order in bf.OrdersSent)
{
OrdersSentDiv.Controls.Add(new LiteralControl(order + "<br />"));
}
OrdersSentDiv.Visible = true;
OrdersInfoDiv.Visible = false;
SuccessLabel.Visible = true;
NoBatchesToProcessLbl.Visible = true;
BatchesToProcessLbl.Visible = false;
PutFTPButton.Enabled = false;
BatchesCreatedLbl.Text = int.Parse(NextBatchNum).ToString();
Thread.Sleep(20000);
if (KaplanFTP.errorMsg.Length != 0)
{
ErrorLabel.Visible = true;
SuccessLabel.Visible = false;
ErrorLabel.Text = KaplanFTP.errorMsg;
}
}
}
Here is my aspx code as well.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="SendOrders.aspx.cs" Inherits="SendOrders" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Kaplan EDI Manager</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style1
{
width: 220px;
height: 19px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="mainPanel">
<div>
<h3>Number of Batches Created Today: <asp:Label runat="server" style="display:inline;" ID="BatchesCreatedLbl"></asp:Label>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<span class="red">COUNTDOWN TO SUBMISSION!</span>
<span id="timespan" class="red"></span>
</h3>
</div>
<div id="batchestoprocessdiv">
</div>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="BatchesToProcessLbl" runat="server" CssClass="green"
Height="22px" Text="THERE IS AN ORDER BATCH TO PROCESS."></asp:Label>
<asp:Label ID="NoBatchesToProcessLbl" runat="server" CssClass="red"
Text="There are no Order Batches to Process." Visible="false"></asp:Label>
<asp:Button ID="PutFTPButton" runat="server" onclick="Button_Click"
Text="Submit Orders" />
<asp:Label ID="SuccessLabel" runat="server" CssClass="green"
Text="Batch has been processed and uploaded successfully." Visible="false"></asp:Label>
<asp:Label ID="ErrorLabel" runat="server" CssClass="red" Text="Error: "
Visible="false"></asp:Label>
<asp:Label ID="lblProgress" runat="server" CssClass="green" Height="16px"
Text="Label" Visible="False"></asp:Label>
<br />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<br />
<img alt="" class="style1" src="images/ajax-loader.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
</div>
<div id="OrdersInfoDiv" runat="server" visible="false">
<asp:GridView ID="BatchDetails" Caption="Details of orders ready to be sent" runat="server" AutoGenerateColumns="true"
CssClass="InfoTable" AlternatingRowStyle-CssClass="InfoTableAlternateRow" >
</asp:GridView>
</div>
<div id="OrdersSentDiv" class="mainPanel" runat="server" visible="false">
<h4>Sent Orders</h4>
</div>
</form>
<script src="js/SendOrders.js" type="text/javascript"></script>
</body>
</html>
If the Label is created inside the UpdateProgress control, then you will need to do something like this
((Label)upUpdateProgress.FindControl("lblProgress")).Text = "Uploading File...";
If the control is declared in markup and the code-behind doesn't recognize it, then your designer.cs file is probably out of sync. There are a few ways to fix this:
Close the form and reopen it, and remove and add lblProgress again
Add the Label to the designer.cs file manually
Here's how to add it manually:
/// <summary>
/// lblProgress control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblProgress;
EDIT
Just noticed that you're putting the UpdatePanel in an UpdateProgress control, in which case you'll need to reference it using FindControl (like #Valeklosse) suggested.
First I have to let you know that i am a newbie in this area, learning from tutorials.
With that said I'm looking for a way to load sourcecode from the codebehind file into a textbox, when clicking a button. Same goes for the aspx file.
Im making this website, where i am going to show code examples from what im doing. So if I navigate to myweb.com/tutorial1done.aspx this page would show me the final result from the tutorial done. When i click the show source button it should make 2 textboxes visible, and add the codebehind to the first box, and the aspx source to the second box.
I don't know if it is possible, but I am hoping so.
This far i have this:
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="DateTimeOutput.aspx.cs" Inherits="WebApplication1.DateTimeOutput" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="../styles/codeformatter.css" />
</head>
<body>
<form id="form1" runat="server">
<customControls:Header runat="server" heading="Date and Time Output" />
<div>
<asp:Panel ID="Panel1" runat="server">
<asp:TextBox ID="outputText" runat="server" Height="175px" TextMode="MultiLine"
Width="400px"></asp:TextBox>
</asp:Panel>
</div>
<asp:Panel ID="Panel2" runat="server">
<asp:Button ID="runButton" runat="server" Text="Run Code"
onclick="runButton_Click" Width="95px" />
<asp:Button ID="clearButton" runat="server" Text="Clear Console"
onclick="clearButton_Click" Width="95px" />
<br />
<br />
<asp:Button ID="dt_showSource_btn" runat="server"
onclick="dt_showSource_btn_Click" Text="Show Source" />
</asp:Panel>
<asp:Label ID="dtLabel1" runat="server" Text="Code Behind:" Visible="False"></asp:Label>
<br />
<asp:TextBox ID="dtcb_output" runat="server" Height="175px"
TextMode="MultiLine" Visible="False" Width="400px"></asp:TextBox>
<br />
<br />
<asp:Label ID="dtLabel2" runat="server" Text="ASPX:" Visible="False"></asp:Label>
<br />
<asp:TextBox ID="dtaspx_output" runat="server" Height="175px"
TextMode="MultiLine" Visible="False" Width="400px"></asp:TextBox>
</form>
</body>
</html>
And the Codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class DateTimeOutput : System.Web.UI.Page
{
protected void output(String value)
{
outputText.Text += value + Environment.NewLine;
}
protected void runButton_Click(object sender, EventArgs e)
{
DateTime dt = new DateTime();
output(dt.ToString());
DateTime nowDt = DateTime.Now;
output(nowDt.ToString());
}
protected void clearButton_Click(object sender, EventArgs e)
{
outputText.Text = "";
}
protected void dt_showSource_btn_Click(object sender, EventArgs e)
{
if (dtcb_output.Visible == false)
{
dtLabel1.Visible = true;
dtcb_output.Visible = true;
}
else
{
dtLabel1.Visible = false;
dtcb_output.Visible = false;
}
if (dtaspx_output.Visible == false)
{
dtLabel2.Visible = true;
dtaspx_output.Visible = true;
}
else
{
dtLabel2.Visible = false;
dtaspx_output.Visible = false;
}
}
}
}
For now source highlighting is not needed, unless its easy to do.
Thx in advance.
If you're refering to the actual code of your code behind file, you have a problem. As the file will be compiled and then be placed as intermediate code in a dynamic link library (.dll), you don't have access to the .aspx.cs file any more. The only way to go would be to include the code behind file with the deployd project and open it with a FileStream (or whatever) to read it and display it's content.