大家都来看看,分页和全选怎样才能兼得?(在线等待,做完这个小弟才能回家过年,小弟的结贴率几乎是100%)
//前台叶面定义了一个ListDataGrid和btnSelectAll,btnDel,其中在ListDataGrid的模版列中定义了一个CheckBox
//这是后置代码
private void Page_Load(object sender, System.EventArgs e)
{
if( !Page.IsPostBack )
{
ListSql = "SELECT * FROM tzgg ORDER BY id DESC";
if ( conn.GetRowCount( ListSql ) > 0 )
{
OleDbConnection myConnection = new OleDbConnection("连接字符串");
myConnection.Open();
OleDbDataAdapter myAdapter = new OleDbDataAdapter( ListSql, myConnection );
DataSet ds = new DataSet();
myAdapter.Fill( ds );
DataView dv = new DataView( ds.Tables[0] );
ListDataGrid.DataSource = dv;
ListDataGrid.DataBind();
}
else
{
//没有资料
}
}
}
private void SelectAll_Click(object sender, System.EventArgs e)
{
for( int i = 0; i < ListDataGrid.Items.Count; i++ )
{
CheckBox MyCheckBox = ( CheckBox )ListDataGrid.Items[i].FindControl("CheckID");
if( MyCheckBox.Checked )
{
MyCheckBox.Checked = false;
SelectAll.Text = "全选";
}
else
{
MyCheckBox.Checked = true;
SelectAll.Text = "全不选";
}
}
}
这个时候btnSelectAll运行正常,但是分页不行,一点DataGrid的下一页,叶面上就没有Datagrid了。如果去掉Page_Load中的Page.IsPostBack则分页正常,但是btnSelectAll就只能点击一次,第2次就没有作用了。我认为此时是DataSet被刷新了,之前选定的的CheckBox的状态无法取得。(请指正)
希望大家给我一个解决的办法,我不想在叶面中用JS脚本全选和<%# DataBinder.Eval(Container.DataItem, "id")%>绑定。