DataGrid中如何保持Check控件的状态 一个DataGrid控件,其中有一列是checkBox控件,该DataGrid带分页,在我选其他页的时候,如何保持前一页的checkBox的状态,用什么方法比较好,效率会比较高? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://dotnet.aspx.cc/ShowDetail.aspx?id=F43AF9A5-2C2E-4AA6-E976-21E9569F5A8Ahttp://dotnet.aspx.cc/Exam/SelectMultiPages.aspx http://dotnet.aspx.cc/ShowDetail.aspx?id=F43AF9A5-2C2E-4AA6-E976-21E9569F5A8Ahttp://dotnet.aspx.cc/Exam/SelectMultiPages.aspx 1.使用runat="server",当做服务器控件使用。2.自已写程序保持它的状态,可存于hidden字段或viewstate中。 以下代码解释:在DataTable中增加一个if_checked列,BOOL型的,用来保存是否点击了,在翻页时候,根据这个值来确定DATAGRID的状态.private void SetViewData(){string strSQL,strYear;DataTable dt;strYear=dblistYear.SelectedValue;string usercode=Session["user_code"].ToString();UserClass user=new UserClass(usercode);user.GetUserInfo(usercode);if(user.strDWDM!="H01"){strSQL="select code,name from jc_jddw where code ='"+user.strDWDM+"'";}else{strSQL="select code,name from jc_jddw where code in( select code from JC_JDDW connect by prior CODE=SJ_CODE start with SJ_CODE='J01'";strSQL+=" MINUS select sj_code from JC_JDDW connect by prior CODE=SJ_CODE start with SJ_CODE='J01')";}DbOper dboper=new DbOper(strSQL); dt=new DataTable();dt.Columns.Add("if_checked"); dt.Columns.Add("dw_code");dt.Columns.Add("dw_name");dt.Columns.Add("chklist");dt.Columns.Add("if_nextsj");dt.Columns.Add("if_havesj");if (dboper.CurrTB.Rows.Count>0){for(int i=0;i<dboper.CurrTB.Rows.Count;i++){DataRow newdr=dt.NewRow();newdr["dw_code"]=dboper.CurrTB.Rows[i]["code"].ToString();newdr["dw_name"]=dboper.CurrTB.Rows[i]["name"].ToString(); newdr["if_nextsj"]=IfNextJy(strYear,dboper.CurrTB.Rows[i]["code"].ToString());newdr["if_havesj"]=IfHaveSj(strYear,dboper.CurrTB.Rows[i]["code"].ToString()); if(IfNextJy(strYear,dboper.CurrTB.Rows[i]["code"].ToString())=="Y"){newdr["if_checked"]="1";}else{newdr["if_checked"]="0";}dt.Rows.Add(newdr); } }Session["table"]=dt;DataGrid1.DataSource=dt;//this.DataGrid1.CurrentPageIndex=0;DataGrid1.DataBind();int istartrow=0; if (this.DataGrid1.PageCount==0){return;}if (this.DataGrid1.PageCount==1){istartrow=0;}if (this.DataGrid1.PageCount>1){istartrow=this.DataGrid1.PageSize*this.DataGrid1.CurrentPageIndex;}System.Web.UI.WebControls.Label lb1;System.Web.UI.WebControls.Label lb2;System.Web.UI.WebControls.Image im1;System.Web.UI.WebControls.Image im2; System.Web.UI.WebControls.CheckBox chkbox;for (int i=0;i<DataGrid1.Items.Count;i++){im1=(System.Web.UI.WebControls.Image)DataGrid1.Items[i].Cells[2].FindControl("img1");lb1=(System.Web.UI.WebControls.Label)DataGrid1.Items[i].Cells[2].FindControl("lb1");im2=(System.Web.UI.WebControls.Image)DataGrid1.Items[i].Cells[3].FindControl("img2");lb2=(System.Web.UI.WebControls.Label)DataGrid1.Items[i].Cells[3].FindControl("lb2"); chkbox=(System.Web.UI.WebControls.CheckBox)DataGrid1.Items[i].Cells[0].FindControl("chkbox");if (dt.Rows[i+istartrow]["if_nextsj"].ToString()=="Y"){lb1.Width=0;im1.ImageUrl="../../images/Ok.bmp";chkbox.Visible=true;chkbox.Checked=false;chkbox.Width=10;}if (dt.Rows[i+istartrow]["if_nextsj"].ToString()=="N"){lb1.Text="无";im1.ImageUrl="../../images/blank.bmp";im1.Width=0;chkbox.Visible=false;}if (dt.Rows[i+istartrow]["if_havesj"].ToString()=="Y"){lb2.Width=0;im2.ImageUrl="../../images/Ok.bmp";}if (dt.Rows[i+istartrow]["if_havesj"].ToString()=="N"){lb2.Text="未形成";im2.ImageUrl="../../images/blank.bmp";im2.Width=0;}}} http://dotnet.aspx.cc/ShowDetail.aspx?id=F43AF9A5-2C2E-4AA6-E976-21E9569F5A8Ahttp://dotnet.aspx.cc/Exam/SelectMultiPages.aspx 那个checkbox如果是runat=server的,是可以自动保存状态的。 后台输出服务器控件LINkbutton 远程连接SQLServer 不存在或访问被拒绝 ~~~~~ 刚跟一个网友的聊天记录,发人深省。虽然不是技术问题,但跟技术人息息相关 datagrid 嵌套 repeater 的问题~~ 0分求助,穷啊 关于对象做为 参数 传递的问题 求救 javascript里怎么得到另外一个页面通过后缀传过来的参数啊? 简单问题,高分相送,高手请进!在线等待! Session到底占多少服务器资源呀? 请问在自定义控件中如何将某一段脚本写到<html>之上或者<head></head>之间 泣血请教:页面刷新问题
2.自已写程序保持它的状态,可存于hidden字段或viewstate中。
在DataTable中增加一个if_checked列,BOOL型的,用来保存是否点击了,在翻页时候,根据这个值来确定DATAGRID的状态.
private void SetViewData()
{
string strSQL,strYear;
DataTable dt;strYear=dblistYear.SelectedValue;
string usercode=Session["user_code"].ToString();
UserClass user=new UserClass(usercode);
user.GetUserInfo(usercode);
if(user.strDWDM!="H01")
{
strSQL="select code,name from jc_jddw where code ='"+user.strDWDM+"'";}
else
{
strSQL="select code,name from jc_jddw where code in( select code from JC_JDDW connect by prior CODE=SJ_CODE start with SJ_CODE='J01'";
strSQL+=" MINUS select sj_code from JC_JDDW connect by prior CODE=SJ_CODE start with SJ_CODE='J01')";
}
DbOper dboper=new DbOper(strSQL); dt=new DataTable();
dt.Columns.Add("if_checked");
dt.Columns.Add("dw_code");
dt.Columns.Add("dw_name");
dt.Columns.Add("chklist");
dt.Columns.Add("if_nextsj");
dt.Columns.Add("if_havesj");if (dboper.CurrTB.Rows.Count>0)
{
for(int i=0;i<dboper.CurrTB.Rows.Count;i++)
{
DataRow newdr=dt.NewRow();newdr["dw_code"]=dboper.CurrTB.Rows[i]["code"].ToString();
newdr["dw_name"]=dboper.CurrTB.Rows[i]["name"].ToString();
newdr["if_nextsj"]=IfNextJy(strYear,dboper.CurrTB.Rows[i]["code"].ToString());
newdr["if_havesj"]=IfHaveSj(strYear,dboper.CurrTB.Rows[i]["code"].ToString());
if(IfNextJy(strYear,dboper.CurrTB.Rows[i]["code"].ToString())=="Y")
{
newdr["if_checked"]="1";
}
else
{
newdr["if_checked"]="0";
}
dt.Rows.Add(newdr);
}
}
Session["table"]=dt;
DataGrid1.DataSource=dt;
//this.DataGrid1.CurrentPageIndex=0;
DataGrid1.DataBind();int istartrow=0;
if (this.DataGrid1.PageCount==0)
{
return;
}
if (this.DataGrid1.PageCount==1)
{
istartrow=0;
}
if (this.DataGrid1.PageCount>1)
{
istartrow=this.DataGrid1.PageSize*this.DataGrid1.CurrentPageIndex;
}System.Web.UI.WebControls.Label lb1;
System.Web.UI.WebControls.Label lb2;
System.Web.UI.WebControls.Image im1;
System.Web.UI.WebControls.Image im2;
System.Web.UI.WebControls.CheckBox chkbox;for (int i=0;i<DataGrid1.Items.Count;i++)
{
im1=(System.Web.UI.WebControls.Image)DataGrid1.Items[i].Cells[2].FindControl("img1");
lb1=(System.Web.UI.WebControls.Label)DataGrid1.Items[i].Cells[2].FindControl("lb1");
im2=(System.Web.UI.WebControls.Image)DataGrid1.Items[i].Cells[3].FindControl("img2");
lb2=(System.Web.UI.WebControls.Label)DataGrid1.Items[i].Cells[3].FindControl("lb2");
chkbox=(System.Web.UI.WebControls.CheckBox)DataGrid1.Items[i].Cells[0].FindControl("chkbox");if (dt.Rows[i+istartrow]["if_nextsj"].ToString()=="Y")
{
lb1.Width=0;
im1.ImageUrl="../../images/Ok.bmp";
chkbox.Visible=true;
chkbox.Checked=false;
chkbox.Width=10;}
if (dt.Rows[i+istartrow]["if_nextsj"].ToString()=="N")
{
lb1.Text="无";
im1.ImageUrl="../../images/blank.bmp";
im1.Width=0;
chkbox.Visible=false;
}if (dt.Rows[i+istartrow]["if_havesj"].ToString()=="Y")
{
lb2.Width=0;
im2.ImageUrl="../../images/Ok.bmp";
}
if (dt.Rows[i+istartrow]["if_havesj"].ToString()=="N")
{
lb2.Text="未形成";
im2.ImageUrl="../../images/blank.bmp";
im2.Width=0;
}
}
}