如何将会话绑定到Girdview [英] How to Bind Session to Girdview

查看:91
本文介绍了如何将会话绑定到Girdview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在接受来自用户的数据并且不将其保存在数据库中的任务我显示网格中的数据

当用户点击txtbx中的btnGo数据时显示在gridview中的页面中..

当用户使用txtbx第二次提交数据时,生成新行,并在网格中显示其旧数据,我这样做:



I'm doing a task of accepting data from user and without saving it in database i'm showing that data in grid
when user clicks btnGo data in txtbx is shown in gridview present in page..
when user submits data second time using txtbx new row is generated and shown in grid with its old data for this i'm doing this:

public partial class Store_Table_in_List : System.Web.UI.Page
{
    
    static int count;

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["list"] = "";
            count = 0;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        count++;
        List<a> list1 = new List<a>();
        a class1 = new a();
        class1.No = count.ToString();
        class1.Fname = txtFname.Text;
        class1.Lname = txtLname.Text;

        list1.Add(class1);
        Session["list1"] += list1.ToString();
        GridView1.DataSource =  (DataTable)Session["list1"];///throws exception: Unable to cast   //object of type 'System.String' to type 'System.Data.DataTable'.
        GridView1.DataBind();
        txtFname.Text="";
        txtLname.Text="";
    }

}
class a
{
    public string No { get; set; }
    public string Fname { get; set; }
    public string Lname { get; set; }
}



问题是:如何使用session作为我的数据源...?



我知道这可以通过使用DataTable的对象来解决,但如果我不想使用DataTable的对象怎么办??

有没有办法在没有DataTable的情况下这样做??



在此先感谢.... :)


the problem is: how can i use session as my datasource...?

I know this can be solved by using DataTable's object but what if I dont want to use DataTable's Object....?
is there any way of doing this without DataTable...?

Thanks in advance.... :)

推荐答案

prashantttt写道:
prashantttt wrote:

我如何使用session作为我的数据源

how can i use session as my datasource

这是非常糟糕的主意。会话不适用于此类内容。不推荐在Session中存储大值。你必须在某种程度上改进你的逻辑/设计。



问候..

This is very bad idea. Session is not meant for such stuff. Storing large values inside Session is not recomended. You must refine your logic/design to some extent.

Regards..




请修改按钮点击代码,如下所示:
Hi,
Please modify your Button click Code like below:
protected void Button1_Click(object sender, EventArgs e)
   {
       count++;
       List<a> list1 = new List<a>();
       a class1 = new a();
       class1.No = count.ToString();
       class1.Fname = txtFname.Text;
       class1.Lname = txtLname.Text;

       list1.Add(class1);

       List<a> tempList = new List<a>();
       tempList = (List<a>)Session["list1"];
       list1.AddRange(tempList);

       Session["list1"] = list1.ToString();
       GridView1.DataSource = (DataTable)Session["list1"];
       GridView1.DataBind();
       txtFname.Text = "";
       txtLname.Text = "";
   }

我使用新的临时列表存储会话值,然后将其添加到实际列表

I used a new temp List to store value of session and then add it to actual List


public partial class Store_Table_in_List : System.Web.UI.Page
{

    static int count;

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["list"] = "";
            count = 0;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        count++;
        List<a> list1 = new List<a>();
        a class1 = new a();
        class1.No = count.ToString();
        class1.Fname = txtFname.Text;
        class1.Lname = txtLname.Text;

        list1.Add(class1);
        Session["list1"] += list1.ToString();
        GridView1.DataSource =  Session["list1"];
        GridView1.DataBind();
        txtFname.Text="";
        txtLname.Text="";
    }

}
[Serilizable]
class a
{
    public string No { get; set; }
    public string Fname { get; set; }
    public string Lname { get; set; }
}


这篇关于如何将会话绑定到Girdview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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