Gridview Master/Detail JS
首先:编写Gridview的RowCreated事件处理方法:
1 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
2 {
3 if (e.Row.RowType == DataControlRowType.DataRow)
4 {
5 string RowID = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ProjectId"));
6 string Url = "CertificateList.aspx?ID=" + RowID;
7 e.Row.Attributes.Add("href", Url); // 当前记录的消息信息
8 e.Row.Attributes.Add("open", "0"); // 展开open=1 折叠:0
9 e.Row.Attributes.Add("hascontent", "0"); // 是否打展开过。
10 e.Row.Cells[2].Attributes.Add("onclick", "javascript:DrillDownOrUpX(this);");
11 }
12 }
13 }
DrillDownOrUpx用来处理详细表弹出内容:
2 {
3 if (e.Row.RowType == DataControlRowType.DataRow)
4 {
5 string RowID = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ProjectId"));
6 string Url = "CertificateList.aspx?ID=" + RowID;
7 e.Row.Attributes.Add("href", Url); // 当前记录的消息信息
8 e.Row.Attributes.Add("open", "0"); // 展开open=1 折叠:0
9 e.Row.Attributes.Add("hascontent", "0"); // 是否打展开过。
10 e.Row.Cells[2].Attributes.Add("onclick", "javascript:DrillDownOrUpX(this);");
11 }
12 }
13 }
function DrillDownOrUpX(This)
{
var oRow = GetParentObject(This, "TR");
var RowIndex = RowObjectToIndex(oRow)
var oTable = GetParentObject(This, "TABLE");
//var oPlusMinus = oRow.firstChild.childNodes[4];
if(oRow.open == "1")
{
var DetailsRow = oTable.rows[RowIndex+1];
DetailsRow.style.display="none";
oRow.open = "0";
return(false);
}
if(oRow.hascontent == "1")
{
var DetailsRow = oTable.rows[RowIndex+1];
DetailsRow.style.display="block";
oRow.open = "1";
return(false);
}
var ColumnCount = oRow.cells.length;
// need to add the row
var NewRow = oTable.insertRow(RowIndex+1);
var NewCell = NewRow.insertCell(0);
NewCell.setAttribute("colSpan", ColumnCount.toString());
var CellContent =
"<table cellpadding='0' cellspacing='0'>"+
"<tr><td><iframe src='"+oRow.href+"' frameborder='0' width='100%' height='200'></iframe></td></tr>"+
"</table>";
NewCell.innerHTML = CellContent;
oRow.open = "1";
oRow.hascontent = "1";
return(false);
}
而该方法中调用了以下两个方法:{
var oRow = GetParentObject(This, "TR");
var RowIndex = RowObjectToIndex(oRow)
var oTable = GetParentObject(This, "TABLE");
//var oPlusMinus = oRow.firstChild.childNodes[4];
if(oRow.open == "1")
{
var DetailsRow = oTable.rows[RowIndex+1];
DetailsRow.style.display="none";
oRow.open = "0";
return(false);
}
if(oRow.hascontent == "1")
{
var DetailsRow = oTable.rows[RowIndex+1];
DetailsRow.style.display="block";
oRow.open = "1";
return(false);
}
var ColumnCount = oRow.cells.length;
// need to add the row
var NewRow = oTable.insertRow(RowIndex+1);
var NewCell = NewRow.insertCell(0);
NewCell.setAttribute("colSpan", ColumnCount.toString());
var CellContent =
"<table cellpadding='0' cellspacing='0'>"+
"<tr><td><iframe src='"+oRow.href+"' frameborder='0' width='100%' height='200'></iframe></td></tr>"+
"</table>";
NewCell.innerHTML = CellContent;
oRow.open = "1";
oRow.hascontent = "1";
return(false);
}
//取得当前对象的父对象。
function GetParentObject(o, tagName)
{
srcElem = o;
while (srcElem.tagName != tagName)
srcElem = srcElem.parentElement;
return(srcElem);
}
//取得当前行行索引号。
function RowObjectToIndex(oTR)
{
if(oTR == null)
return(-1);
var oTABLE = GetParentObject(oTR, "TABLE");
var i;
for(i=0; i<oTABLE.rows.length; i++)
{
if(oTABLE.rows[i] == oTR)
{
return(i);
}
}
}
function GetParentObject(o, tagName)
{
srcElem = o;
while (srcElem.tagName != tagName)
srcElem = srcElem.parentElement;
return(srcElem);
}
//取得当前行行索引号。
function RowObjectToIndex(oTR)
{
if(oTR == null)
return(-1);
var oTABLE = GetParentObject(oTR, "TABLE");
var i;
for(i=0; i<oTABLE.rows.length; i++)
{
if(oTABLE.rows[i] == oTR)
{
return(i);
}
}
}