代码如下:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
<!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>我的Coolite测试</title>
</head>
<body>
    <form id="form1" runat="server">
    <br />
        <ext:ScriptManager ID="ScriptManager1" runat="server" />
        <ext:Store ID="Store1" runat="server">
            <Reader>
                <ext:JsonReader>
                    <Fields>
                        <ext:RecordField Name="id" />
                        <ext:RecordField Name="a" />
                        <ext:RecordField Name="b" />
                        <ext:RecordField Name="c" />
                    </Fields>
                </ext:JsonReader>
            </Reader>
        </ext:Store>
        <ext:GridPanel ID="GridPanel1" runat="server" AutoExpandColumn="c" Title="用户管理" AutoHeight="true"
            Width="500px" StoreID="Store1">
            <TopBar>
                <ext:Toolbar ID="Toolbar1" runat="server">
                    <Items>
                        <ext:ToolbarFill />
                        <ext:Button ID="UserAdd" runat="server" Text="添加User">
                            <Listeners>
                                <Click Handler="#{Window2}.show();" />
                            </Listeners>
                        </ext:Button>
                        <ext:ToolbarSeparator />
                        <ext:TextField ID="Key" runat="server" EmptyText="请输入关键字">
                        </ext:TextField>
                        <ext:Button ID="Modify" Icon="Find" runat="server" Text="搜">
                            <AjaxEvents>
                                <Click OnEvent="FindUser">
                                    <EventMask ShowMask="true" Msg="正在加载…" MinDelay="500" />
                                </Click>
                            </AjaxEvents>
                        </ext:Button>
                    </Items>
                </ext:Toolbar>
            </TopBar>
            <ColumnModel>
                <Columns>
                    <ext:Column ColumnID="ColumnID" Header="ID" DataIndex="id" Width="100px">
                    </ext:Column>
                    <ext:Column ColumnID="Columna" Header="列A" DataIndex="a" Width="100px">
                    </ext:Column>
                    <ext:Column ColumnID="Columnb" Header="列B" DataIndex="b" Width="100px">
                    </ext:Column>
                    <ext:Column ColumnID="Columnc" Header="列C" DataIndex="c">
                    </ext:Column>
                </Columns>
            </ColumnModel>
            <BottomBar>
                <ext:PagingToolbar ID="PageToolBar" runat="server" PageSize="50" StoreID="Store1" DisplayInfo="false" ></ext:PagingToolbar>
            </BottomBar>
        </ext:GridPanel>
        <ext:Window runat="server" ID="Window2" AutoShow="false" Hidden="true" Title="新建用户" >
        </ext:Window>
    </form>
</body>
</html>
后台:
using System;
using System.Data;
using Coolite.Ext.Web;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
         this.Store1.DataSource = GetData();
         this.Store1.DataBind();
                //this.Store1.DataSource = GetData00000000();
        //this.Store1.DataBind();
    }    private DataTable GetData()
    {
        string Constr = "Data Source=.;Initial Catalog=user;Integrated Security=True";
        SqlConnection sc = new SqlConnection(Constr);
        SqlDataAdapter sda = new SqlDataAdapter("select * from users;", sc);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        return dt;
    }
    private DataTable GetData00000000()
    {
        string Constr = "Data Source=.;Initial Catalog=user;Integrated Security=True";
        SqlConnection sc = new SqlConnection(Constr);
        SqlDataAdapter sda = new SqlDataAdapter("select top 2 * from users;", sc);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        return dt;
    }    public void AddUser(object sender, EventArgs e)
    {
    }
    public void FindUser(object sender, EventArgs e)
    {
        string KeyWord = this.Key.Text;
        if (KeyWord.Equals(""))
        {
            Ext.Msg.Show(new MessageBox.Config()
            {
                Title = "提示",
                Message = "请输入您要搜索的关键字!",
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.INFO
            });
        }
        else
        {
            string Constr = "Data Source=.;Initial Catalog=user;Integrated Security=True";
            SqlConnection sc = new SqlConnection(Constr);
            SqlCommand scm = new SqlCommand("select * from users where a like @Key or b like @Key or c like @Key;", sc);
            SqlParameter sp = new SqlParameter("@Key", "%" + KeyWord + "%");
            scm.Parameters.Add(sp);
            SqlDataAdapter sda = new SqlDataAdapter(scm);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            
            if (dt.Rows.Count > 0)
            {
                this.Store1.DataSource = dt;
                this.Store1.DataBind();
            }
            else
            {
                Ext.Msg.Show(new MessageBox.Config()
                {
                    Title = "提示",
                    Message = "没有找到相应的数据!",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO
                });
            }
        }        //string Key = this.Key.Text;        //string Constr = "Data Source=.;Initial Catalog=user;Integrated Security=True";
        //SqlConnection sc = new SqlConnection(Constr);
        //string sql = "select * from users where a like '%" + Key + "%' or b like '%" + Key + "%' or c like '%" + Key + "%';";
        //SqlCommand scm = new SqlCommand(sql, sc);
        //SqlParameter sp = new SqlParameter("Key", Key);
        //scm.Parameters.Add(sp);        //SqlDataAdapter sda = new SqlDataAdapter(scm);
        //DataTable dt = new DataTable();
        //sda.Fill(dt);        //this.Store1.DataSource = dt;
        //this.Store1.DataBind();
    }
}
数据库有6条数据。如下
id a b c
1 地方官 4 三等功
2 人第三 4 士大夫
3 如法 2 5
4 的鬼地 3 地方官
6 是 2 无
7 士大夫 4 阿达然后关键的出来了:在page_load里执行两次绑定的话(取消对于第二次绑定的注释),store里的数据是后一次绑定的数据也就是GetData00000000的数据(有两条)。
现在注释掉page_load里的第二次绑定,
在前台使用搜索功能,此时先在page_load里绑定数据(6条),然后在FindUser()里执行搜索后的绑定(在前台输入3),你猜最终的绑定的数据有几条(是1条么?);但实际上是6条,前台显示6条。这是为什么呢?
我实在不知道怎么办了,我的经理在对page_load加上isposback后居然正常了。他解释说是page_load晚于按钮事件执行,
服了他了。大家知道是什么原因么?

解决方案 »

  1.   

    分析问题(个人推测,请指正):
            由于没有用IsPostBack判断是否是第一次加载此页面,所以不管什么情况下只要有PostBack,都会对sda.Fill(dt); 进行数据绑定。所以在进行任何的sumbit后,dg都会去数据库绑定数据而不理会页面中的数据。
             当对选中的数据进行修改完毕后,在点“更新”时,提交此页面修改的数据,而马上遇到Page_Load事件,不等对修改数据进行处理,服务器就先生成原页面(MS用这种方法提高速度?),发现Store1.DataBind(),执行之,于是放弃数据库更新,于是看不到更新结果了你要打开界面绑定数据一定要 
       
                  if (!IsPostBack)
                {....}而且你这个是coolite的!没有ext那么复杂!