【问题标题】:JQuery Search on Specific Gridview Columns when Using BoundField使用 BoundField 时在特定 Gridview 列上进行 JQuery 搜索
【发布时间】:2017-12-26 11:10:48
【问题描述】:

我是一家公司的实习生,该公司给我的任务是创建一个可以访问和操作公司数据库的网站。它允许同事轻松研究客户正在使用的代码。以下问题难倒了其他开发人员和我。

目前,jQuery 搜索在没有按钮或单击“全部”按钮时有效,并搜索整个表格。其他按钮尚未搜索。它确实识别按钮,因此它输入 if/else if 语句,但不识别按钮的名称/类。

在从下拉列表中选择名称并为每一列使用 get/set 方法后,GridView 使用 GrdiView.DataSource 和 DataBind() 将数据绑定到它们。因此,我将 BoundField 与 DataField 属性一起使用,而不是 TemplateField。

问题:有没有办法为 BoundField 分配名称/类/值,以便 jQuery 可以找到列单元格?或者,有没有办法让 TemplateField 工作?

$(document).on("keyup", function ()
{
    SearchGrid('<%=txtSearchBox.ClientID%>', '<%=grdIniData1.ClientID%>', 
    '<%=grdIniData2.ClientID%>');

    function SearchGrid(txtSearch, grd1, grd2)
    {           
      if ($("input:radio[name=Section]").is(":checked"))
        {
        //Tried adding a CSS class to BoundField 'Section' and calling
        $('.Section').each(function (i)
        {
            alert("hello");
            $("Section").quicksearch("[id*=grdList] tr:not(:has(th))",
            {
               'testQuery': function (query, txt, row)
                {
                  return $(row).children(":eq(" + i")").text().toLowerCase().
                  indexOf(query[0].toLowerCase()) != -1;
                }
             });
          });
      }

     else if ($("input:radio[name=Name]").is(":checked"))
     {
        //For name and IniValue, tried accessing by BoundField name
        //Unsure how to name BoundField so jQuery can access
        $('#name').each(function ()
        {
           if ($("Name").find('td').eq(1).text() != "")
           {
              $(this).index();
           }
         });
      }
      else if ($("input:radio[name=IniValue]").is(":checked"))
      {
         var searchKey = $("[id *=" + txtSearch + " ]").val().toLowerCase();
         $("#grd1 tr td:nth-child(2)").each(function ()
         {
            var cellText = $(("input:radio[name=IniValue]").
            is(":checked")).text().toLowerCase();
            if (cellText.indexOf(searchKey) >= 0)
            {
               $("[id *=" + grd1 + " ]").parent().show();
            }
            else
            {
               $("[id *=" + grd1 + " ]").parent().hide();
            }
         });
      }
      else 
      {
         //Code is functional after this point
       }

<asp:GridView ID="grdIniData2" runat="server" AllowSorting="True" AutoGenereateColumns="false" AutoSizeColumnsMode="AllCellsExceptHeader" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="0" GridLines="Vertical" OnRowDataBound="grdIniData2_RowDataBound" OnSelectedIndexChanged="ddlClientList2_SelectedIndexChanged" OnSorting="grdIniData2_Sort" Font-Bold="True" Font-Names="Sylfaen" Font-Size="24px">
    <Columns>
        <asp:BoundField DataField="Section" HeaderText="Section" SortExpression="Section" ControlStyle-CssClass="Section" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="INIValue" HeaderText="INIValue" SortExpression="IniValue" />
        <asp:BoundField DataField="Description" HeaderText="Description" />
        <asp:BoundField DataField="Exposed" HeaderText="Exposed" />
        <asp:BoundField DataField="DataType" HeaderText="DataType" />
        <asp:BoundField DataField="DataFormat" HeaderText="DataFormat" />
    </Columns>
</asp:GridView>

【问题讨论】:

    标签: c# jquery css asp.net gridview


    【解决方案1】:

    您可以将类分配给 BoundField。

    <asp:BoundField DataField="DataFormat" HeaderText="DataFormat"
        ItemStyle-CssClass="MyCLass"
        HeaderStyle-CssClass="MyHeaderCLass" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2023-03-29
      相关资源
      最近更新 更多