我有这个 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>
问题是我无法从下拉列表的行中获取姓氏+名字的等效值
var name = '<%# Eval("SURNAME")+Eval("FIRSTNAME") %>';
这不起作用。 有什么解决办法吗?
最佳答案
尝试在函数中传递 SURNAME 和 FIRSTNAME ,例如
OnClientClick='<%#String.Format(“return PrintPanel('{0}','{1}')”, Eval(“SURNAME”), Eval(“FIRSTNAME”)) %>’
并更改您的函数定义,例如
function PrintPanel(Surname,Firstname) {
var name =Surname+' '+Firstname;
关于javascript - 使用 Javascript 获取 Gridview 行中特定项目的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32052447/