大家都来看看,分页和全选怎样才能兼得?(在线等待,做完这个小弟才能回家过年,小弟的结贴率几乎是100%)

gOODiDEA 2003-01-22 08:05:55
//前台叶面定义了一个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")%>绑定。
...全文
70 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
kinglht 2003-01-22
  • 打赏
  • 举报
回复
更正一下:
方案:
得到DataGrid.PageCount(pCount=DataGrid.PageCount),循环做pCount次,每次保存一页,然后翻页,如果到了最后一页则回到第一页,这样循环pCount次后还是回到当前页!!

回到当前页而不是第一页!!
kinglht 2003-01-22
  • 打赏
  • 举报
回复
我做过一各类似的,你可以参考一下:

方案:
得到DataGrid.PageCount(pCount=DataGrid.PageCount),循环做pCount次,每次保存一页,然后翻页,如果到了最后一页则回到第一页,这样循环pCount次后还是回到第一页!!
==================================================================
代码:
private void SelectAll()
{
customerDS = (CustomerDataset)Session["Hoten.Boss.CustInfoSys.Web.custinfo.serviceinfo.customerDS"];
int i=0;
int pCount=dgUserSP.PageCount;
while (i<pCount)
{
int j;
for (j = dgUserSP.CurrentPageIndex * dgUserSP.PageSize;
j< (dgUserSP.CurrentPageIndex+1) * dgUserSP.PageSize && j< customerDS.USERS.Count;
j++)
{

DataGridItem rowItem = dgUserSP.Items[j%dgUserSP.PageSize];
DataList dataList = (DataList)rowItem.FindControl("DataList1");
foreach (DataListItem colItem in dataList.Items)
{
if(colItem.ItemIndex==DropDownSP.SelectedIndex)
{
string spID=customerDS.SP[colItem.ItemIndex].SP_ID;
CheckBox check=(CheckBox)colItem.FindControl("CheckBox2");
if((!check.Checked))
{
check.Checked=true;
}
}

}
}
SaveData();

if(dgUserSP.CurrentPageIndex==pCount-1)
dgUserSP.CurrentPageIndex=0;
else
dgUserSP.CurrentPageIndex++;
BindGrid();
i++;
}
}

====================================================================
private void SaveData()
{
customerDS = (CustomerDataset)Session["Hoten.Boss.CustInfoSys.Web.custinfo.serviceinfo.customerDS"];
int i;
for (i = dgUserSP.CurrentPageIndex * dgUserSP.PageSize;
i < (dgUserSP.CurrentPageIndex+1) * dgUserSP.PageSize && i < customerDS.USERS.Count;
i++)
{
DataGridItem rowItem = dgUserSP.Items[i%dgUserSP.PageSize];
string userID = customerDS.USERS[i].USER_ID;
DataList dataList = (DataList)rowItem.FindControl("DataList1");
foreach (DataListItem colItem in dataList.Items)
{
string spID=customerDS.SP[colItem.ItemIndex].SP_ID;
CheckBox check=(CheckBox)colItem.FindControl("CheckBox2");
//
if((check.Checked) && (check.ForeColor==Color.Black))
{
customerDS.USER_SP.AddUSER_SPRow(userID, spID);
}
//
if((check.Checked==false) && (check.ForeColor==Color.Red))
{
customerDS.USER_SP.FindByUSER_IDSP_ID(userID, spID).Delete();
}
}
}
}

