我想实现 选中Gridview中的Checkbox后,利用js 获取被选中这一行的其他字段的数据。
解决方案 »
- repeater或者gridview之类的控件嵌套之后,怎么获取里面的控件?
- 关于访问DataGrid中FooterTemplate里控件问题
- 关于iis的问题(最后一问)
- ASP.NET中用AJAX实现无刷新求和~!!~~~~~~~
- 保存Table为Excel
- 这样的数组覆值的时候为什么会出现问题?
- 超简易留言薄 附数据库连接类,分页类。跟大家交流一下
- 招聘.net程序员、系统分析员、专职测试人员(北京工作)
- 为什么在生成网站时出现dropdownlist未定义?
- 100分请教关于ASP.net中Page_Load事件的问题??
- PetShop4.0的疑惑,关于BLL层是否可以使用静态方法
- .NET页面里.怎样循环输出一个日期段?
{
this.parentElement.children.item(index);
}
runat=server Text=<%# DataBinder.Eval(Container.DataItem,"name")%> />
...选择checkbox的时候将自己传过去onclick=ustbwuyi(this)function ustbwuyi(obj)
{
var id=obj.id;
id=id.substring(id.indexOf("a")+1,id.length);
var b=document.getElementById("b"+id).value;
}
for(var i=1;i<oTbody.children.length;i++)
{
var oTr=oTbody.children.item(i);
var 字段1=oTr.children.item(0).innerText
var 字段2=oTr.children.item(1).innerText;
}
var cellCount = tb.rows[1].cells.length;
for(var m=1;m<rowCount;m++)
{
for(var n=1;n<cellCount;n++)
{
var cell = tb.rows[m].cells[n];
if(typeof cell!="undefined" && cell!=null)
{
if(cell.style.backgroundColor=="blue")
cell.style.backgroundColor="white";
}
}
}
var sn = false;
var A2 = null;
for(i = 0; i < document.all.length; i = i + 1)
{
if (RegString.test(document.all[i].id))
{
//获取查找到的gridView对象
var grid = document.getElementById(document.all[i].id);
//var oTbody = grid.children.item(0);
//循环执行选中操作
for(i = 0; i < grid.all.length; i = i + 1)
{
var RegString = new RegExp("(.*?)" + ctl00_ContentPlaceHolder1_GridView1);
if (RegString.test(grid.all[i].id))
{
if (grid.all[i].checked)
{
sn = true;//说明有选中的
}
}
}
break;
}
}
if (sn)
{
//说明有选中的,可以进行处理....
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkSelect = (CheckBox)e.Row.FindControl("chkSelect");
chkSelect.Attributes.Add("onclick", "show ('" + e.Row.RowIndex + "')");
}
}
//aspx页添加脚本
function show(index)
{
var table = document.getElementById("GridView1");
for(var i = 1; i < table.rows[0].cells.length; i++)
alert (table.rows[index + 1].cells[i].innerText);
return false;
}
1, 单击 checkbox 返回当前行值
2, 外部按钮获取所有选择行的值
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %><%-- http://community.csdn.net/Expert/TopicView3.asp?id=5653656 --%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
LoadProductData();
}
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
if (btnHiddenPostButton != null) {
e.Row.Attributes["onclick"] = String.Format("javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID);
// 额外样式定义
e.Row.Attributes["onmouseover"] = "javascript:this.style.background='red'";
e.Row.Attributes["onmouseout"] = "javascript:this.style.background=''";
e.Row.Attributes["style"] = "cursor:pointer";
e.Row.Attributes["title"] = "单击选择当前行";
}
// 若希望将隐藏按钮单独放于一列,则设置此列隐藏,占位符 <cellIndex> 表示此列索引
//e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none";
}
void LoadProductData()
{
DataTable dt = CreateSampleProductData(); GridView1.DataSource = dt;
GridView1.DataBind();
} #region sample data static DataTable CreateSampleProductData()
{
DataTable tbl = new DataTable("Products"); tbl.Columns.Add("ProductID", typeof(int));
tbl.Columns.Add("ProductName", typeof(string));
tbl.Columns.Add("UnitPrice", typeof(decimal));
tbl.Columns.Add("CategoryID", typeof(int)); tbl.Rows.Add(1, "Chai", 18, 1);
tbl.Rows.Add(2, "Chang", 19, 1);
tbl.Rows.Add(3, "Aniseed Syrup", 10, 2);
tbl.Rows.Add(4, "Chef Anton's Cajun Seasoning", 22, 2);
tbl.Rows.Add(5, "Chef Anton's Gumbo Mix", 21.35, 2);
tbl.Rows.Add(47, "Zaanse koeken", 9.5, 3);
tbl.Rows.Add(48, "Chocolade", 12.75, 3);
tbl.Rows.Add(49, "Maxilaku", 20, 3); return tbl;
} #endregion
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CSDN_AccessGridViewRowByJS</title>
<script type ="text/javascript">
function getRowValue(sender)
{
//debugger;
// if(sender.checked) {
var tblRow = sender.parentNode.parentNode;
// 改变 tblRow.cells[<cellIndex>] 中占位符 <cellIndex> 访问不同单元格
return tblRow.cells[1].innerText + "," + tblRow.cells[2].innerText;
// }
}
function getAllRowValue(grdId, chkIdPart)
{
//debugger;
var tbl = document.getElementById(grdId);
var chkList;
var txt = "";
for(var i = 0; i < tbl.rows.length; i++) {
chkList = tbl.rows[i].getElementsByTagName("input");
for(var j = 0; j < chkList.length; j++) {
if(chkList[j].type == "checkbox" && chkList[j].checked && chkList[j].id.indexOf(chkIdPart) > -1) {
txt += getRowValue(chkList[j]) + "\n";
}
}
}
return txt;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="Button1" value="Rebind" onclick="location.href=location.href;" />
<input type="button" id="Button2" value="Show All Checked Rows's Values" onclick="alert(getAllRowValue('GridView1', 'chkItem1'))" />
<div style="float:left">
<h1>GridView Version</h1>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkItem1" runat="server" onclick="if(this.checked) alert(getRowValue(this))" />
<%--OR--%>
<input type="checkbox" id="chkItem2" onclick="if(this.checked) alert(getRowValue(this))" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductName" >
<ItemTemplate>
<%# Eval("ProductName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
</Columns>
</asp:GridView></div>
</div>
</form>
</body>
</html>
http://www.cnblogs.com/Jinglecat/archive/2007/07/15/818967.html
var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;
for(i=0;i<elm.length;i++)
if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
{
if(elm[i].checked!=xState)
elm[i].click();
}
现在要改成js的。