如何在viewstate中存储一个列值并逐个检索 [英] How to store one column values in viewstate and retrieve one by one

查看:52
本文介绍了如何在viewstate中存储一个列值并逐个检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,我是基于网络的ERP应用程序的jitendra kumar



我想在viewstate中存储列的多个值,并在字符串中逐个检索类型变量从一个页面发送到另一个页面。



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.

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 ›

推荐答案

视图状态是在往返之间保留页面和控件的值的方法。它是一种页面级状态管理技术。



默认情况下,视图状态处于打开状态,通常会对页面上每个控件中的数据进行序列化,而不管它是否在回发期间实际使用。例如,您可以将信息存储在视图状态中,该信息将在下次将页面发送到服务器时在页面加载事件期间访问。



Step1 :声明具有Seri​​alizable属性的类

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
[Serializable]
 public class field
 {
	public int class_Id { get; set; }
	public string adm_no { get; set; }
 }



Step2 :将数据存储在Viewstate中


Step2: Store data in Viewstate

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 :从viewstate检索数据


Step3: Retrieve data from viewstate

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



注意:您在Viewstate中存储列表对象,它可能会产生页面的性能开销。其次在你的代码中它被重定向到其他页面,意味着你不会在其他页面中看到Viewstate值。


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.


这篇关于如何在viewstate中存储一个列值并逐个检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