1

我有一个与 .net 中的 DataTable 相关的问题。我正在尝试在 page_load 上填充数据表。那部分没问题,数据表正确填充了数据。但我注意到当我点击页面上的按钮时......它再次尝试填充数据表,这只是一项耗时的任务......我希望数据表应该只填充一次,当网站在浏览器中打开时......不是每次点击...因为我在表中有大量数据,所以加载数据表需要时间..请帮帮我...这是我的代码:

protectd void Page_Load(object sender, EventArgs e)
{
        cnn.ConnectionString= ConfigurationManager.ConnectionStrings["con"].ConnectionString;

      //  if (this.IsPostBack == true)
        {
            dr1 = TableUtoP(); 
            dtWordsList.Load(dr1);
       }
}

OleDbDataReader TableUtoPunj(String ArrWord)
    {
        if (cnn.State == ConnectionState.Open)
        {
            cnn.Close();
        }
        cnn.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "SELECT U, P from UtoP";
        cmd.Connection = cnn;
        OleDbDataReader dr = cmd.ExecuteReader();
        return dr;
    }
4

2 回答 2

3

您需要检查页面是否正在回发,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    cnn.ConnectionString= ConfigurationManager.ConnectionStrings["con"].ConnectionString;

    if(!IsPostBack)
    {
        // This will only happen when the page is first loaded, as the first time is not considered a post back, but all others are
        dr1 = TableUtoP(); 
        dtWordsList.Load(dr1);
    }
}
于 2013-08-09T20:42:34.977 回答
0

仅在不是 PostBack 时才填充数据表...

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //TO DO: Make the call to fill the data table here
   }
}
于 2013-08-09T20:43:59.613 回答