简体   繁体   中英

Postback event on text changed event

I want to postback on every keypress/textchanged event in textbox but my javascript is not running. Am i missing something...????

My Html textbox is this:

<asp:TextBox ID="Txt_Search" runat="server" AutoCompleteType="Disabled" 
  onkeypress="Postback();" OnTextChanged="Txt_Search_TextChanged" AutoPostBack="true">
</asp:TextBox>

My Javascript code is this:

    function Postback() {
    __doPostBack('<%= Txt_Search.ClientID %>', '');
};

My on textchanged event is this:

 protected void Txt_Search_TextChanged(object sender, EventArgs e)
    {
        FolderStructure.Nodes.Clear();
        Searchtxt();
    }

如下修改代码并检查

__doPostBack('Txt_Search', '');

here is demo for dynamic search in asp.net.

Your textbox should be:

<asp:TextBox ID="PopertyName" placeholder="Property Name" href="#"
                                propertyid="" runat="server" class="dropdownAnchor"
                                autocomplete="off" onkeydown="SearchProperty()"></asp:TextBox>

here we will call a function named searchProperty() onkeydown event.

so, your ajax should be,

function SearchProperty() {                            
                        $.ajax({
                            url: '<%=Page.ResolveUrl("~/Dashboard/NewDashboard.aspx/GetProperty") %>',
                            data: "{ 'prefix': '" + $("#PopertyName").val() + "'}",
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            success: function (data) {
                               //Your code to bind result that received from your back end.
                            },
                            error: function (response) {
                                alert(response.responseText);
                            },
                            failure: function (response) {
                                alert(response.responseText);
                            }
                        });
                    };

these function will call a GetPoprty() method on newDashboard.aspx.cs file.

so my back end method is,

[WebMethod]
public static List<Model.Property> GetProperty(string prefix)
{
   // Here you get your text in prefix 
 }

I create sample projects ASP.NET WebForms and AJAX.

I Suggest you can use AJAX process .NET Generic Handler File. Please you research about Generic Handler file.

Before create Generic Handler file "Search.ashx" and Paste above the code.

            public void ProcessRequest(HttpContext context)
    {

        var search = HttpContext.Current.Request.Form["term"];

        //Dummy Data
        List<string> searchList = new List<string> { "Red", "Orange", "Ping", "Blue", "White", "Black" };

        string result = string.Empty;

        if (!string.IsNullOrEmpty(search))
        {
            searchList = searchList.Where(x => x.ToLower().Contains(search.ToLower())).ToList();

            if (searchList != null && searchList.Count() > 0)
            {
                foreach (var item in searchList)
                {

                    result += "<li>" + item + "</li>";

                }
            }
        }
        else
        {
            result="<li> Not Found </li>";
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write(result);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

and Create Your Search Page, YourFile.aspx, my file name Search.aspx.

ASPX Page Code :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication7.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="txtSearch" class="js-search" value="Search" />

        <div class="searchResult">
            <ul>


            </ul>
        </div>

    </div>
    </form>

    <script src="https://code.jquery.com/jquery-2.2.3.min.js" type="text/javascript"></script>

    <script>

        $(function () {
            $(".js-search").on('keyup', function () {

                var term = $('.js-search').val();
                if (term.length > 2) {
                    sendSearchRequest({ "term": term });
                }
            });

            function sendSearchRequest(value) {

                var datas = $.ajax({
                    type: "POST",
                    url: '/Search.ashx',
                    cache: false,
                    async: false,
                    data: value,
                    success: function (term) {
                        $('.searchResult').empty();
                        $('.searchResult').append(term);
                    }

                });

            }
        });
    </script>
</body>
</html>

This example when all three letters entered send ajax request search.ashx file and contains search term and get result on search page.

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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