简体   繁体   English

没有后端数据库,只有动态创建的表绑定到GridView

[英]No backend database, only dynamically created table bound to GridView

I am need a solution like this: no backend database, no Xml, no Excel, only when user gives information display in GridData. 我需要这样的解决方案:没有后端数据库,没有Xml,没有Excel,仅当用户在GridData中提供信息显示时。 Only creating dynamically in the .aspx.cs table and column ... 仅在.aspx.cs表和列中动态创建...

Eg:-- 例如: -

Name |-Text box-| Button

Gridview 网格视图

When user visited page ..like above Name with TextBox and Submit Button . 当用户使用TextBox和Submit Button访问上述名称的页面时。

When user enters a name in the TextBox and clicks on submit I want to add that name to the grid, but here I want not only presented data... also previous enter data display in Gridview. 当用户在TextBox中输入名称并单击Submit时,我想将该名称添加到网格中,但是在这里,我不仅要显示数据...而且还要在Gridview中显示先前输入的数据。

eg2:- eg2:-

sairam  submit--->click
pamidi  submit-->click 

I want display how many values user enter that many i want to display in gridview ,like this 我想显示用户输入的多少个值,我想在gridview中显示多少个值,像这样

sairam
pamidi
some....
more...
values..

up-to when user perform close of browser . 当用户执行浏览器关闭时。

info.aspx : info.aspx

<form id="form1" runat="server">
    <div>
    <asp:Label ID="Label1" runat="server" Text="Enter Name"></asp:Label>
      <asp:TextBox  ID="TextBox1" runat="server"></asp:TextBox>
      <asp:Button ID="btnSumbit"  runat="server" Text="Submit" 
            onclick="btnSumbit_Click" />
      <asp:GridView ID="aspGrid1" runat="server" AutoGenerateColumns="false"  >

       <Columns>
       <asp:TemplateField>
       <ItemTemplate>
       <%#Eval("Name") %>
       </ItemTemplate>
       </asp:TemplateField>
       </Columns>
      </asp:GridView>
</div>
</form>

info.aspx.cs : info.aspx.cs

public partial class Default6 : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        dt.Columns.Add("Name", typeof(string));
        dt.Rows.Add(TextBox1.Text);
        ViewState["DataTable"] = dt;

    }
    protected void btnSumbit_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == " ")
        {
            return;
        }
        else if (TextBox1.Text != string.Empty)
        {
            aspGrid1.DataSource = (DataTable)ViewState["DataTable"];
            aspGrid1.DataBind();
        }
    }
}

You have to persist the data somehow. 您必须以某种方式保留数据。

The Standard options include a database, a variable stored in session state or in-page hidden fields. 标准选项包括数据库,存储在会话状态或页面内隐藏字段中的变量。 The .net control isn't going to databind to it's own viewstate . .net控件不会将数据绑定到它自己的viewstate

A non-standard option would be to handle everything client side without post backs. 一个非标准的选择是处理客户端的所有内容而无需回发。 If you go this route you will be better served but not using the standard .net controls but rather use straight html. 如果您采用这种方式,则可以更好地为您服务,但最好不要使用标准的.net控件,而应使用纯HTML。 This will require a fair bit of coding. 这将需要相当多的编码。

using System;
using System.Web.UI;
using System.Data;


public partial class Default6 : System.Web.UI.Page
{
    DataTable _dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            _dt = new DataTable();
            _dt = CreateDataTable();
            Session["DataTable"] = _dt;
            aspGrid1.DataSource = ((DataTable)Session["DataTable"]).DefaultView;
            aspGrid1.DataBind();
        }

    }
    protected void btnSumbit_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == " ")
        {
            return;
        }
        else if (TextBox1.Text != string.Empty)
        {

            AddDataTable(TextBox1.Text, (DataTable)Session["DataTable"]);
            aspGrid1.DataSource = ((DataTable)Session["DataTable"]).DefaultView;
            aspGrid1.DataBind();
        }

    }

    private DataTable CreateDataTable()
    {
        DataTable datatable=new DataTable();

        DataColumn datacolumn=new DataColumn();
        datacolumn.ColumnName = "Name";
        datacolumn.DataType = Type.GetType("System.String");
        datatable.Columns.Add(datacolumn);
        return datatable;
    }
    private void AddDataTable(string name,DataTable datatable)
    {

        DataRow dataRow;
        dataRow = datatable.NewRow();
        dataRow["Name"] = name;
        datatable.Rows.Add(dataRow);
    }
}

You're creating the data table everytime you enter Page_Load. 每次输入Page_Load时都在创建数据表。

You need to check if you're not already have a data table in the ViewState and if you do - pull it out and add a new row, or create the data table for the first time. 您需要检查ViewState中是否还没有数据表,如果有,请拉出数据表并添加新行,或者首次创建数据表。

The best way is to initialize the data table for the first time in the if(!PostBack){...} in Page_Load and store it in the Session (similary like ViewState, but reduces PostBack data from/to the client). 最好的方法是第一次在Page_Load的if(!PostBack){...}中初始化数据表并将其存储在Session中(类似于ViewState,但是减少了往返于客户端的PostBack数据)。

Then, if it's a postback, just get the data table from the session and add the new row to it. 然后,如果是回发,只需从会话中获取数据表并向其中添加新行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM