问题描述
使用Java脚本的Gridview qwith Checkbox
选择所有选项
Gridview qwith Checkbox using Java script
select all options
推荐答案
<script type="text/javascript">
function checkAll(objRef) {
var GridView = objRef.parentNode.parentNode.parentNode;
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
//Get the Cell To find out ColumnIndex
var row = inputList[i].parentNode.parentNode;
if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
if (objRef.checked) {
//If the header checkbox is checked
//check all checkboxes
//and highlight all rows
//row.style.backgroundColor = "#ff5959";
inputList[i].checked = true;
}
else {
//If the header checkbox is checked
//uncheck all checkboxes
//and change rowcolor back to original
if (row.rowIndex % 2 == 0) {
//Alternating Row Color
// row.style.backgroundColor = "#ececec";
}
else {
//row.style.backgroundColor = "#d8d8d8";
}
inputList[i].checked = false;
}
}
}
}
function Check_Click(objRef) {
//Get the Row based on checkbox
var row = objRef.parentNode.parentNode;
if (objRef.checked) {
//If checked change color to Aqua
//row.style.backgroundColor = "#ff5959";
}
else {
//If not checked change back to original color
if (row.rowIndex % 2 == 0) {
//Alternating Row Color
// row.style.backgroundColor = "#ececec";
}
else {
// row.style.backgroundColor = "#d8d8d8";
}
}
//Get the reference of GridView
var GridView = row.parentNode;
//Get all input elements in Gridview
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
//The First element is the Header Checkbox
var headerCheckBox = inputList[0];
//Based on all or none checkboxes
//are checked check/uncheck Header Checkbox
var checked = true;
if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
if (!inputList[i].checked) {
checked = false;
break;
}
}
}
headerCheckBox.checked = checked;
}
--------------
--------------
<asp:GridView ID="grdRequsers" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" OnSelectedIndexChanging="grdRequsers_SelectedIndexChanging" DataKeyNames="CID,ipaddress">
<Columns>
<asp:TemplateField HeaderText="" HeaderStyle-Width="5px">
<HeaderTemplate>
<asp:CheckBox Text="" ID="chkSelectAll" runat="server" onclick="checkAll(this);"
TextAlign="Left" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" onclick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="sd" SortExpression="sd" HeaderStyle-Width="70px"
HeaderStyle-BorderColor="White">
<ItemTemplate>
<asp:Label ID="lblPostCode" runat="server" Text='<%# Eval("sd") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
-------------------------------
-------------------------------
protected void grd1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
var tempid = Convert.ToInt64(grd1.DataKeys[e.NewSelectedIndex].Values[0]);
Session["ViewID"] = tempid;
Response.Redirect("NextPage.aspx");
}
---------------
这是下一页
tempid = Convert.ToInt64(Session ["ViewID"]);
SqlCommand cmd =新的SqlCommand("SELECT * FROM tblname where id =" + tempid,con);
SqlDataReader dr = cmd.ExecuteReader();
如果(dr.Read())
{
txtid.Text = Convert.ToString(dr ["id"]);
txtname.Text = Convert.ToString(dr ["name"]);
}
con.Close();
---------------
this is for next page
tempid = Convert.ToInt64(Session["ViewID"]);
SqlCommand cmd = new SqlCommand("SELECT * FROM tblname where id=" + tempid, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtid.Text = Convert.ToString(dr["id"]);
txtname.Text = Convert.ToString(dr["name"]);
}
con.Close();
这篇关于带复选框的Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!