【问题标题】:Search not working on Gridview搜索在 Gridview 上不起作用
【发布时间】:2014-11-21 15:43:24
【问题描述】:

我已经在 gridview 上搜索来自this 的列 关联。我按照我的要求实施了。但它对我不起作用。请参阅代码供您参考:-

 <asp:GridView ID="grdCSRPageData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3" class="hoverTable" AutoGenerateColumns="false" AllowPaging="True" BackColor="#E5E5E5" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" ForeColor="Black" GridLines="Vertical" ShowFooter="true" PageSize="5" OnPageIndexChanging="grdCSRPageData_PageIndexChanging" OnRowCancelingEdit="grdCSRPageData_RowCancelingEdit" OnRowEditing="grdCSRPageData_RowEditing" OnRowUpdating="grdCSRPageData_RowUpdating" OnRowDeleting="grdCSRPageData_RowDeleting" OnRowCommand="grdCSRPageData_RowCommand" OnDataBound="grdCSRPageData_DataBound">
                <AlternatingRowStyle BackColor="#CCCCCC" />

                <Columns>
                    <asp:BoundField DataField="page_title" HeaderText="Page Title" ItemStyle-Width="30" />
                    <asp:BoundField DataField="page_description" HeaderText="Page Description" ItemStyle-Width="30" />
                    <asp:BoundField DataField="meta_title" HeaderText="Meta Title" ItemStyle-Width="30" />
                    <asp:BoundField DataField="meta_keywords" HeaderText="Meta Keywords" ItemStyle-Width="30" />
                    <asp:BoundField DataField="meta_description" HeaderText="Meta Description" ItemStyle-Width="30" />
                    <asp:CheckBoxField DataField="Active" HeaderText="Active" ItemStyle-Width="15" />
                    <asp:TemplateField HeaderText="Edit/Delete" HeaderStyle-Width="15%">
                        <ItemTemplate>
                            <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" Text="Edit" />
                            <span onclick="return confirm('Are you sure want to delete?')">
                                <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete"></asp:LinkButton>
                            </span>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
                            <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
                        </EditItemTemplate>
                        <HeaderTemplate>
                            <asp:Button ID="btnInsertRecord" runat="server" Text="Add" CommandName="Insert" />
                        </HeaderTemplate>
                        <HeaderStyle Width="15%"></HeaderStyle>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

另见 JS 脚本

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>          
<script type="text/javascript" src="js/quicksearch.js"></script>
<script type="text/javascript">
    $(function () {
        $('.form-control').each(function (i) {
            $(this).quicksearch("[id*=grdCSRPageData] tr:not(:has(th))", {
                'testQuery': function (query, txt, row) {
                    return $(row).children(":eq(" + i + ")").text().toLowerCase().indexOf(query[0].toLowerCase()) != -1;
                }
            });
        });
    });
</script>

另请参阅您的 ref 背后的代码:-

  protected void grdCSRPageData_DataBound(object sender, EventArgs e)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
        for (int i = 0; i < grdCSRPageData.Columns.Count; i++)
        {
            TableHeaderCell cell = new TableHeaderCell();
            TextBox txtSearch = new TextBox();
            txtSearch.Attributes["placeholder"] = grdCSRPageData.Columns[i].HeaderText;
            txtSearch.CssClass = "form-control";
            cell.Controls.Add(txtSearch);
            row.Controls.Add(cell);
        }
        grdCSRPageData.HeaderRow.Parent.Controls.AddAt(1, row);
    }

网格的变化

 protected void grdUser_DataBound(object sender, EventArgs e)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
        for (int i = 0; i < grdUser.Columns.Count; i++)
        {
            TableHeaderCell cell = new TableHeaderCell();
            TextBox txtSearch = new TextBox();
            txtSearch.Attributes["placeholder"] = grdUser.Columns[i].HeaderText;
            txtSearch.CssClass = "form-control HaydaBre";

            if (grdUser.Columns[i].HeaderText != "Action" && grdUser.Columns[i].HeaderText != "" && grdUser.Columns[1].HeaderText != "Select") // && grdUser.Columns[i].HeaderText != "" && grdUser.Columns[i].HeaderText != null && grdUser.Columns[i].HeaderText != "Select")
            {
                cell.Controls.Add(txtSearch);
            }
            row.Controls.Add(cell);
        }
        grdUser.HeaderRow.Parent.Controls.AddAt(1, row);
    }

当我添加HeaderText != Select。它对第一列停止工作,但对另一列有效

【问题讨论】:

  • 任何工作方式?
  • 由于这是一个客户端实现,例如使用Firebug 调试了您的代码。那么请在此处分享错误消息。您的代码对我有用...
  • @wooer: 错误信息,你的意思是说是控制台错误?
  • 是的,控制台消息。
  • @wooer:我有两个错误。 1)TypeError: $(...).sortable is not a function $('.sortable').sortable(); 2)TypeError: $(...).quicksearch is not a function $(this).quicksearch("[id*=grdCSRPageData] tr:not(:has(th))", {

标签: javascript jquery asp.net gridview


【解决方案1】:

表单控件类有很多元素。那么您可以将您的 C# 代码更改为:

protected void grdCSRPageData_DataBound(object sender, EventArgs e)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
        for (int i = 0; i < grdCSRPageData.Columns.Count; i++)
        {
            TableHeaderCell cell = new TableHeaderCell();
            TextBox txtSearch = new TextBox();
            txtSearch.Attributes["placeholder"] = grdCSRPageData.Columns[i].HeaderText;
            txtSearch.CssClass = "form-control HaydaBre";
            cell.Controls.Add(txtSearch);
            row.Controls.Add(cell);
        }
        grdCSRPageData.HeaderRow.Parent.Controls.AddAt(1, row);
    }

你的 JS 代码是:

<script type="text/javascript">
    $(function () {
        $('.HaydaBre').each(function (i) {
            $(this).quicksearch("[id*=grdCSRPageData] tr:not(:has(th))", {
                'testQuery': function (query, txt, row) {
                    return $(row).children(":eq(" + i + ")").text().toLowerCase().indexOf(query[0].toLowerCase()) != -1;
                }
            });
        });
    });
</script> 

【讨论】:

  • 感谢wooer,非常感谢您的帮助。它现在对我来说就像魅力..!! :)
  • 这就是为什么我们都在这里.. 很高兴听到我提供了帮助。拥有一位欣赏员工的经理也一定很棒:)谢谢。祝你有美好的一天,并注意 css 类名:)
  • Firebug 会说什么吗?
  • 我没有检查。我对代码隐藏进行了一项更改,但它停止了工作
  • 而变化是.. ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 2014-06-15
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多