如何从TextBox中提取数据到GridView(要在GridView中显示多行)?

时间:2021-11-08 01:36:06

I have used the following code to display data from a TextBox to a GridView without saving the data to the database(Two TextBoxes and a Button). When I enter the Name and City in the TextBoxes, and click on the Button, those values will be displayed in the Gridview. It is working as expected without any errors. But I want to tweak the code a bit so that the GridView should be able to add new data from the Textbox by retaining the old data as it is in the Gridview (multiple rows should be displayed in the Gridview instead of single rows).

我使用以下代码将数据从TextBox显示到GridView,而不将数据保存到数据库(两个TextBoxes和一个Button)。当我在TextBoxes中输入Name和City,然后单击Button时,这些值将显示在Gridview中。它按预期工作,没有任何错误。但是我想稍微调整一下代码,以便GridView能够通过保留Gridview中的旧数据来从Textbox添加新数据(多行应该显示在Gridview中而不是单行中)。

It is a web-based ASP.NET application using C# coding (Visual Studio 2010).

它是一个使用C#编码的基于Web的ASP.NET应用程序(Visual Studio 2010)。

Can you make the necessary changes to the code given below so as to implement the above functionality?

您是否可以对下面给出的代码进行必要的更改以实现上述功能?

public partial class _Default : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
    {

    }

protected void btnTextDisplay_Click(object sender, EventArgs e)
    {
            DataColumn dc1 = new DataColumn("Name");
            DataColumn dc2 = new DataColumn("City");
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);

            DataRow dr = dt.NewRow();
            dr[0] = txtName.Text;
            dr[1] = txtCity.Text;
            dt.Rows.Add(dr);
            gvDisplay.DataSource = dt;
            gvDisplay.DataBind();
    }
}

1 个解决方案

#1


2  

You have to persists your data between postbacks, you have many options here: by Session, ViewState, Cache and some other.

您必须在回发之间保留数据,这里有很多选项:Session,ViewState,Cache和其他一些选项。

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostback)
    {
         dt = Session["data_table"] as DataTable;
    }
}

protected void btnTextDisplay_Click(object sender, EventArgs e)
{
     if (dt == null)
     {
          dt = new DataTable();
          DataColumn dc1 = new DataColumn("Name");
          DataColumn dc2 = new DataColumn("City");
          dt.Columns.Add(dc1);
          dt.Columns.Add(dc2);   
     }

     DataRow dr = dt.NewRow();
     dr[0] = txtName.Text;
     dr[1] = txtCity.Text;
     dt.Rows.Add(dr);
     gvDisplay.DataSource = dt;
     gvDisplay.DataBind();

     Session["data_table"] = dt;
}

#1


2  

You have to persists your data between postbacks, you have many options here: by Session, ViewState, Cache and some other.

您必须在回发之间保留数据,这里有很多选项:Session,ViewState,Cache和其他一些选项。

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostback)
    {
         dt = Session["data_table"] as DataTable;
    }
}

protected void btnTextDisplay_Click(object sender, EventArgs e)
{
     if (dt == null)
     {
          dt = new DataTable();
          DataColumn dc1 = new DataColumn("Name");
          DataColumn dc2 = new DataColumn("City");
          dt.Columns.Add(dc1);
          dt.Columns.Add(dc2);   
     }

     DataRow dr = dt.NewRow();
     dr[0] = txtName.Text;
     dr[1] = txtCity.Text;
     dt.Rows.Add(dr);
     gvDisplay.DataSource = dt;
     gvDisplay.DataBind();

     Session["data_table"] = dt;
}