问题描述
想在gridvew外面加一个按钮,点击设置所有项的全选和取消全选,该怎样做?
解决方案
解决方案二:
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;publicpartialclassDefault2:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){Bind();}}privatevoidBind(){this.GridView1.DataSource=GetData();this.GridView1.DataBind();}protectedvoidButton1_Click(objectsender,EventArgse){for(inti=0;i<this.GridView1.Rows.Count;i++){CheckBoxchb=this.GridView1.Rows[i].Cells[0].FindControl("CheckBox1")asCheckBox;chb.Checked=true;}}protectedvoidButton2_Click(objectsender,EventArgse){for(inti=0;i<this.GridView1.Rows.Count;i++){CheckBoxchb=this.GridView1.Rows[i].Cells[0].FindControl("CheckBox1")asCheckBox;chb.Checked=false;}}privateDataTableGetData(){DataTabledt=newDataTable();dt.Columns.Add("aa",typeof(string));dt.Columns.Add("bb",typeof(string));for(inti=0;i<5;i++){DataRowdr=dt.NewRow();dr["aa"]=i.ToString();dr["bb"]=i.ToString();dt.Rows.Add(dr);}returndt;}}
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>无标题页</title></head><body><formid="form1"runat="server"><div><asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"><Columns><asp:TemplateField><EditItemTemplate><asp:TextBoxID="TextBox1"runat="server"></asp:TextBox></EditItemTemplate><ItemTemplate><asp:CheckBoxID="CheckBox1"runat="server"/></ItemTemplate></asp:TemplateField><asp:BoundFieldDataField="aa"HeaderText="AAAA"/><asp:BoundFieldDataField="bb"HeaderText="BBBB"/></Columns></asp:GridView><asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="All"Width="39px"/> <asp:ButtonID="Button2"runat="server"OnClick="Button2_Click"Text="None"/></div></form></body></html>
解决方案三:
http://www.cnblogs.com/Ewait/archive/2008/03/04/1090327.htmlhttp://www.cnblogs.com/jeff377/archive/2007/12/12/992896.htmlhttp://www.cnblogs.com/temptation/archive/2007/06/21/791746.htmlhttp://www.cnblogs.com/webabcd/archive/2007/01/25/629810.html这个操作比较平常,有很多中解决方法
解决方案四:
找一下以前的帖子,很多解决的。就前几天我才回贴子贴了一段代码。1楼的是服务端做。一般用js做会有更好的用户体验。
解决方案五:
我不是用按钮控制的用的也是一个复选框,写的JS代码楼主看看,或许有用,改改很简单的functioncheckall(list){//alert('暂无数据');if(document.getElementById(list)!=null){chk11=document.getElementsByName("chk11");if(chk11.checked==true){chk11.checked=false;}else{chk11.checked=true;}chk=document.getElementsByName("chk1");if(chk11.checked==true){for(i=0;i<chk.length;i++){chk[i].checked=true;}}else{for(i=0;i<chk.length;i++){chk[i].checked=false;}}}else{alert('暂无数据');}}</script>
解决方案六:
谢谢,已解决,想问一下,想获取各行各个格子中的元素怎样取?我这样一写GridView_Asign.DataKeys[i]就说我的i越界,可是我限制的是for(inti=0;i<=GridView_Asign.Rows.Count-1;i++)
解决方案七:
那个搞定了,现在有个新问题:错误显示:指定的参数已超出有效值的范围。参数名:index说明:执行当前Web请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息:System.ArgumentOutOfRangeException:指定的参数已超出有效值的范围。参数名:index源错误:行46:AssessRow["PaperReader"]=DropDownList1.SelectedValue.ToString();行47://objectobtest=(TextBox)(GridView_Asign.Rows[i].Cells[1].Controls[1]);行48:AssessRow["PaperTitle"]=((TextBox)(GridView_Asign.Rows[i].Cells[1].Controls[0])).Text.ToString().Trim();行49:AssessRow["PaperAuthor"]=((System.Web.UI.DataBoundLiteralControl)GridView_Asign.Rows[i].Cells[1].Controls[0]).Text.Trim();行50:isUpdate=true;我看了一下,是controls[0]这里就错了,麻烦您帮忙解释一下
解决方案八:
//设置CheckBox的选择状态js反选全选多选框兼容ie和firefox版本functionSettingChecked(isCked){varck=document.getElementsByTagName("input");//意思是找到页面上所有<input>节点for(vari=0;i<ck.length;i++){if(ck[i].type=="checkbox"){ck[i].checked=(isCked=="selectAll")?true:(!ck[i].checked);}}}//判断选中提示。functionDelAllItem(id,evt){if(CkeckedTolNum(id)==0){alert('<%=FSSoft.Web.UI.Language.Msg("00005")%>');returnfalse;}else{returnwindow.confirm('<%=FSSoft.Web.UI.Language.Msg("00008")%>');}}functionCkeckedTolNum(cid){varnum=0;varcks=document.getElementsByTagName("input");for(vari=0;i<cks.length;i++){//cks[i].id.indexOf(cid)=0时,说明是以cid开头的IDif(cks[i].type=="checkbox"&&cks[i].checked&&cks[i].id.indexOf(cid)>=0){num++;}}returnnum;}functionNoCheckSelect()//不选{varcks=document.getElementsByTagName("input");for(vari=0;i<cks.length;i++){if(cks[i].type.toLowerCase()=="checkbox"&&cks[i].checked){//如果选择框选中则清空if(cks[i].checked.toString()=="true"){cks[i].checked=false;}}}<asp:CheckBoxID="single_Chk"name="selectAll"runat="server"/>}
解决方案九:
//判断选中没。没选中则提示delWriteLog.OnClientClick="returnDelAllItem('single_Chk');";按钮事件放在pageload