asp.net GridView控件中實(shí)現(xiàn)全選的解決方案
更新時(shí)間:2010年03月05日 23:14:29 作者:
在GridView中我們經(jīng)常要利用復(fù)選按鈕實(shí)現(xiàn)全選的功能,下面針對(duì)這一解決方案做以總結(jié)
第一種:利用客戶端控件實(shí)現(xiàn)
JS:
<script type="text/javascript">
function checkAll()
{
var checklist=document.getElementsByTagName("input");
for(var i=0;i<checklist.length;i++)
{
if(checklist[i].type=="checkbox")
{
checklist[i].checked=document.form1.ck.checked;
}
}
}
</script>
GridView控件:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="產(chǎn)品編號(hào)" />
<asp:TemplateField>
<HeaderTemplate>
<input id="ck" type="checkbox" onclick="checkAll();" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
第二種:利用服務(wù)器端控件實(shí)現(xiàn)
protected void 全選_CheckedChanged(object sender, EventArgs e)
{
if (全選.Checked == true)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("checkbox1") as CheckBox;
if (ck!=null)
{
ck.Checked = true;
}
}
}
else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("checkbox1") as CheckBox;
if (ck != null)
{
ck.Checked = false;
}
}
}
}
JS:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function checkAll()
{
var checklist=document.getElementsByTagName("input");
for(var i=0;i<checklist.length;i++)
{
if(checklist[i].type=="checkbox")
{
checklist[i].checked=document.form1.ck.checked;
}
}
}
</script>
GridView控件:
復(fù)制代碼 代碼如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="產(chǎn)品編號(hào)" />
<asp:TemplateField>
<HeaderTemplate>
<input id="ck" type="checkbox" onclick="checkAll();" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
第二種:利用服務(wù)器端控件實(shí)現(xiàn)
復(fù)制代碼 代碼如下:
protected void 全選_CheckedChanged(object sender, EventArgs e)
{
if (全選.Checked == true)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("checkbox1") as CheckBox;
if (ck!=null)
{
ck.Checked = true;
}
}
}
else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("checkbox1") as CheckBox;
if (ck != null)
{
ck.Checked = false;
}
}
}
}
您可能感興趣的文章:
- ASP.NET 2.0/3.5中直接操作Gridview控件插入新記錄
- asp.net GridView控件鼠標(biāo)移動(dòng)某行改變背景顏色(方法一)
- asp.net GridView控件中模板列CheckBox全選、反選、取消
- ASP.NET2.0中用Gridview控件操作數(shù)據(jù)的代碼
- ASP.NET GridView控件在列上格式化時(shí)間及DataFormatString使用
- asp.net的GridView控件使用方法大全
- asp.net中GridView控件遍歷的小例子
- Asp.net的GridView控件實(shí)現(xiàn)單元格可編輯方便用戶使用
- ASP.NET4 GridView的四種排序樣式詳解
- ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
- asp.net gridview分頁(yè):第一頁(yè) 下一頁(yè) 1 2 3 4 上一頁(yè) 最末頁(yè)
- ASP.NET中為GridView添加刪除提示框的方法
- asp.net中GridView數(shù)據(jù)鼠標(biāo)移入顯示提示信息
- 如何用jQuery實(shí)現(xiàn)ASP.NET GridView折疊伸展效果
- ASP.NET GridView中加入RadioButton不能單選的解決方案
- 靈活掌握asp.net中g(shù)ridview控件的多種使用方法(上)
- 靈活掌握asp.net中g(shù)ridview控件的多種使用方法(下)
相關(guān)文章
.NET性能調(diào)優(yōu)之一:ANTS Performance Profiler的使用介紹
本系列文章主要會(huì)介紹一些.NET性能調(diào)優(yōu)的工具、Web性能優(yōu)化的規(guī)則(如YSlow)及方法等等內(nèi)容。成文前最不希望看到的就是園子里不間斷的“哪個(gè)語(yǔ)言好,哪個(gè)語(yǔ)言性能高”的爭(zhēng)論,不多說(shuō),真正的明白人都應(yīng)該知道這樣的爭(zhēng)論有沒(méi)有意義,希望我們能從實(shí)際性能優(yōu)化的角度去討論問(wèn)題2013-01-01ASP.NET 緩存分析和實(shí)踐淺析提高運(yùn)行效率
說(shuō)到ASP.NET緩存,那就是:盡早緩存;經(jīng)常緩存您應(yīng)該在應(yīng)用程序的每一層都實(shí)現(xiàn)緩存。2010-02-02如何利用FluentMigrator實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移
這篇文章主要給大家介紹了關(guān)于如何利用FluentMigrator實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04.NET客戶端實(shí)現(xiàn)Redis中的管道(PipeLine)與事物(Transactions)
本文主要介紹了.NET客戶端實(shí)現(xiàn)Redis中的管道(PipeLine)與事物(Transactions)的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03基于.NET Core 3.1 網(wǎng)站開(kāi)發(fā)和部署的方法
這篇文章主要介紹了基于.NET Core 3.1 網(wǎng)站開(kāi)發(fā)和部署的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08