<
form id
=
"
form1
"
runat
=
"
server
"
>
< asp:GridView ID = " GridView1 " runat = " server " AutoGenerateColumns = " False " DataSourceID = " ObjectDataSource1 " AllowPaging = " True " OnRowDataBound = " GridView1_RowDataBound " >
< Columns >
< asp:BoundField DataField = " STATIONID " HeaderText = " STATIONID " SortExpression = " STATIONID " />
< asp:TemplateField >
< ItemTemplate >
< asp:CheckBox ID = " chkSelect " runat = " server " onclick = " checkbox_onchange() " />
</ ItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField = " STATIONNAME " HeaderText = " STATIONNAME " SortExpression = " STATIONNAME " />
< asp:BoundField DataField = " SECONDNAME " HeaderText = " SECONDNAME " SortExpression = " SECONDNAME " />
< asp:BoundField DataField = " THIRDNAME " HeaderText = " THIRDNAME " SortExpression = " THIRDNAME " />
< asp:BoundField DataField = " ALLROAD " HeaderText = " ALLROAD " SortExpression = " ALLROAD " />
</ Columns >
</ asp:GridView >
< asp:ObjectDataSource ID = " ObjectDataSource1 " runat = " server " SelectMethod = " GetAllList "
TypeName = " BusManage.BLL.BusStation " ></ asp:ObjectDataSource >
< input id = " eleName " readonly
type = " text " size = " 53 " runat = " server " > < input id = " eleValue " type = " hidden " runat = " server " >
</ form >
< asp:GridView ID = " GridView1 " runat = " server " AutoGenerateColumns = " False " DataSourceID = " ObjectDataSource1 " AllowPaging = " True " OnRowDataBound = " GridView1_RowDataBound " >
< Columns >
< asp:BoundField DataField = " STATIONID " HeaderText = " STATIONID " SortExpression = " STATIONID " />
< asp:TemplateField >
< ItemTemplate >
< asp:CheckBox ID = " chkSelect " runat = " server " onclick = " checkbox_onchange() " />
</ ItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField = " STATIONNAME " HeaderText = " STATIONNAME " SortExpression = " STATIONNAME " />
< asp:BoundField DataField = " SECONDNAME " HeaderText = " SECONDNAME " SortExpression = " SECONDNAME " />
< asp:BoundField DataField = " THIRDNAME " HeaderText = " THIRDNAME " SortExpression = " THIRDNAME " />
< asp:BoundField DataField = " ALLROAD " HeaderText = " ALLROAD " SortExpression = " ALLROAD " />
</ Columns >
</ asp:GridView >
< asp:ObjectDataSource ID = " ObjectDataSource1 " runat = " server " SelectMethod = " GetAllList "
TypeName = " BusManage.BLL.BusStation " ></ asp:ObjectDataSource >
< input id = " eleName " readonly
type = " text " size = " 53 " runat = " server " > < input id = " eleValue " type = " hidden " runat = " server " >
</ form >
.cs绑定
if
(e.Item.ItemType
==
ListItemType.Item
||
e.Item.ItemType
==
ListItemType.AlternatingItem)
... {
if(HdnSelectedValues.Value.IndexOf(((Literal)e.Item.Cells[1].FindControl("TitleShow")).Text) >= 0 )
...{
HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Item.Cells[0].FindControl("ChkSelect"));
ChkSelected.Checked = true;
}
}
... {
if(HdnSelectedValues.Value.IndexOf(((Literal)e.Item.Cells[1].FindControl("TitleShow")).Text) >= 0 )
...{
HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Item.Cells[0].FindControl("ChkSelect"));
ChkSelected.Checked = true;
}
}
下面javarscript放页面下方
<
script language
=
"
javascript
"
type
=
"
text/javascript
"
>
SetCheckBoxStatus();
function SetCheckBoxStatus()
... {
var tb = document.getElementById("GridView1");
var eleValue = document.getElementById("eleValue").value;
for(var i=1;i<tb.rows.length-1;i++)
...{
var chk = tb.rows[i].cells[1].firstChild;
if(eleValue.indexOf(tb.rows[i].cells[2].innerText+",") >-1)
...{
chk.checked = true;
}
}
}
function checkbox_onchange()
... {
var tb = document.getElementById("GridView1");
var eleValue = document.getElementById("eleValue");
for(var i=1;i<tb.rows.length-1;i++)
...{
var chk = tb.rows[i].cells[1].firstChild;
var id = tb.rows[i].cells[2].innerText;
eleValue.value = eleValue.value.replace(id+ ",","");
if(chk.checked)
...{
eleValue.value = eleValue.value + id+ ",";
}
}
}
</ script >
SetCheckBoxStatus();
function SetCheckBoxStatus()
... {
var tb = document.getElementById("GridView1");
var eleValue = document.getElementById("eleValue").value;
for(var i=1;i<tb.rows.length-1;i++)
...{
var chk = tb.rows[i].cells[1].firstChild;
if(eleValue.indexOf(tb.rows[i].cells[2].innerText+",") >-1)
...{
chk.checked = true;
}
}
}
function checkbox_onchange()
... {
var tb = document.getElementById("GridView1");
var eleValue = document.getElementById("eleValue");
for(var i=1;i<tb.rows.length-1;i++)
...{
var chk = tb.rows[i].cells[1].firstChild;
var id = tb.rows[i].cells[2].innerText;
eleValue.value = eleValue.value.replace(id+ ",","");
if(chk.checked)
...{
eleValue.value = eleValue.value + id+ ",";
}
}
}
</ script >