简体   繁体   English

在 asp.net c# 中使用 JavaScript 过滤时保存 gridview 的状态

[英]Save state of gridview when using JavaScript filtering in asp.net c#

I'm using javascript to filter my gridview and it works great.我正在使用 javascript 过滤我的 gridview,效果很好。 The problem is when I click on the gridview to edit an item, it postback and the filtered table is now unfiltered.问题是当我单击 gridview 编辑项目时,它回发并且过滤表现在未过滤。 How can I keep the state of the gridview when using the javascript filter?使用 javascript 过滤器时如何保持 gridview 的状态?

Javascript: Java脚本:

<script type="text/javascript">
    function myFunction() {
        //Searcing the table 
        var input, filter, table, tr, td, i, txtValue;
        input = document.getElementById("<%=txtSearch.ClientID %>");
         filter = input.value.toUpperCase();
         table = document.getElementById("<%=gridList.ClientID %>");
        tr = table.getElementsByTagName("tr");

        // Loop through all table rows, and hide those who don't match the search query
        for (i = 0; i < tr.length; i++) {

            td = tr[i].getElementsByTagName("td");

            if (td.length > 0) { // to avoid th 

                //Search the specific column
                if (
                    td[0].innerHTML.toUpperCase().indexOf(filter) > -1 ||
                    td[1].innerHTML.toUpperCase().indexOf(filter) > -1 ||
                    td[2].innerHTML.toUpperCase().indexOf(filter) > -1 ||
                    td[3].innerHTML.toUpperCase().indexOf(filter) > -1 ||
                    td[4].innerHTML.toUpperCase().indexOf(filter) > -1) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
</script>

ASP:售价:

<asp:TextBox ID="txtSearch" runat="server" class="form-control" Visible="true" Width="250px" placeholder="Enter Search Term..." onkeyup="myFunction()"/>

<asp:GridView ID="gridList" runat="server" HorizontalAlign="left" ShowHeader="True" AutoGenerateColumns="false" GridLines="None"
    OnRowEditing="gridList_RowEditing" OnRowCancelingEdit="gridListt_RowCancelingEdit" OnRowUpdating="gridList_RowUpdating">
    <Columns>
        <asp:TemplateField ItemStyle-HorizontalAlign="left" HeaderText="User">
            <ItemTemplate>
                <asp:Label ID="user" Visible="true" runat="server" Text='<%# Eval("User") %> ' />
            </ItemTemplate>

            <EditItemTemplate>
                <asp:TextBox ID="txtUser" class="form-control" runat="server" Text='<%# Eval("User") %> '></asp:TextBox>
            </EditItemTemplate>

        </asp:TemplateField>

        <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" HeaderText="Modify"
            EditText="<span style='font-size: 20px; color: #27ae60;'><span class='glyphicons glyph-edit'></span></span>"
            DeleteText="<span style='font-size: 18px; color: #c0392b;'><span class='glyphicons glyph-bin'></span></span>"
            CancelText="<span style='font-size: 20px; color: #7f8c8d;'><span class='glyphicons glyph-remove-2'></span></span>"
            UpdateText="<span style='font-size: 20px; color: #2980b9;'><span class='glyphicons glyph-floppy-saved'></span></span>" />
    </Columns>
</asp:GridView> 

c# editing a row for example: c# 编辑一行,例如:

protected void gridListt_RowEditing(object sender, GridViewEditEventArgs e)
{
    gridListGiftList.EditIndex = e.NewEditIndex;
    //I need to somehow load the filtered javascript state of the table here rather than the full table
    DataSet dsList = objuser.GetList(0);
    gridList.DataSource = dsList.Tables[0];
    gridList.DataBind();
}

Assuming txtSearch is an ASP.NET control, it should retain its state on a postback.假设 txtSearch 是一个 ASP.NET 控件,它应该在回发时保留其状态。 I'm assuming the search box doesn't get cleared out when you save a row.我假设当您保存一行时搜索框不会被清除。

Why not just run myFunction() on window.onload in addition to the button click event that already runs it.. Then add a conditional in it to check for blank search values before executing your code:除了已经运行它的按钮单击事件之外,为什么不在 window.onload 上运行 myFunction() 。然后在其中添加一个条件以在执行代码之前检查空白搜索值:

    function myFunction() {
    //Searcing the table 
    var input, filter, table, tr, td, i, txtValue;
    input = document.getElementById("txtSearch");
    filter = input.value.toUpperCase();
    table = document.getElementById("gridList");
    tr = table.getElementsByTagName("tr");
    if (input.value != "") {
       for (i = 0; i < tr.length; i++) {
        td = tr[i].getElementsByTagName("td");
        if (td.length > 0) { // to avoid th 
            //Search the specific column
            if (  td[0].innerHTML.toUpperCase().indexOf(filter) > -1 ||
 td[1].innerHTML.toUpperCase().indexOf(filter) > -1 ||
 td[2].innerHTML.toUpperCase().indexOf(filter) > -1 ||
 td[3].innerHTML.toUpperCase().indexOf(filter) > -1 ||
 td[4].innerHTML.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
            } else {
                tr[i].style.display = "none";
            }
        }           
       }
    }
    else {
            for (i = 0; i < tr.length; i++) {
                tr[i].style.display = "";
            }
        }
    }    
    window.onload = myFunction;

Assuming your txtSearch looks like this假设您的txtSearch看起来像这样

<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>

Then you simply have to check in code behind if there is a PostBack and if the TextBox has a Value.然后,如果有 PostBack 以及 TextBox 是否有 Value,您只需检查后面的代码。 Then use ScriptManager to execute myFunction() again.然后再次使用 ScriptManager 执行myFunction() I've placed the code in Page_Load , but you could also place it in gridListt_RowEditing我已将代码放在Page_Load中,但您也可以将其放在gridListt_RowEditing

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack && !string.IsNullOrEmpty(txtSearch.Text))
    {
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "reFilter", "myFunction()", true);
    }
}

However if you are doing self build sorting and filtering stuff on a html table, I would recommend looking into DataTables.net但是,如果您要在 html 表上自行构建排序和过滤内容,我建议您查看DataTables.net

Please try following.请尝试以下。 There should be no post back on grid view.Even edit should happen through api methods via Ajax.网格视图上不应该回发。甚至编辑也应该通过 Ajax 通过 api 方法进行。

As someone mentioned use https://datatables.net/ it is a good approach.正如有人提到的那样,使用https://datatables.net/这是一个很好的方法。 Instead of using gridview use api enabled solution而不是使用 gridview 使用 api 启用解决方案

The other way is when you filter anything save it in a hidden field and read it from server side post back and implement same filtering from server side (harder way).另一种方法是当您过滤任何内容时将其保存在隐藏字段中并从服务器端回发读取它并从服务器端实施相同的过滤(更难的方法)。

I cannot provide code at this time.But I had implemented it in past.我现在无法提供代码。但我过去已经实现过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM