Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据表正在被覆盖_C#_Asp.net_Datatable - Fatal编程技术网

C# 数据表正在被覆盖

C# 数据表正在被覆盖,c#,asp.net,datatable,C#,Asp.net,Datatable,在ASP.NET项目中,我有两个文本框和一个提交按钮 在button clicked的事件处理程序中,我希望将文本框的值保存到datatable,然后将datatable绑定到Gridview 这必须发生多次。但是每次datatable有一行时,就像每次事件处理程序触发时被覆盖一样。这就像每次事件处理程序启动时都从一开始创建datatable一样。代码如下。谢谢你抽出时间 编辑:谢谢你给出的所有答案 protected void BtnInsertCustomerLegalRelat

在ASP.NET项目中,我有两个文本框和一个提交按钮

在button clicked的事件处理程序中,我希望将文本框的值保存到datatable,然后将datatable绑定到Gridview

这必须发生多次。但是每次datatable有一行时,就像每次事件处理程序触发时被覆盖一样。这就像每次事件处理程序启动时都从一开始创建datatable一样。代码如下。谢谢你抽出时间

编辑:谢谢你给出的所有答案

      protected void BtnInsertCustomerLegalRelationsInsert_Click(object sender, EventArgs e)
      {
        string FullCompanyName = TbxFullCompanyName.Text.Trim();
        object LegalRelationAfm = TbxLegalRelationAfm.Text.Trim();
        if (dtCustomersLegalRelations.Columns.Count == 0)
        {
            dtCustomersLegalRelations.Columns.Add("FullCompanyName",typeof(string));
            dtCustomersLegalRelations.Columns.Add("LegalRelationAfm",typeof(string));
        }
        DataRow dr = dtCustomersLegalRelations.NewRow();
        dr["FullCompanyName"] = FullCompanyName;
        dr["LegalRelationAfm"] = LegalRelationAfm;
        dtCustomersLegalRelations.Rows.Add(dr);            
        GvCustomerRegalRelations.DataSource = dtCustomersLegalRelations;
        GvCustomerRegalRelations.DataBind();
       }
这里的全部代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class ConnectedBorrowers_CustomerBasicInput : System.Web.UI.UserControl
{
    DataTable dtCustomersLegalRelations = new DataTable();   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {            
            TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = false;
        }
    }

    protected void CbMandatoryAFM_CheckedChanged(object sender, EventArgs e)
    {
        if (CbMandatoryAFM.Checked == true)
        {
            TbxCustAfm.ReadOnly = true;
            TbxCustAfm.BackColor = Color.LightGray;
        }
        else
        {
            TbxCustAfm.ReadOnly = false;
            TbxCustAfm.BackColor = Color.White;            
        }
    }

    protected void CbLegalRelationsMandatoryAFM_CheckedChanged(object sender, EventArgs e)
    {
        if (CbLegalRelationsMandatoryAFM.Checked == true)
        {
            TbxLegalRelationAfm.ReadOnly = true;
            TbxLegalRelationAfm.BackColor = Color.LightGray;
        }
        else
        {
            TbxLegalRelationAfm.ReadOnly = false;
            TbxLegalRelationAfm.BackColor = Color.White;
        }
    }

    protected void CbLegalRelations_CheckedChanged(object sender, EventArgs e)
    {
        if (CbLegalRelations.Checked == true)
        {
            TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = true;            
        }
        else
        {
            TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = false; 
        }
    }

    protected void BtnInsertCustomerLegalRelationsInsert_Click(object sender, EventArgs e)
    {
        try
        {
            string FullCompanyName = TbxFullCompanyName.Text.Trim();
            object LegalRelationAfm = TbxLegalRelationAfm.Text.Trim();
            if (dtCustomersLegalRelations.Columns.Count == 0)
            {
                dtCustomersLegalRelations.Columns.Add("FullCompanyName",typeof(string));
                dtCustomersLegalRelations.Columns.Add("LegalRelationAfm",typeof(string));
            }
            DataRow dr = dtCustomersLegalRelations.NewRow();
            dr["FullCompanyName"] = FullCompanyName;
            dr["LegalRelationAfm"] = LegalRelationAfm;
            dtCustomersLegalRelations.Rows.Add(dr);            
            GvCustomerRegalRelations.DataSource = dtCustomersLegalRelations;
            GvCustomerRegalRelations.DataBind();
        }
        catch (Exception ex)
        {
            ((Label)this.Page.Master.FindControl("LblError")).Text = ex.Message;
        }
    }

在什么时候创建数据表(DTCustomerLegalRelations)?你能粘贴完整的代码,包括数据表的标签和按钮吗?

试试这种方法:

public partial class ConnectedBorrowers_CustomerBasicInput : System.Web.UI.UserControl
{
    DataTable dtCustomersLegalRelations;   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {            
            dtCustomersLegalRelations = new DataTable(); 
            Session["table"] = dtCustomersLegalRelations;  
            TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = false;
        } else {
             dtCustomersLegalRelations = Session["table"] as DataTable;
        }
    }

  ...

}

“DTCustomerLegalRelations”的值不会在回发事件中保持不变-因此,当您在其末尾添加行时,您是在新实例上这样做的

在添加新行并重新绑定它之前,需要从GridView中提取所有数据


st4hoo上面的答案应该为您排序。

当您单击按钮时,会出现回发。在每次回发期间,都会从头创建数据表。所以你会丢失你的旧数据。
因此,一个选项是,您可以在会话中保留包含数据的数据表,并在“下一步”按钮单击并添加新行期间从会话中检索数据表。

显示您创建
dtCustomerLegalRelations
的代码部分。检查我编辑的问题。当然可以,但您在哪里创建它?谢谢您的回答。