Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. PostBack data lost ASP.NET 4.5

PostBack data lost ASP.NET 4.5

Scheduled Pinned Locked Moved C#
csharphtmlasp-netdatabaselinq
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paulo J Afonso
    wrote on last edited by
    #1

    Hi All we are newbies on ASP and .NET, but with some experience on C# :-D. We need some explanation about what we are doing wrong in this code and HTML page. HTML Page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GraficosDados.aspx.cs" Inherits="Testing_Studies_GraficosDados" %>

    <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">

    </form>
    

    </body>
    </html>

    C# Code

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.DataVisualization.Charting;
    using System.Data;

    using Windwater;

    public partial class Testing_Studies_GraficosDados : System.Web.UI.Page
    {
    Chart C1 = new Chart();
    System.Web.UI.WebControls.CheckBoxList cbLengend = new CheckBoxList();
    SQLHelper SQL = new SQLHelper();
    IOHelper Log = new IOHelper();
    DataTable dt = new DataTable();

    protected void Page\_Load(object sender, EventArgs e)
    {
        LoadTable();
    }
    
    private void LoadTable() 
    {
        if (!IsPostBack)
        {
            string\[\] InValues = new string\[\] { "2013-08-26 00:00", "2013-08-28 23:59", "2", "50" };
    
            Log.OnInfo(string.Format("Begin={0}\\tEnd={1}\\tTT\_Id={2}\\tpggrel\_id={3}", InValues\[0\], InValues\[1\], InValues\[2\], InValues\[3\]));
            dt = SQL.execStroredProcedures("EAPMSDAT", "wwGetDataForGraph", InValues);
            PopulateChart();
        }
    
    }
    
    private void PopulateChart()
    {
        string\[\] x = new string\[dt.Rows.Count\];
        double\[\]\[\] y = new double\[dt.Columns.Count\]\[\];
    
        for (int z = 1; z < dt.Columns.Count; z++)
        {
            y\[z\] = new double\[dt.Rows.Count\];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (z == 1)
                    x\[i\] = dt.Rows\[i\]\[0\].ToString();
                y\[z\]\[i\] = Convert.ToDouble(dt.Rows\[i\]\[z\]);
            }
            C1.Series.Add(dt.Columns\[z\].ToStr
    
    Richard DeemingR N 2 Replies Last reply
    0
    • P Paulo J Afonso

      Hi All we are newbies on ASP and .NET, but with some experience on C# :-D. We need some explanation about what we are doing wrong in this code and HTML page. HTML Page

      <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GraficosDados.aspx.cs" Inherits="Testing_Studies_GraficosDados" %>

      <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
      <title></title>
      </head>
      <body>
      <form id="form1" runat="server">

      </form>
      

      </body>
      </html>

      C# Code

      using System;
      using System.Collections.Generic;
      using System.Configuration;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.DataVisualization.Charting;
      using System.Data;

      using Windwater;

      public partial class Testing_Studies_GraficosDados : System.Web.UI.Page
      {
      Chart C1 = new Chart();
      System.Web.UI.WebControls.CheckBoxList cbLengend = new CheckBoxList();
      SQLHelper SQL = new SQLHelper();
      IOHelper Log = new IOHelper();
      DataTable dt = new DataTable();

      protected void Page\_Load(object sender, EventArgs e)
      {
          LoadTable();
      }
      
      private void LoadTable() 
      {
          if (!IsPostBack)
          {
              string\[\] InValues = new string\[\] { "2013-08-26 00:00", "2013-08-28 23:59", "2", "50" };
      
              Log.OnInfo(string.Format("Begin={0}\\tEnd={1}\\tTT\_Id={2}\\tpggrel\_id={3}", InValues\[0\], InValues\[1\], InValues\[2\], InValues\[3\]));
              dt = SQL.execStroredProcedures("EAPMSDAT", "wwGetDataForGraph", InValues);
              PopulateChart();
          }
      
      }
      
      private void PopulateChart()
      {
          string\[\] x = new string\[dt.Rows.Count\];
          double\[\]\[\] y = new double\[dt.Columns.Count\]\[\];
      
          for (int z = 1; z < dt.Columns.Count; z++)
          {
              y\[z\] = new double\[dt.Rows.Count\];
              for (int i = 0; i < dt.Rows.Count; i++)
              {
                  if (z == 1)
                      x\[i\] = dt.Rows\[i\]\[0\].ToString();
                  y\[z\]\[i\] = Convert.ToDouble(dt.Rows\[i\]\[z\]);
              }
              C1.Series.Add(dt.Columns\[z\].ToStr
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You need to read the ASP.NET Page Life Cycle[^]. In particular, note that a new instance of the page class is created to serve each request. Field values will not be preserved between the initial request and the post-back request. Dynamically created controls must be re-created on every request. If you want to keep the DataTable alive across requests, you'll need to store it in the Session or the Cache. However, since you say it contains 25K rows, this will put a lot of memory pressure on the server, and could lead to other problems.

      private void Page_Init(object sender, EventArgs e)
      {
      // Recreate the dynamic controls in the Init event
      // instead of the Load event to allow state to be preserved.
      LoadTable();
      }

      private DataTable LoadChartData()
      {
      // This key needs to be unique across your application:
      const string cacheKey = "Testing_Studies_GraficosDados:ChartData";

      // Using the Cache, since the data doesn't seem to vary between users:
      var result = (DataTable)Cache\[cacheKey\];
      if (result == null)
      {
          string\[\] InValues = new string\[\] { "2013-08-26 00:00", "2013-08-28 23:59", "2", "50" };
          
          var log = new IOHelper();
          log.OnInfo(string.Format("Begin={0}\\tEnd={1}\\tTT\_Id={2}\\tpggrel\_id={3}", InValues\[0\], InValues\[1\], InValues\[2\], InValues\[3\]));
          
          result = SQL.execStroredProcedures("EAPMSDAT", "wwGetDataForGraph", InValues);
          
          Cache.Add(cacheKey, result, 
             /\* dependencies: \*/ null, 
             /\* absoluteExpiration: \*/ DateTime.UtcNow.AddMinutes(5), 
             /\* slidingExpiration: \*/ Cache.NoSlidingExpiration, 
             /\* priority: \*/ CacheItemPriority.High, 
             /\* onRemoveCallback: \*/ null);
      }
      
      return result;
      

      }

      private void LoadTable()
      {
      DataTable dt = LoadChartData();
      PopulateChart(dt);
      }

      private void PopulateChart(DataTable dt)
      {
      var C1 = new Chart();
      var cbLengend = new CheckBoxList();
      ...
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      P 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You need to read the ASP.NET Page Life Cycle[^]. In particular, note that a new instance of the page class is created to serve each request. Field values will not be preserved between the initial request and the post-back request. Dynamically created controls must be re-created on every request. If you want to keep the DataTable alive across requests, you'll need to store it in the Session or the Cache. However, since you say it contains 25K rows, this will put a lot of memory pressure on the server, and could lead to other problems.

        private void Page_Init(object sender, EventArgs e)
        {
        // Recreate the dynamic controls in the Init event
        // instead of the Load event to allow state to be preserved.
        LoadTable();
        }

        private DataTable LoadChartData()
        {
        // This key needs to be unique across your application:
        const string cacheKey = "Testing_Studies_GraficosDados:ChartData";

        // Using the Cache, since the data doesn't seem to vary between users:
        var result = (DataTable)Cache\[cacheKey\];
        if (result == null)
        {
            string\[\] InValues = new string\[\] { "2013-08-26 00:00", "2013-08-28 23:59", "2", "50" };
            
            var log = new IOHelper();
            log.OnInfo(string.Format("Begin={0}\\tEnd={1}\\tTT\_Id={2}\\tpggrel\_id={3}", InValues\[0\], InValues\[1\], InValues\[2\], InValues\[3\]));
            
            result = SQL.execStroredProcedures("EAPMSDAT", "wwGetDataForGraph", InValues);
            
            Cache.Add(cacheKey, result, 
               /\* dependencies: \*/ null, 
               /\* absoluteExpiration: \*/ DateTime.UtcNow.AddMinutes(5), 
               /\* slidingExpiration: \*/ Cache.NoSlidingExpiration, 
               /\* priority: \*/ CacheItemPriority.High, 
               /\* onRemoveCallback: \*/ null);
        }
        
        return result;
        

        }

        private void LoadTable()
        {
        DataTable dt = LoadChartData();
        PopulateChart(dt);
        }

        private void PopulateChart(DataTable dt)
        {
        var C1 = new Chart();
        var cbLengend = new CheckBoxList();
        ...
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        P Offline
        P Offline
        Paulo J Afonso
        wrote on last edited by
        #3

        ;) ;) Thank is a great tips. :thumbsup: But if we need to use session, because the InValues can be different by users, so in that case we had to change to session. Can you send any link for how to change the cache to session, can you advice that change? :-\ Regards

        Paulo Afonso Windwater Technologies & Solutions

        Richard DeemingR F 2 Replies Last reply
        0
        • P Paulo J Afonso

          ;) ;) Thank is a great tips. :thumbsup: But if we need to use session, because the InValues can be different by users, so in that case we had to change to session. Can you send any link for how to change the cache to session, can you advice that change? :-\ Regards

          Paulo Afonso Windwater Technologies & Solutions

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          It's a fairly simple change:

          private DataTable LoadChartData()
          {
          const string cacheKey = "Testing_Studies_GraficosDados:ChartData";

          var result = (DataTable)Session\[cacheKey\];
          
          if (result == null)
          {
              string\[\] InValues = new string\[\] { "2013-08-26 00:00", "2013-08-28 23:59", "2", "50" };
              
              var log = new IOHelper();
              log.OnInfo(string.Format("Begin={0}\\tEnd={1}\\tTT\_Id={2}\\tpggrel\_id={3}", InValues\[0\], InValues\[1\], InValues\[2\], InValues\[3\]));
              
              result = SQL.execStroredProcedures("EAPMSDAT", "wwGetDataForGraph", InValues);
              
              Session\[cacheKey\] = result;
          }
          
          return result;
          

          }


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • P Paulo J Afonso

            ;) ;) Thank is a great tips. :thumbsup: But if we need to use session, because the InValues can be different by users, so in that case we had to change to session. Can you send any link for how to change the cache to session, can you advice that change? :-\ Regards

            Paulo Afonso Windwater Technologies & Solutions

            F Offline
            F Offline
            Forbiddenx
            wrote on last edited by
            #5

            You can hook events OnLoad() and still have access to session and ViewState. But, from my experience Dynamic controls can be a night mirror, so being new to dotnet you may want to look at other options to do what you are trying to do.. Like Repeater Control, ListView, etc. Also, your control ID is very important.. If you generating controls for example each time you generate them if you want them to keep their ViewState data then you must assign them the same ID they where on the prev postback. I have often implemented a list type of viewstate where I can add all the controls there.. But, then you also run into a lot of problems with this too... IE, Rendering.. etc.... life cycle hell.. So, I would see if the build in controls will do what you want before going down the dynamic path of life cycle hell. I also want to add you can use viewstate, its often better to use then sessions for passing simple postbacks. Good luck my friend... its a scary path..

            =)

            1 Reply Last reply
            0
            • P Paulo J Afonso

              Hi All we are newbies on ASP and .NET, but with some experience on C# :-D. We need some explanation about what we are doing wrong in this code and HTML page. HTML Page

              <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GraficosDados.aspx.cs" Inherits="Testing_Studies_GraficosDados" %>

              <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

              <html xmlns="http://www.w3.org/1999/xhtml">
              <head runat="server">
              <title></title>
              </head>
              <body>
              <form id="form1" runat="server">

              </form>
              

              </body>
              </html>

              C# Code

              using System;
              using System.Collections.Generic;
              using System.Configuration;
              using System.Linq;
              using System.Web;
              using System.Web.UI;
              using System.Web.UI.WebControls;
              using System.Web.UI.DataVisualization.Charting;
              using System.Data;

              using Windwater;

              public partial class Testing_Studies_GraficosDados : System.Web.UI.Page
              {
              Chart C1 = new Chart();
              System.Web.UI.WebControls.CheckBoxList cbLengend = new CheckBoxList();
              SQLHelper SQL = new SQLHelper();
              IOHelper Log = new IOHelper();
              DataTable dt = new DataTable();

              protected void Page\_Load(object sender, EventArgs e)
              {
                  LoadTable();
              }
              
              private void LoadTable() 
              {
                  if (!IsPostBack)
                  {
                      string\[\] InValues = new string\[\] { "2013-08-26 00:00", "2013-08-28 23:59", "2", "50" };
              
                      Log.OnInfo(string.Format("Begin={0}\\tEnd={1}\\tTT\_Id={2}\\tpggrel\_id={3}", InValues\[0\], InValues\[1\], InValues\[2\], InValues\[3\]));
                      dt = SQL.execStroredProcedures("EAPMSDAT", "wwGetDataForGraph", InValues);
                      PopulateChart();
                  }
              
              }
              
              private void PopulateChart()
              {
                  string\[\] x = new string\[dt.Rows.Count\];
                  double\[\]\[\] y = new double\[dt.Columns.Count\]\[\];
              
                  for (int z = 1; z < dt.Columns.Count; z++)
                  {
                      y\[z\] = new double\[dt.Rows.Count\];
                      for (int i = 0; i < dt.Rows.Count; i++)
                      {
                          if (z == 1)
                              x\[i\] = dt.Rows\[i\]\[0\].ToString();
                          y\[z\]\[i\] = Convert.ToDouble(dt.Rows\[i\]\[z\]);
                      }
                      C1.Series.Add(dt.Columns\[z\].ToStr
              
              N Offline
              N Offline
              Nelson Costa Inacio
              wrote on last edited by
              #6

              Why not define a limit of rows to return?, for example 1000. If the user wants to find more specific information should use filters. That will avoid a large consume of memory due storing 25k rows on cache and the requests to database will take less time to process.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups