【问题标题】:Get value of a specific item in a row of Gridview with Javascript使用Javascript获取Gridview行中特定项目的值
【发布时间】:2015-08-17 14:03:46
【问题描述】:

我有这个 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>

问题是我无法从下拉列表的行中获取 surname + firstname 的等效值

   var name = '<%# Eval("SURNAME")+Eval("FIRSTNAME") %>';

这不起作用。 有什么解决办法吗?

【问题讨论】:

    标签: javascript c# asp.net gridview


    【解决方案1】:

    尝试在您的函数中传递 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;
    

    【讨论】:

      【解决方案2】:

      另一种选择:

      首先您需要将控件作为参数传递,因此您的asp:ImageButtonOnClientClick 属性将是这样的

      <asp:ImageButton ID="ImageButtonPrint" ... OnClientClick = "return PrintPanel(this);" />
      

      现在在函数中接收它,我们将使用parentNode property 直到我们获得&lt;tr&gt; 元素,然后在数组中获取您想要的单元格内的文本

      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;
      }
      

      我觉得这段代码比其他的更清晰

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-13
        • 1970-01-01
        • 2016-03-16
        • 1970-01-01
        • 1970-01-01
        • 2018-05-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多