使用Javascript获取Gridview一行中特定项的值
本文关键字:一行 获取 Javascript Gridview 使用 | 更新日期: 2023-09-27 18:08:24
我有这个Gridview:
<asp:GridView ID="GridViewSearch" runat="server" DataKeyNames="ID" RowStyle-HorizontalAlign="Center" AutoGenerateColumns="False" DataSourceID="SqlDataSourceSearch" >
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" InsertVisible="False" ReadOnly="True" Visible="False"></asp:BoundField>
<asp:BoundField DataField="SURNAME" HeaderText="SURNAME" SortExpression="SURNAME"></asp:BoundField>
<asp:BoundField DataField="FIRSTNAME" HeaderText="FIRSTNAME" SortExpression="FIRSTNAME"></asp:BoundField>
<asp:CommandField ShowDeleteButton="True"></asp:CommandField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonPrint" runat="server" ImageUrl="~/images/printSmall.png" CssClass="addbuttons" OnClientClick = "return PrintPanel();" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
在最后一列我有图像按钮"ImageButtonPrint"。我将运行以下Js:
<script type = "text/javascript">
function PrintPanel() {
var name = '<%# Eval("SURNAME")+Eval("FIRSTNAME") %>';//this is not working
var printWindow = window.open('', '', 'height=800,width=400');
printWindow.document.write(name);
printWindow.document.close();
setTimeout(function () {
printWindow.print();
}, 500);
return false;
}
</script>
问题是我无法从下拉列表
的行中获得姓氏+ firstname的等效值 var name = '<%# Eval("SURNAME")+Eval("FIRSTNAME") %>';
这是不工作。有解决方案吗?
尝试在函数中传递姓氏和FIRSTNAME,如
OnClientClick='<%#String.Format(“return PrintPanel('{0}','{1}')”, Eval(“SURNAME”), Eval(“FIRSTNAME”)) %>’
并将函数定义更改为
function PrintPanel(Surname,Firstname) {
var name =Surname+' '+Firstname;
另一种选择:
首先,您需要将控件作为参数传递,因此您的asp:ImageButton
的OnClientClick
属性将像这样
<asp:ImageButton ID="ImageButtonPrint" ... OnClientClick = "return PrintPanel(this);" />
现在在函数中接收它我们将使用parentNode属性直到我们获得<tr>
元素然后在单元格中获得你想要的文本例如数组
function PrintPanel(element) {
var name = element.parentNode.parentNode.cells[1].innerHTML +
' ' + element.parentNode.parentNode.cells[2].innerHTML;
// If you want to verify the var
// alert(name);
var printWindow = window.open('', '', 'height=800,width=400');
printWindow.document.write(name);
printWindow.document.close();
setTimeout(function () {
printWindow.print();
}, 500);
return false;
}
我认为这段代码比另一段更清晰