插入数据并及时显示问题

关于数据绑定并及时显示问题,这种问题老是被忘记掉,所以现在在这里把可能出现的一些原因写出来,分析一下,以便留着自己以后看,并且也可以供大家参考,遇到类似的情况的时候,引以为戒.

问题如下:

添加一条记录后,重新绑定,为什么还是要延迟一次显示

分析如下:

主要这个问题是由于未在插入(更新,删除)数据的时候及时进行绑定,这样的话,会导致绑定早于插入,这是我用断点来进行跟踪所得到的结论.

一定要记住插入,删除,修改的时候,要同时进行次绑定,这是防止出现这种错误的最好办法

下面是一个例子

数据库结构:

access数据库:

字段:

uid 自动编号

uname 文本

cs文件代码:

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Data.OleDb;

public   partial   class  Test : System.Web.UI.Page
{
    
static   int  i;
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  ( ! Page.IsPostBack)
        {
            i 
=   0 ;
           
        }
        
if (Page.IsPostBack)
        
// 设置断点的地方,在回发的时候执行断点
        Response.Write(i ++ ); 
        
/* 不管是否回发都执行此绑定,当然如果下面在插入和更新
        ,删除后都已经执行了绑定,那么这里是否有绑定都是可以的
*/
        dltBind();
        
    }
    
// 绑定控件
     protected   void  dltBind() 
    {
        OleDbConnection conn 
=   new  OleDbConnection();
        conn.ConnectionString 
=  ConfigurationSettings.AppSettings[ " conStr " ] + Server.MapPath( " app_Data/TestData.mdb " );
        OleDbCommand comm 
=  conn.CreateCommand();
        comm.CommandText 
=   " select * from TestTable " ;
        conn.Open();
        OleDbDataReader dr;
        dr
= comm.ExecuteReader();
        
// 有连接的DataList绑定
        dltTest.DataSource  =  dr;
        dltTest.DataBind();
        dr.Dispose();
        conn.Close();

    }
    
protected   void  btnInsert_Click( object  sender, EventArgs e)
    {
        
        InsertData(
this .txtName.Text);
        
// 这里是最关键的地方,出现无法进行及时更新,就是这里的问题
        
// 如果没有dltBind(),那么由于它的执行步骤是先pageLoad再进行插入的
        
// 这样的话就算你在pageLoad当中有了dltBind();这个动作,也是于事无补
        
// 绑定对它来说根本不起什么作用,这是由于你还未将数据插入数据库,也就是还未执行btnInsert的点击事件
        
// 步骤:
        
// Page_Load方法--->dltbind方法--->btnInsert_Click方法,这样的绑定就只会是比插入先执行,因此会出现如下的数据
        
// 数据库中数据个数 客户端界面显示的数据个数
        
//       1                   0
        
//       2                   1
        
//       3                   2           
        
//       4                   3
        
// 由此数据就可以看出在执行这种绑定的时候都是比数据库晚了一步骤,当这里有了dltBind()
        
// 这是对数据进行了一次提交,及时的对数据进行了一次绑定,因此所得到的数据都是新的数据
        
// 由此要记住如果想要及时对数据进行更新显示,那么一定要在进行"删除,更新,插入"的时候同时对相应的控件进行一次绑定
        dltBind();
    }
    
// 插入数据
     protected   void  InsertData( string  name) 
    {
        OleDbConnection conn 
=   new  OleDbConnection();
        conn.ConnectionString 
=  ConfigurationSettings.AppSettings[ " conStr " +  Server.MapPath( " app_Data/TestData.mdb " );
        OleDbCommand comm 
=  conn.CreateCommand();
        comm.CommandText 
=   string .Format( " insert into TestTable (uname) values ('{0}') " ,name);
        
try
        {
            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();
        }
        
catch  (Exception err)
        {
            message.InnerHtml 
=   " <font color=red> "   +  err.Message  +   " </font> " ;
        }
        
finally
        {
            
if  (conn.State  ==  ConnectionState.Open)
            {
                conn.Close();
            }
        }
    }
    
// 删除数据库中的数据
     protected   void  btnDelete_Click( object  sender, EventArgs e)
    {
        OleDbConnection conn 
=   new  OleDbConnection();
        conn.ConnectionString 
=  ConfigurationSettings.AppSettings[ " conStr " +  Server.MapPath( " app_Data/TestData.mdb " );
        OleDbCommand comm 
=  conn.CreateCommand();
        comm.CommandText 
=   " delete from TestTable  " ;
        conn.Open();
        comm.ExecuteNonQuery();
        conn.Close();
        dltBind();
    }
}

 

aspx文件代码:

 

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Test.aspx.cs "  Inherits = " Test "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
    
< center >
        
& nbsp; < table style = " width: 637px; height: 222px " >
            
< tr >
                
< td >
                    名称:
< asp:TextBox ID = " txtName "  runat = " server " ></ asp:TextBox >
                    
< asp:Button ID = " btnInsert "  runat = " server "  OnClick = " btnInsert_Click "  Text = " 插入 "   />
                    
< asp:Button ID = " btnDelete "  runat = " server "  OnClick = " btnDelete_Click "  Text = " 删除数据库所有记录 "   />< span id = " message "  runat = " server " ></ span ></ td >
            
</ tr >
            
< tr >
                
< td >
       
< asp:DataList ID = " dltTest "  runat = " server " >
        
< ItemStyle BackColor = " AliceBlue "   />
        
< ItemTemplate >
            编号:
<% #DataBinder.Eval(Container.DataItem, " uid " %>< br  />
            名称:
            
<% #DataBinder.Eval(Container.DataItem, " uname " %>        
        
</ ItemTemplate >
        
</ asp:DataList >
                 
</ td >
            
</ tr >
        
</ table >
     
</ center >
     
</ div >
    
</ form >
</ body >
</ html >

这个问题感觉可能比较低级,但是很容易就犯了,写这个警告自己,不要犯相同的错误,也仅代表个人看法,如果有错误,还请见谅

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值