javascript - 使用 Javascript 获取 Gridview 行中特定项目的值

标签 javascript c# asp.net gridview

我有这个 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(&#39;{0}&#39;,&#39;{1}&#39;)”, Eval(“SURNAME”),  Eval(“FIRSTNAME”)) %>’ 

并更改您的函数定义,例如

function PrintPanel(Surname,Firstname) {

        var name =Surname+' '+Firstname;

关于javascript - 使用 Javascript 获取 Gridview 行中特定项目的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32052447/

相关文章:

javascript - JavaScript 中符号 "+"的含义?

asp.net - 在 owin MVC5 中正确关闭 session

c# - 将文件拖放到 WPF 窗口中

成功提交 Web 表单后,Javascript 重置功能将不起作用

c# - 如何用另一个表/列表中的匹配值替换 id?

javascript - 如何在 React/Redux 中取消挂起的异步操作

javascript - 对逗号赋值的困惑

javascript - Firebase 登录问题

c# - 从字符串列表中查找最大时间

c# - Dispatcher.BeginInvoke 问题