我正在使用常见的JavaScript函数在其父网格视图中展开和折叠子网格视图。我的问题是我想获取父网格视图中当前行的一个单元格的值,或者至少它的行索引。我没有尝试过任何东西似乎工作 - 我一直得到子gridview的当前行的值或行索引。
这是我的aspx与JavaScript函数:
<script language="javascript" type="text/javascript">
function divexpandcollapse(divname)
{
var div = document.getElementById(divname);
var img = document.getElementById('img' + divname);
if (div.style.display == "none")
{
div.style.display = "inline";
img.src = "minus.gif";
}
else
{
div.style.display = "none";
img.src = "plus.gif";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<input id="Hidden1" type="hidden" runat="server" value="" />
<asp:GridView ID="gvOrders" runat="server" DataKeyNames="OrderID" Width="750px" AutoGenerateColumns="False" OnRowDataBound="gvOrders_RowDataBound" GridLines="None"
BorderStyle="Solid" BorderWidth="3px" BorderColor="Blue" style="z-index: 100; left: 126px; position: absolute; top: 96px" BackColor="White" OnRowCommand="gvOrders_RowCommand"
OnSelectedIndexChanged="gvOrders_SelectedIndexChanged">
<RowStyle BackColor="#E1E1E1" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="LightBlue" Font-Bold="True" ForeColor="Black" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("OrderID") %>');">
<img id="imgdiv<%# Eval("OrderID") %>" width="9px" src="plus.gif" alt =""/>
</a>
</ItemTemplate>
<ItemStyle Width="20px" />
<ControlStyle BackColor="Black" />
</asp:TemplateField>
<asp:BoundField DataField="OrderID" HeaderText="Order ID" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="CustomerID" HeaderText="Customer ID" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Company" HeaderText="Company" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="OrderDate" HeaderText="Order Date" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ShippingDate" HeaderText="Shipping Date" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("OrderID") %>" style="display: none; position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Double" BorderColor="#df5015" GridLines="None" Width="700px"
OnRowCancelingEdit="gvChildGrid_RowCancelingEdit" OnRowDeleting="gvChildGrid_RowDeleting" OnRowEditing="gvChildGrid_RowEditing" OnRowUpdating="gvChildGrid_RowUpdating" >
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<RowStyle BackColor="#E1E1E1" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="CoreID">
<ItemTemplate>
<asp:Label ID="lblCoreID" runat="server" Text='<%# Bind("CoreID") %>'></asp:Label>
<asp:TextBox ID="tbxCoreID" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("CoreID") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbxCoreID" runat="server" Text='<%# Bind("CoreID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BoxID">
<ItemTemplate>
<asp:Label ID="lblBoxID" runat="server" Text='<%# Bind("BoxID") %>'></asp:Label>
<asp:TextBox ID="tbxBoxID" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("BoxID") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbxBoxID" runat="server" Text='<%# Bind("BoxID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CoresPerBox">
<ItemTemplate>
<asp:Label ID="lblCoresPerBox" runat="server" Text='<%# Bind("CoresPerBox") %>'></asp:Label>
<asp:TextBox ID="tbxCoresPerBox" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("CoresPerBox") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbxCoresPerBox" runat="server" Text='<%# Bind("CoresPerBox") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PalletID">
<ItemTemplate>
<asp:Label ID="lblPalletID" runat="server" Text='<%# Bind("PalletID") %>'></asp:Label>
<asp:TextBox ID="tbxPalletID" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("PalletID") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbxPalletID" runat="server" Text='<%# Bind("PalletID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BoxesPerPallet">
<ItemTemplate>
<asp:Label ID="lblBoxesPerPallet" runat="server" Text='<%# Bind("BoxesPerPallet") %>'></asp:Label>
<asp:TextBox ID="tbxBoxesPerPallet" runat="server" Style="z-index: 100; left: 0px; position: relative" Visible="false" Text='<%# Bind("BoxesPerPallet") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbxBoxesPerPallet" runat="server" Text='<%# Bind("BoxesPerPallet") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowCancelButton="True" CancelText="Cancel" HeaderText="Edit Controls" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#8080FF" />
</asp:GridView>
我的部分代码背后:
protected void gvChildGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gv = (GridView)gvOrders.Rows[e.NewEditIndex].FindControl("gvChildGrid");
GridViewRow parentRow = (GridViewRow)gv.Parent.Parent;
int indexRow = int.Parse(parentRow.RowIndex.ToString().Trim()); //gvOrders.Rows[e.NewEditIndex].Cells[1].Text.ToString().Trim());
//int indexRow = parentRow.RowIndex;
我尝试过使用隐藏字段,在gridview模板字段中获取已评估变量的值,以及其他几种方法。由于扩展/崩溃是由JavaScript处理的,我无法弄清楚如何通过“普通”方式访问父网格视图,例如_IndexChanged或_RowCommand。
为了方便起见,我只是将我试图在_RowEditing方法中使用的代码卡住了,直到我弄清楚如何使其工作。我知道通过这样做,e属于儿童gridview。两个注释掉的行是命令,它们不起作用或只是抓住子gridview的当前行。
我承认我对C#相对较新(但不是编程),并意识到我可能只是遗漏了一些东西。但我不能为我的生活弄清楚是什么。
答案 0 :(得分:0)
也许这可以解决您的问题: 使用RowDataBound事件,您可以访问服务器端的每一行:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
((Label)e.Row.FindControl("myLabelId")).Attributes.Add("data-customAttribute", ((Order)e.Row.DataItem).OrderNo);
}
}
您可以看到可以将标签(或任何控件)添加到TemplatedField(ID为myLabelId的ea),然后您可以将html属性添加到该标签,或根据您的数据进行所需的任何更改。在示例数据中-customAttribute添加到控件中。您甚至可以添加onclick属性以了解该行是否为当前行。您可以从e.Row.DataItem访问数据,然后必须将其强制转换为数据对象(例如,订单)。
使用Jquery,您可以访问clientSide中的标签:
// get all rows in table including header
var $rows=$('#<%=gvOrders.ClientID%>').find('tr');
// access the innerHTML of Label control we created before which contains the OrderNo
var OrderNo=$(aRow).find('#myLabelId').html();
请注意,ClientID返回任何服务器控件的html Id,您可以通过document.getElementById或$('#id')访问它。
我希望这些提示可以帮到你。