I want to increase & decrease date on Image click like this. '<' for decrease and '>' for increment and show them in textbox. I tried like following code but not working
kindly help me to do so
protected void ImageButtonNextDate_Click(object sender, ImageClickEventArgs e)
{
DateTime date = DateTime.Now;
DateTime nextday = date.AddDays(1);
txtDate.Text = nextday.ToShortDateString();
}
protected void ImageButtonPrevDate_Click(object sender, ImageClickEventArgs e)
{
DateTime date = DateTime.Now;
DateTime nextday = date.AddDays(-1);
txtDate.Text = nextday.ToShortDateString();
}
Every time you handle click event on your images, you create a new date variable with value equal to current date. You need to store data between clicks somehow, so that it's retained for the next event. There are multiple ways to do so, for example, session variables:
protected void ImageButtonPrevDate_Click(object sender, ImageClickEventArgs e)
{
DateTime date = Session["MyDateVariable"] as DateTime ?? DateTime.Now;
DateTime nextday = date.AddDays(-1);
Session["MyDateVariable"] = nextday;
txtDate.Text = nextday.ToShortDateString();
}
I think you need to get the current value from the txtDate, then perform the logic.
protected void ImageButtonNextDate_Click(object sender, ImageClickEventArgs e)
{
txtDate.Text = (Convert.ToDateTime(txtDate.Text).AddDays(1)).ToShortDateString();
}
protected void ImageButtonPrevDate_Click(object sender, ImageClickEventArgs e)
{
txtDate.Text = (Convert.ToDateTime(txtDate.Text).AddDays(-1)).ToShortDateString();
}
UPDATE
You only need to load the value of DateTime.Now to txtDate on first page load, on every post back, do not set it.
if(!IsPostBack()){
txtDate.Text = DateTime.Now.ToShortDateString();
}
Related
I want to clear the DateTimePicker control value when i click on clear button but i can't do that with simple double qoutes, So please help me
tbAddress.Text = "";
dtpBirth.Value = "";
cBoxGender.SelectedIndex = -1;
This should do the trick
dtpBirth.CustomFormat = " ";
dtpBirth.Format = DateTimePickerFormat.Custom;
it will clear the input box.
try is
dateTimePickerDOB.Value = DateTimePicker.MinimumDateTime
in this your date and time going to be minimum date and time in your DateTimePicker and dateTimePickerDOB mean your Design(name)
or
try this for clear the date
dateTimePicker_dob.Text = string.Empty;
in here dateTimePickerDOB mean your Design(name)
The DateTimePicker.Value is from type DateTime and not a String.
dtpBirth.Value = DateTime.Now;
You can do this:
private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (dateTimePicker1.Value == DateTimePicker.MinimumDateTime)
{
dateTimePicker1.Value = DateTime.Now; // This is required in order to show current month/year when user reopens the date popup.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
}
else
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
}
}
private void Clear_Click(object sender, EventArgs e)
{
dateTimePicker1.Value = DateTimePicker.MinimumDateTime;
}
Below is my code. I want to capture the difference between two timestamps at two different button clicks, i.e., i want the "startTime" of btnStartTime_click event to be used in btnEndTime_click event.
protected void btnStartTime_Click(object sender, EventArgs e)
{
var startTime = DateTime.Now;
lblStartTime.Text = startTime.ToString("HH:mm:ss tt");
}
protected void btnEndTime_Click(object sender, EventArgs e)
{
var workDuration = DateTime.Now.Subtract(startTime).TotalMinutes;
lblEndTime.Text = ("The Work duration is "+workDuration);
}
Just make your startTime outside the local scope:
DateTime startTime;
protected void btnStartTime_Click(object sender, EventArgs e)
{
startTime = DateTime.Now;
lblStartTime.Text = startTime.ToString("HH:mm:ss tt");
}
protected void btnEndTime_Click(object sender, EventArgs e)
{
var workDuration = DateTime.Now.Subtract(startTime).TotalMinutes;
lblEndTime.Text = ("The Work duration is "+workDuration);
}
Since this concerns a web application, you must store the startTime in a way where it can be restored on a later post back.
Here's a quick sample that should work using ViewState:
private const string StartTimeViewstateKey = "StartTimeViewstateKey";
protected void btnStartTime_Click(object sender, EventArgs e)
{
var startTime = DateTime.Now;
ViewState[StartTimeViewstateKey] = startTime.ToString(CultureInfo.InvariantCulture);
}
protected void btnEndTime_Click(object sender, EventArgs e)
{
var startTime = DateTime.Parse((string)ViewState[StartTimeViewstateKey], CultureInfo.InvariantCulture);
var workDuration = DateTime.Now.Subtract(startTime).TotalMinutes;
lblEndTime.Text = ("The Work duration is " + workDuration);
}
Alternatively you could use session state:
private const string StartTimeSessionKey= "StartTimeSessionKey";
protected void btnStartTime_Click(object sender, EventArgs e)
{
var startTime = DateTime.Now;
Session[StartTimeSessionKey] = startTime;
}
protected void btnEndTime_Click(object sender, EventArgs e)
{
var startTime = (DateTime)Session[StartTimeSessionKey];
var workDuration = DateTime.Now.Subtract(startTime).TotalMinutes;
lblEndTime.Text = ("The Work duration is " + workDuration);
}
Firstly, apologies for asking a question that has been asked before, but even with the examples I am not getting the desired results.
All I am trying to do is display the current time, which it does, but I noticed that the datetime format was 9:5:6 instead of 09:05:06. I read the examples about formatting DateTime but it doesn't work for some reason. Can anyone shed any light on where I am going wrong?
Thanks for your help as always.
public MainWindow()
{
InitializeComponent();
DispatcherTimer dispatchTimer = new DispatcherTimer();
dispatchTimer.Tick += new EventHandler(dispatchTimer_Tick);
dispatchTimer.Interval = new TimeSpan(0, 0, 1);
dispatchTimer.Start();
}
private void dispatchTimer_Tick(object sender, EventArgs e)
{
var hour = DateTime.Now.Hour.ToString();
var min = DateTime.Now.Minute.ToString();
var sec = DateTime.Now.Second.ToString();
var today = hour + ":" + min + ":" + sec;
label1.Content = today;
textBlock1.Text = today;
button1.Content = today;
}
Just use a custom format string:
var today = DateTime.Now.ToString("HH:mm:ss");
Or the standard one:
var today = DateTime.Now.ToString("T");
string now = DateTime.Now.ToString("HH:mm:ss");
Check TimeZoneInfo Class for more detailed globalization.
I think there are many ways to tackle this issue.
Personally, and to keep things simple, I would do it this way:
private void dispatchTimer_Tick(object sender, EventArgs e)
{
string wTime = DateTime.Now.ToString("HH:mm:ss");
// OR THIS WAY
string wTime2 = DateTime.Now.ToString("T");
label1.Content = wTime;
textBlock1.Text = wTime;
button1.Content = wTime;
}
But if you want for some reasons to keep your initial logic then this would do it too.
private void dispatchTimer_Tick(object sender, EventArgs e)
{
string hour = DateTime.Now.Hour.ToString("00");
string min = DateTime.Now.Minute.ToString("00");
string sec = DateTime.Now.Second.ToString("00");
var today = hour + ":" + min + ":" + sec;
label1.Content = today;
textBlock1.Text = today;
button1.Content = today;
}
You may also wish to look at this http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
I want to take the DateTime of the user's connection. I guess I will use the Session variable because I don't want that the DateTime changes at every refresh of my page.
Maybe something like that :
void Session_Start(object sender, EventArgs e)
{
Session["dateandhour"] = DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + "." + DateTime.Now.Hour + "." + DateTime.Now.Minute + "." + DateTime.Now.Second;
}
protected void Page_Load(object sender, EventArgs e)
{
string hour= (string)(Session["dateandhour"]);
lab10.Text = hour;
}
You can save the DateTime directly:
void Session_Start(object sender, EventArgs e)
{
Session["dateandhour"] = DateTime.Now;
}
protected void Page_Load(object sender, EventArgs e)
{
DateTime time = (DateTime)(Session["dateandhour"]);
lab10.Text = time.Hour;
}
This greatly simplifies subsequent use of the data.
I have a web application consisting of an aspx-file.
On page load two textboxes are filled with data (a "username" and a "password"). This works.
On a button click it should save the textboxes' text. But for some reason the text of the textboxes isn't updated if I have changed it manually meanwhile (by typing in some letters with my keyboard).
Why is that? And how can I tell my program to regard my changes?
My code is:
protected void Page_Load(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
protected void Button_Speichern_Click(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
cm.setCredentials(TextBox_Benutzername.Text, TextBox_Passwort.Text);
}
EDIT:
It works with this improvement:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
For further information, see answers below. Thanks everyone!
Try checking for a postback -
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
Your Page_Load code will currently run after every button click (or postback), and overwrite the values you have manually added.
Try this,
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
You are assiging the value to the textboxes on every page load instead of firt page load.
Change the Page_Load method to :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
I think the problem is that you are creating a new CredentialsManager each and every time that the page is loaded (I assume that a new CredentialsManager has an empty Username and Password fields). You should only do that on new page loads, and not when the page is refreshed because of a button click. That is determined with the Page.IsPostBack property, so you moght need to do:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}