===================================================================
private void BindGrid()
{
//从Session中得到customerDS
customerDS = (CustomerDataset)Session["Hoten.Boss.CustInfoSys.Web.custinfo.serviceinfo.customerDS"];
dgUserSP.DataBind();
int i;
for (i = dgUserSP.CurrentPageIndex * dgUserSP.PageSize;
i < (dgUserSP.CurrentPageIndex+1) * dgUserSP.PageSize && i < customerDS.USERS.Count;
i++)
{
DataGridItem rowItem = dgUserSP.Items[i%dgUserSP.PageSize];
string userID = customerDS.USERS[i].USER_ID;
DataList dataList = (DataList)rowItem.FindControl("DataList1");
foreach (DataListItem colItem in dataList.Items)
{
string spID = customerDS.SP[colItem.ItemIndex].SP_ID;
CheckBox check = (CheckBox)colItem.FindControl("CheckBox2");
//如果使用此SP,置相应checked为true,forecolor为red
check.Checked = customerDS.USER_SP.FindByUSER_IDSP_ID(userID, spID) != null;
check.ForeColor = customerDS.USER_SP.FindByUSER_IDSP_ID(userID, spID) != null? Color.Red : Color.Black;
}
}
}

==================================================================
我是在DataGrid的模板列加了个DataList,然后在DataList里加的CheckBox,你可以适当改改!
在dgUserSP_PageIndexChanged()里不要忘了调用SaveData();
Good luck!!
gOODiDEA 2003-01-22
  • 打赏
  • 举报
回复
着急啊!
Lostinet 2003-01-22
  • 打赏
  • 举报
回复
那是意思意思而已。。。
。。。。
当然不是这样。~~~
---------
gOODiDEA 2003-01-22
  • 打赏
  • 举报
回复
to Lostinet(迷失网络)(大家注意∶他不是人)(ΔΔΔΔΔ)
我想这样
if(!IsPostBack)
{
ViewState["dv"]=dv;
}
else
{
DataSet dv=ViewState["dv"];
ListDataGrid.DataSource = dv;
ListDataGrid.DataBind();

}

你看行吗?我试了一下,但是不行!:)
Lostinet 2003-01-22
  • 打赏
  • 举报
回复
ds.Tables[0].Rows[item.DataSetIndex]["newColumnName"]=isChecked...
Lostinet 2003-01-22
  • 打赏
  • 举报
回复
例如::
if(!IsPostBack)
{
DataSet ds=....get...data...set;
adpater.Fill(ds);
DataColumn dc=....
DataTable dt=ds.Tables[0];
dt.Columns.Add(dc);
DataGrid1.DataSource=...DataBind();
ViewState["ds"]=ds;
}
else
{
DataSet ds=ViewState["ds"];
foreach(DataGridItem item in DataGrid1.Items)
{
bool isChecked=item.cells[x].FindControl("xxx")...Checked;
ds.Tables[0].Rows[item.DataSetIndex]=isChecked...
}
}
gOODiDEA 2003-01-22
  • 打赏
  • 举报
回复
goodidea@gnovels.com
WQLu 2003-01-22
  • 打赏
  • 举报
回复
ok
Email多少﹖
gOODiDEA 2003-01-22
  • 打赏
  • 举报
回复
To WQLu(單弦獨奏)
不好意思,你能把你的文件发给我看看吗?我不是太明白你的代码,不好试!
gOODiDEA 2003-01-22
  • 打赏
  • 举报
回复
To Lostinet(迷失网络)(大家注意∶他不是人)(ΔΔΔΔΔ)

你能详细的说说吗?


To WQLu(單弦獨奏)

我再试试你的m_strClientSelectScript
WQLu 2003-01-22
  • 打赏
  • 举报
回复
上面是我寫的一個自定義控件的部份代碼,已經通過測試.其中請教了思歸和江雨等前輩才關現﹐再次感謝他們
gOODiDEA 2003-01-22
  • 打赏
  • 举报
回复
To houjianxun(三千弱水,独取一瓢清泉)

绑定了的,我的分页代码:
protected void ListDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
ListDataGrid.CurrentPageIndex = e.NewPageIndex;
ListDataGrid.DataBind();
}
cooldht 2003-01-22
  • 打赏
  • 举报
回复
ip
WQLu 2003-01-22
  • 打赏
  • 举报
