之前从网上借鉴了一个方法,在gridview中添加全选与不选批处理.但是不支持firefox 文章地址
所以弄了个方法,现在以下的方法支持ff,已经测试通过
aspx代码
<head runat="server">
<title>案例审核</title>
<script type="text/javascript" language="javascript">
<!--
function checkAllCheckboxes(_checkboxAll, controlName) {
var _isChecked;
var _checkbox;
var i = 2;
_isChecked = _checkboxAll.checked;
//_checkboxAll.checked=(_isChecked);
while (document.getElementById(controlName + '_ctl' + getIDString(i) + '_chkItem')) {
_checkbox = document.getElementById(controlName + '_ctl' + getIDString(i) + '_chkItem')
if (_checkbox.type) {
if (_checkbox.type == 'checkbox') {
if (!_checkbox.disabled) {
_checkbox.checked = _isChecked
}
} // if
} // if
i++;
} // while
}
function getIDString(_id) {
var returnString
if (_id < 10) {
return '0' + _id.toString()
}
else {
return _id.toString()
}
}
function checkTopCheckbox(controlName) {
var _checkbox;
var i = 2;
var _topCheckbox = document.getElementById(controlName + '_ctl01_chkSelectAll');
var _isAllChecked = true;
while (document.getElementById(controlName + '_ctl' + getIDString(i) + '_chkItem')) {
_checkbox = document.getElementById(controlName + '_ctl' + getIDString(i) + '_chkItem')
if (_checkbox.type) {
if (_checkbox.type == 'checkbox') {
if (_checkbox.checked == false) {
_isAllChecked = false;
//alert(_isAllChecked);
break;
} // end if
} // end if
} // end if
i++;
} // end while
if (_isAllChecked) {
_topCheckbox.checked = true;
}
else {
_topCheckbox.checked = false;
}
}
-->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="960px" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" style="font-weight: bold; font-size: 15px;" valign="top">
案例审核
</td>
</tr>
<tr>
<td valign="top">
关键字:<asp:DropDownList ID="dropSearchType" runat="server">
<asp:ListItem>用户名</asp:ListItem>
<asp:ListItem>IP</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtKeyword" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="搜索" OnClick="btnSearch_Click" />
</td>
</tr>
<tr>
<td valign="top">
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" Width="100%" EnableViewState="False"
PageSize="20" PagerStyle-Height="15px" CellPadding="2" ForeColor="#333333" GridLines="None" BorderColor="#507CD1"
BorderWidth="1px" AutoGenerateColumns="False" AllowPaging="true"
onrowdatabound="GridView1_RowDataBound"
onpageindexchanging="GridView1_PageIndexChanging">
<RowStyle HorizontalAlign="Center" BackColor="#EFF3FB" Font-Size="11px" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkSelectAll"></asp:CheckBox>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkItem" runat="server" />
</ItemTemplate>
<ItemStyle Width="20px" />
</asp:TemplateField>
<asp:BoundField DataField="案例编号" HeaderText="案例编号" />
<asp:BoundField DataField="用户名" HeaderText="用户名" />
<asp:BoundField DataField="项目名称" HeaderText="项目名称" />
<asp:BoundField DataField="宗地号" HeaderText="宗地号" />
<asp:BoundField DataField="房屋用途" HeaderText="房屋用途" />
<asp:BoundField DataField="均价" HeaderText="均价" />
<asp:BoundField DataField="添加时间" HeaderText="添加时间" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" Height="18px" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Font-Size="11px" />
<AlternatingRowStyle BackColor="White" />
<EmptyDataRowStyle Height="50px" HorizontalAlign="Center" Font-Bold="True" Font-Size="17px" />
<EmptyDataTemplate>
没有找到符合条件的信息。
</EmptyDataTemplate>
<PagerSettings PageButtonCount="20" />
</asp:GridView>
</td>
</tr> <tr><td><asp:Button
ID="btnCheckBatch" runat="server" Text="批量通过审核"
OnClientClick="javascript:event.returnValue=confirm('确定要批量通过审核?');"
onclick="btnCheckBatch_Click" />
</td></tr>
</table>
</div>
</form>
</body>
后台绑定gridview_rowDataBound事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.DataRow:
CheckBox chkItem = (CheckBox)e.Row.FindControl("chkItem");
chkItem.Attributes.Add("onclick", String.Format("checkTopCheckbox('{0}')", this.GridView1.ClientID));
break;
case DataControlRowType.Header:
CheckBox chkSelectAll = (CheckBox)e.Row.FindControl("chkSelectAll");
chkSelectAll.Attributes.Add("onclick", String.Format("checkAllCheckboxes(this,'{0}')", this.GridView1.ClientID));
break;
}
}
获取select选择项的值:
protected void btnCheckBatch_Click(object sender, EventArgs e)
{
string aa = "";
foreach (GridViewRow _listItem in this.GridView1.Rows) //获取所选项
{
CheckBox chkbox = (CheckBox)_listItem.FindControl("chkItem");
if (chkbox.Checked)
{
int _Id = Convert.ToInt32(this.GridView1.DataKeys[_listItem.RowIndex].Value);
//处理数据操作
aa += _Id.ToString() + ",";
}
}
Response.Write(aa);
}
忘了加上没有选择项的时候按钮javascript提示了
在aspx 也的<script> 中加入
// 判断多选是否与选中项(没有选中的返回false)
function slcNo_click(controlName)
{
var _checkbox;
var i = 2;
//_checkboxAll.checked=(_isChecked);
while (document.getElementById(controlName + '_ctl' + getIDString(i) + '_chkItem')) {
_checkbox = document.getElementById(controlName + '_ctl' + getIDString(i) + '_chkItem')
if (_checkbox.type == 'checkbox') {
if (_checkbox.checked) return true;
i++;
}
} // while
alert("请选择后再操作!");
return false;
}
再在后台cs文件Page_Load事件中加入
btnCheckBatch.Attributes.Add("onclick", String.Format("return slcNo_click('{0}')", this.GridView1.ClientID));
这样就可以在没有选择任何项进行操作时弹出客户端提示不进行后台批量操作了。