Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear friend I am jitendra kumar working on an Web based ERP Application

I want to store multiple values of an column in viewstate and retrive one by one in string type variable to send from one page to another page.

C#
using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    using System.Text.RegularExpressions;
    using System.Collections.Generic;

    namespace egurkul
    {
     [Serializable]
     public class field
     {
        public int class_Id { get; set; }
        public string adm_no { get; set; }
     }
    public partial class Searchstudentresult : System.Web.UI.Page
    {
    string adm_no = "";
    List<field> data = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            data = new List<field>();
        }
       protected void btnprint_Click(object sender, EventArgs e)
         {
           printall();
           string adm_no= ViewState["_data"].ToString();
           int class_id= ViewState["_data"].ToString();
           Response.Redirect("examreportcard.aspx?adm_no=" + adm_no +  "&class_id=" + class_id + "");

        }

         public void printall()
          {
           foreach (GridViewRow g1 in gvstudentDetails.Rows)
            {
               field f1 = new field();

               f1.adm_no=Convert.ToString((g1.FindControl("lbladm_no")  as Label).Text);

               f1.Class_Id=Convert.ToInt32(ddlclass.SelectedValue);
           }
             data.Add(f1);
             ViewState["_data"] = data;

        }


    }


‹ Previous Thread|Next Thread ›
Posted
Updated 3-Feb-16 21:49pm
v2
Comments
Kornfeld Eliyahu Peter 4-Feb-16 4:03am    
1. Adding data to the viewstate is not a good idea, unless you need to preserve that data via postbacks (and can't do it on the server). The size of the viewstate means more network to both direction...
2. In any case viewstate not preserved between pages, so can't use to pass data between
3. It would be nice to share with us, what are you try to achieve and someone may come up with a better suggestion...
F-ES Sitecore 4-Feb-16 4:28am    
Use the Session instead of ViewState.
jitendra raj 4-Feb-16 4:31am    
how we do that

1 solution

View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique.

View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back. For example, you can store information in view state that will be accessed during the page load event the next time the page is sent to the server.

Step1: Declare a class with Serializable attribute
C#
[Serializable]
 public class field
 {
	public int class_Id { get; set; }
	public string adm_no { get; set; }
 }

Step2: Store data in Viewstate
C#
List<field> data = new List<field>();
// Storing it in Viewstate
foreach (GridViewRow g1 in gvstudentDetails.Rows)
{
	field f1 = new field();

	f1.adm_no=Convert.ToString((g1.FindControl("lbladm_no")  as Label).Text);

	f1.Class_Id=Convert.ToInt32(ddlclass.SelectedValue);
}

data.Add(f1);
ViewState["_data"] = data;

Step3: Retrieve data from viewstate
C#
// get value from viewstate
if (ViewState["_data"] != null)
{
  List<field> fieldList = (List<field>)ViewState["_data"];
  
  // Implement your logic
}

Note: You are storing list object in Viewstate, it may create performance overhead of the page. Secondly in your code it is redirect to some other page, means you won't the Viewstate value in other page.
 
Share this answer
 
v5
Comments
jitendra raj 5-Feb-16 4:33am    
Thanks Manas ji for reply but i want to retrive value from list one by one means if we have a gridview which have 30 record per column and we store 30 students admission no. now i want to store these 30 admission no in view state from griedview and read these admission no. one by one using loop or what ever
[no name] 5-Feb-16 4:46am    
You are storing value in below code:

foreach (GridViewRow g1 in gvstudentDetails.Rows)
{
field f1 = new field();

f1.adm_no=Convert.ToString((g1.FindControl("lbladm_no") as Label).Text);

f1.Class_Id=Convert.ToInt32(ddlclass.SelectedValue);
}

data.Add(f1);

So where is the problem?
jitendra raj 5-Feb-16 6:40am    
for read value from viewstate one by one not all values like as below
if (ViewState["_data"] != null)
{
List<field> fieldList = (List<field>)ViewState["_data"];

// Implement your logic
}

I want to read adm_no one by one student from viewstate not all students adm_no at time.
[no name] 7-Feb-16 4:17am    
@downvoter, I am not getting why this answer gets 1 vote. Can you please clarify it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900