I have a page that allows the user to select various options from drop down lists. Below this is a gridview recording the details that have been submitted previously. So when the user hits submit their data is stored in a database and the gridview is rebinded to display this data. I have 3 labels that display the number of items, the total time spent and the total cost. But when the submit button is selected the labels aren't being updated straight away. The gridview updates automatically but for the labels if you refresh the page it then updates. Any why this can be updated automatically like the gridview?

 lblref.Text = Session["SessRef"].ToString();
                    try
                    {
                        //create and insert a new record
                        Activity act = new Activity();
                        act.ActivityAppRef = Session["SessRef"].ToString();
                        act.ActivityCreatedBy = HttpContext.Current.User.Identity.Name;
                        act.ActivityCreatedDate = DateTime.Now;
                        act.ActivityProcess = ddlProcess.SelectedValue;
                        act.ActivityAct = ddlActivity.SelectedValue;
                        act.ActivityGrade = ddlGrade.SelectedValue;

                        if (ddlTime.SelectedValue != "-1")
                        {
                            act.ActivityTime = Convert.ToInt32(ddlTime.SelectedValue);
                        }
                        else
                        {
                            Int32 Hours = 0;
                            if (txtTime.Text != String.Empty)
                            {
                                Hours = Convert.ToInt32(txtTime.Text);
                            }

                            Int32 Mins = Convert.ToInt32(ddlMin.SelectedValue);
                            Int32 Time = (Hours * 60) + Mins;
                            act.ActivityTime = Time;
                        }

                        //insert record
                        ActivityManager.InsertActivity(act);

                        //rebind gridvidew to show new record
                        gvActHistory.DataBind();

                        //display success message and set the values of the form back to default.
                        ddlProcess.SelectedValue = "0";
                        //ddlActivity.SelectedValue = "0";   doesn't get reset as this is dependent on value of ddlProcess
                        ddlGrade.SelectedValue = "0";
                        ddlTime.SelectedValue = "0";
                        txtTime.Text = String.Empty;
                        ddlTime.Visible = true;

So these label.text values are being updated somewhere in your code, page load maybe. Can you call a procedure somewhere in the code you posted above to update their values? Or create a procedure that executes when the user hits the submit button to update the labels?

Yea in my page load section ive an if statement to determine if it is a postback and also code to display the various totals.

//redirect user back to search page if no session variable is present
            if (Session["SessRef"] != null && Session["SessRef"].ToString() != String.Empty)
            {

                if (!Page.IsPostBack)
                {
                    lblref.Text = Session["SessRef"].ToString();               
                }

                //display totals
               ActivityTotals totals = (ActivityTotals)ActivityTotalsManager.GetActivityByAppRef(Session["SessAppRef"].ToString());
                lblActivityCount.Text = totals.ActivityActivity.ToString();
                lblCost.Text = String.Format("{0:c}", totals.ActivityCost);
                TimeSpan ts = TimeSpan.FromMinutes(totals.ActivityTime);
                lblTime.Text = String.Format("{0:%h} hours {0:%m} minutes", ts);
            }
            else
            {
                Response.Redirect("~/Search.aspx");
            }    

It is updating the totals just not right away, the user has to enter the details, submit, then refresh the page to get the latest.

The code for the button click is:

  if (Page.IsValid)
            {
                //check if the session is still valid otherwise redirect
                if (Session["SessRef"] != null && Session["SessRef"].ToString() != String.Empty)
                {
                    lblref.Text = Session["SessRef"].ToString();
                    try
                    {
                        //create and insert a new timekeeping activity record
                        Activity act = new Activity();
                        act.ActivityAppRef = Session["SessAppRef"].ToString();
                        act.ActivityCreatedBy = HttpContext.Current.User.Identity.Name;
                        act.ActivityCreatedDate = DateTime.Now;
                        act.ActivityProcess = ddlProcess.SelectedValue;
                        act.ActivityActivity = ddlActivity.SelectedValue;
                        act.ActivityGrade = ddlGrade.SelectedValue;

                        if (ddlTime.SelectedValue != "-1")
                        {
                            tk.TKRecordTime = Convert.ToInt32(ddlTime.SelectedValue);
                        }
                        else
                        {
                            Int32 Hours = 0;
                            if (txtTime.Text != String.Empty)
                            {
                                Hours = Convert.ToInt32(txtTime.Text);
                            }

                            Int32 Mins = Convert.ToInt32(ddlMin.SelectedValue);
                            Int32 Time = (Hours * 60) + Mins;
                           act.ActivityTime = Time;
                        }

                        //insert record
                       ActivityManager.InsertActivity(act);

                        //rebind gridvidew to show new record
                        gvActivityHistory.DataBind();

Why not move the label.text updates from the page load to the button_click procedure, so the values are when the event fires, just before the page reload?

That worked, thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.