首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

datalist 数据源绑定有关问题

2012-01-02 
datalist 数据源绑定问题.我用adapter+DataSet填充数据源绑定datalist,进行添加删除修改后绑定datalist,但

datalist 数据源绑定问题.
我用adapter+DataSet填充数据源绑定datalist,进行添加删除修改后绑定datalist,但是操作进行后需要刷新或者再执行一次操作才能看到上一次的操作效果,而用DataReader直接就可以看到执行效果,不知道是为什么.代码如下:
adapter+dataSet绑定:
                ICollection   CreateSource()
                {
                        int   StartIndex;
                        //设定倒入的起终地址
                        StartIndex   =   CurrentPage   *   PageSize;
                        string   strSel   =   "select   *   from   Message   order   by   id   desc ";
                        DataSet   ds   =   new   DataSet();
                        OleDbDataAdapter   da   =   new   OleDbDataAdapter(strSel,con);
                        da.Fill(ds,   StartIndex,   PageSize,   "Message ");
                        return   ds.Tables[0].DefaultView;

                }
public   void   ListBind()
                {
                        this.DataList1.DataSource   =   CreateSource();
                        this.DataList1.DataBind();

}
dataReader绑定:
                                        cmd.CommandText   =   "select   *   from   Message   order   by   id   desc ";
                                        OleDbDataReader   sdr   =   cmd.ExecuteReader();
                                        this.DataList1.DataSource   =   sdr;
                                        this.DataList1.DataBind();


[解决办法]
我用adapter+DataSet填充数据源绑定datalist,进行添加删除修改后绑定datalist,但是操作进行后需要刷新或者再执行一次操作才能看到上一次的操作效果,而用DataReader直接就可以看到执行效果,不知道是为什么.
----------------------
看不到你执行代码,所以不知道有没有从新绑定
[解决办法]
protected void SetBind()
{
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "Conn "]);
SqlDataAdapter da=new SqlDataAdapter( "select * from upimg ",con);
DataSet ds=new DataSet();
da.Fill(ds, "table1 ");
for(int i=0;i <ds.Tables[ "table1 "].Rows.Count;i++)
{
ds.Tables[ "table1 "].Rows[i][ "type "] = se.dtcase(ds.Tables[ "table1 "].Rows[i][ "type "].ToString());


ds.Tables[ "table1 "].Rows[i][ "imgtype "] = se.tycase(ds.Tables[ "table1 "].Rows[i][ "imgtype "].ToString());
}
this.DataGrid1.DataSource=ds.Tables[ "table1 "];
this.DataGrid1.DataBind();
}


private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName== "del ")
{
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "Conn "]);
SqlCommand comm=new SqlCommand( "delete from upimg where [id]=@id ",con);
SqlParameter parm1=new SqlParameter( "@id ",SqlDbType.Int);
parm1.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
comm.Parameters.Add(parm1);
con.Open();
comm.ExecuteNonQuery();
con.Close();
SetBind();
}
}
试试,我这里测试无问题

热点排行