本文介绍了如何将按钮单击事件中的记录添加到数据表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想多次将记录添加到数据表中一键单击事件而不将数据表声明为静态.

例如我想在用户按下10时间按钮时将10条记录添加到数据表中


我正在使用此代码,但无法在每个Click事件数据表中清除

I want to Add record into Data Table in multiple time One Button Click Event Without Declare DataTable as Static.

e.g. I want add 10 record into Data Table when User press 10 Time Button


I am using this code but not working every Click event Data Table Clear

DataTable dt_dr = new DataTable("Test");

dt_dr.Columns.Add(LedgerId, typeof(int));
dt_dr.Columns.Add(LedgerName, typeof(string));


DataRow dt_row;
dt_row = dt_dr.NewRow();
dt_row[LedgerId] = TextBox1.Text;
dt_row[LedgerName] = TextBox2.Text;
dt_dr.Rows.Add(dt_row);

推荐答案


protected void Button1_Click(object sender, EventArgs e)
 {
     try
     {
         //Check if previous session is exist
         if (Session["MyTable"] == null)
         {
             dtMyTable = new DataTable("MyTable");
             dtMyTable.Columns.Add("Id", typeof(int));
             dtMyTable.Columns.Add("LName", typeof(string));

         }
         else
         {
             //If yes then get it from current session
             dtMyTable = (DataTable)Session["MyTable"];
         }

         //Add new row every time
         DataRow dt_row;
         dt_row = dtMyTable.NewRow();
         dt_row["Id"] = TextBox1.Text;
         dt_row["LName"] = TextBox2.Text;
         dtMyTable.Rows.Add(dt_row);

         //Update session table
         Session["MyTable"] = dtMyTable;
     }
     catch (Exception ex)
     {

         Response.Write(ex.Message);
     }
 }


这篇关于如何将按钮单击事件中的记录添加到数据表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 13:39