回复
看看我下面的代碼﹕選擇時不用提交在客戶端實現﹐類似于郵件選擇刪除的功能 /**************************************************************************
**函數名: m_strClientSelectScript
**功能描述: 客戶端選擇操作的腳本字串,方便用戶記錄的選擇
**日期: 2003/01/07
**修改:
**日期:
**版本
***************************************************************************/
private const string m_strClientSelectScript =
@"<script language=?javascript?>
<!--
function Onchanged(bIsSelAll)
{
var theform = document.Main;
var chkSelectAll;

for(i = theform.elements.length - 1; i >= 0; --i)
{
if (theform.elements[i].id.indexOf(?chkSelectAll?) > -1)
{
chkSelectAll = theform.elements[i];
break;
}
}
if (bIsSelAll)
{
for(i = 0; i < theform.elements.length; ++i)
if (theform.elements[i].id.indexOf(?chkSelectItem?) > -1
&& theform.elements[i].checked != chkSelectAll.checked)
theform.elements[i].checked = chkSelectAll.checked
}
else
{
bIsSelAll = true;
for(i = 0; i < theform.elements.length; ++i)
{
if (theform.elements[i].id.indexOf(?chkSelectItem?) > -1)
bIsSelAll = bIsSelAll && theform.elements[i].checked;
}
chkSelectAll.checked = bIsSelAll;
}
}

//-->
</script>";
private string GetClientSelectScript() //返回選擇腳本字符串
{
return m_strClientSelectScript.Replace("?","\"");
}

***********************************************

Response.Write(GetClientScript());

LinkButton btn;
string strScript = "javascript:theform = document.Main;isSel = false;"//確定選擇的腳本
+ "for(i = 0; i < theform.elements.length; ++i)"
+ "{"
+ " if (theform.elements[i].id.indexOf('chkSelectItem') > -1 && theform.elements[i].checked) "
+ " {isSel = true;break;}"
+ "}";
string strScript1 = "";
for (int i = 0; i < dlsSubProgLevel3.Items.Count; ++i)
{
btn = (LinkButton)dlsSubProgLevel3.Items[i].FindControl("btnSubProgItem");
switch (dlsSubProgLevel3.DataKeys[i].ToString().Trim())

{
case "add":
strScript1 = "javascript:window.open('Comm/Editor.aspx?EditState=add&Key1=&Key2=','Detail').focus();return false;";
btn.Attributes["OnClick"] = strScript1;
continue;
case "delete"://添加刪除確認的腳本
strScript1 = "if (!isSel)"
+ "{alert('請選擇要刪除的記錄!'); return false;}"
+ "else return confirm('刪除後將不能恢復,真的要刪除嗎?');";
btn.Attributes["OnClick"] = strScript + strScript1;
continue;
case "modify"://添加修改的腳本
strScript1 = "if (!isSel)"
+ " {alert('請選擇要修改的記錄!'); return false;}"
+ "else "
+ "{var cell = theform.elements[i].parentElement.parentElement;"
+ "cell = cell.parentElement.cells[cell.cellIndex+1];"
+ "window.open(cell.all.tags('A')[0].href.replace('EditState=browse','EditState=modify'),'Detail').focus();"
+ "return false;}";
btn.Attributes["OnClick"] = strScript + strScript1;
continue;
}
}
gOODiDEA 2003-01-22
  • 打赏
  • 举报
回复
TO WQLu(單弦獨奏) 你的办法不行啊。

你那样的话,分页和全选都不成!
Lostinet 2003-01-22
  • 打赏
  • 举报
回复
DataGrid的DataBind会把所有的子控件清空一次。
所以新加的控件的状态需要保存起来。
在DataTable新增加一列用来做那事就好了。
gOODiDEA 2003-01-22
  • 打赏
  • 举报
回复
我试试先!
WQLu 2003-01-22
  • 打赏
  • 举报
回复
對不起﹐我上面的不行
ykn 2003-01-22
  • 打赏
  • 举报
回复
up
加载更多回复(4)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