I have a web form (with master page). In this web form I include a Web User Control, which consists of a label and a button.
On the first load I want the label to display today's date. If the user clicks the button, the date should be updated by adding 1 day.
But on the first click nothing happens. The 2nd, 3rd, etc. time it works fine.
What am I missing? :)
mrfreeze
Member
7 Points
7 Posts
Viewstate seems to work only 2nd time
Jan 09, 2013 01:03 PM|LINK
I have a web form (with master page). In this web form I include a Web User Control, which consists of a label and a button.
On the first load I want the label to display today's date. If the user clicks the button, the date should be updated by adding 1 day.
But on the first click nothing happens. The 2nd, 3rd, etc. time it works fine.
What am I missing? :)
Here's the code:
Web form:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <%@ Register TagPrefix="uc" TagName="Bookings" Src="~/Bookings.ascx" %> <!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"> <uc:Bookings ID="Bookings1" runat="server" /> </form> </body> </html>Web user control:
using System; public partial class Bookings : System.Web.UI.UserControl { DateTime selectedDate; protected void Page_Load(object sender, EventArgs e) { if (ViewState["SelectedDate"] != null) { selectedDate = (DateTime)ViewState["SelectedDate"]; } else { selectedDate = DateTime.Today; } lblDate.Text = selectedDate.ToString(); } protected void btnNextDay_Click(object sender, EventArgs e) { selectedDate = selectedDate.AddDays(1); ViewState["SelectedDate"] = selectedDate; } }mrfreeze
Member
7 Points
7 Posts
Re: Viewstate seems to work only 2nd time
Jan 09, 2013 02:17 PM|LINK
test
AidyF
Star
9204 Points
1570 Posts
Re: Viewstate seems to work only 2nd time
Jan 09, 2013 02:31 PM|LINK
Your page load is happening before the button click event so;
Page load runs for the first time >>>
Nothing in view state so makes selectedDate today's date
Shows selectedDate (today's date)
User clicks button >>>
Page load gets selectedDate from view state (which is today's date)
Shows selectedDate (today's date)
btnNextDay_Click runs
Adds 1 to selectedDate (making it today + 1) and stores it in view state
Note at this point the label is still showing today's date as you've updated it in the button click event but you haven't updated the label
User clicks button again >>>
Page load gets selectedDate from view state (which is today's date + 1)
Shows selectedDate (today's date + 1)
btnNextDay_Click runs
Adds 1 to selectedDate (making it today + 2) and stores it in view state
What you have to do is update the label again inside your click event
protected void btnNextDay_Click(object sender, EventArgs e) { selectedDate = selectedDate.AddDays(1); ViewState["SelectedDate"] = selectedDate; lblDate.Text = selectedDate.ToString(); }mrfreeze
Member
7 Points
7 Posts
Re: Viewstate seems to work only 2nd time
Jan 09, 2013 02:41 PM|LINK
That's the solution, thanks!
I didn't realize the page_load happens befóre the click event.