将DataSet重新绑定新的数据源,但是翻页后没有显示新数据源的数据,仍然显示的是老数据源的数据,这应该如何处理?
解决方案 »
- 我在Datalist中用OnItemCommand事件实现了点击linkbutton后下载datalist中文件的功能,但是由于OnItemCommand得存在
- SWFupload组件传递参数
- drpdownlist获得主键的问题
- 正则替换问题
- web页面禁止用户刷分,或刷点击率??
- 大家现在用javascript还是Jscript,vbscript?
- |M| 第二贴:服务器被攻击 请大家帮忙要怎么防 谢谢
- 探讨:动态操作iframe的方法,大家来看
- 框架刷新有何好一点的方法?
- 超简单,帮我看看这段语句应该怎么写?
- 请教各位朋友,现在想宣传一个网站,有没有一些低成本的方案呢?
- 高手请进-->如何把多个表的内容放如一个dataset中
绑定了是否在翻页事件中设了翻页属性没有
{
DGResult.CurrentPageIndex = e.NewPageIndex;
}
private void dgrdCustomerInfo_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgrdCustomerInfo.CurrentPageIndex = e.NewPageIndex; BindDataGrid();//DataSet重新绑定也要写在这个方法里
}
CS代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace WebNoteBook
{
/// <summary>
/// admin_msg_Test 的摘要说明。
/// </summary>
public class admin_msg_Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btdel;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.btdel.Attributes.Add("onclick", "return confirm('您真的要删除所选项吗?');");
if(!this.IsPostBack)
{
this.BindNoteWordDataGrid();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.btdel.Click += new System.EventHandler(this.btdel_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
private void BindNoteWordDataGrid()
{
SqlConnection con=DB.createCon();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from NoteBook",con);
DataSet ds=new DataSet();
sda.Fill(ds,"NB");
this.DataGrid1.DataSource=ds.Tables["NB"];
this.DataGrid1.DataBind();
this.DataGrid1.Columns[3].Visible=false;
foreach(DataGridItem dgi in this.DataGrid1.Items)
{
//以下绑定非编辑状态TextBox
TextBox tbv=(TextBox)dgi.FindControl("tbview");
if(tbv!=null)
{
tbv.Text=ds.Tables["NB"].Rows[dgi.ItemIndex]["NoteWord"].ToString();
}
//以下绑定编辑状态TextBox
TextBox tbe=(TextBox)dgi.FindControl("tbedit");
if(tbe!=null)
{
tbe.Text=ds.Tables["NB"].Rows[dgi.ItemIndex]["NoteWord"].ToString();
}
}
con.Close();
} private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
this.BindNoteWordDataGrid();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
this.BindNoteWordDataGrid();
} private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
this.BindNoteWordDataGrid();
} //在编辑的时候对TextBox的宽度进行修改
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
TextBox t;
t=(TextBox)e.Item.Cells[2].Controls[0];
t.Width=120;
// t=(TextBox)e.Item.Cells[4].Controls[0];
// t.Width=450;
t=(TextBox)e.Item.Cells[5].Controls[0];
t.Width=70;
// for (int i=2;i<5;i++)
// {
// if(e.Item.Cells[i].Controls.Count>0)
// {
// TextBox t =(TextBox)e.Item.Cells[i].Controls[0];
// switch(i)
// {
// case 2:
// t.Width=120;
// break;
// case 4:
// t.Width=450;
// break;
// case 5:
// t.Width=70;
// break;
// }
// }
// }
}
} private void btdel_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in this.DataGrid1.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbselect");
if(cb.Checked)
{
//以下执行删除操作
string strid = Convert.ToString(dgi.Cells[0].Text);
string strSql = "delete from NoteBook where ID='"+strid+"'";
DB.ExecuteSql(strSql);
}
}
this.DataGrid1.CurrentPageIndex = 0;
this.BindNoteWordDataGrid();
}
public void CheckedAllmsg(object sender, System.EventArgs e)
{
CheckBox cball = (CheckBox)sender;
if(cball.Text=="全选")
{
foreach(DataGridItem dgi in this.DataGrid1.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbselect");
cb.Checked = cball.Checked;
}
}
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strid=e.Item.Cells[0].Text;//处于非编辑状态
string strtitle = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;//处于编辑状态
string strword = ((TextBox)e.Item.Cells[4].FindControl("tbedit")).Text;
string strdatatime = ((TextBox)(e.Item.Cells[5].Controls[0])).Text;
string strsql = "update NoteBook set NoteTitle='"+strtitle+"',NoteWord='"+strword+"',NoteTime='"+strdatatime+"' where ID='"+strid+"'";
DB.ExecuteSql(strsql);
this.DataGrid1.EditItemIndex = -1;
this.BindNoteWordDataGrid();
}
}
}