2016-12-02 4 views
6

Ich möchte eine jQuery Autocomplete-Funktion aber nach einem Klick klicken. In Button-Click-Ereignis sind Gridview und Textbox sichtbar, ansonsten sind sie unsichtbar.Call jquery Autocomplete nach Button Klick Ereignis

Im Folgenden sind mein Code Script

$(document).ready(function() { 
      SearchText(); 
     }); 
     function SearchText() 
     { 
      $(".autosuggest").autocomplete({ 
       source: function (request, response) { 
        $.ajax({ 
         type: "POST", 
         contentType: "application/json; charset=utf-8", 
         url: "CalendarDetails.aspx/GetAutoCompleteData", 
         data: "{'Col3':'" + document.getElementById('txtSearch').value + "'}", 
         dataType: "json", 
         success: function (data) { 
          response(data.d); 
         }, 
         error: function (result) { 
          alert("Error"); 
         } 
        }); 
       } 
      }); 
     } 

HTML

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
      <asp:Label ID="Label4" runat="server" Text="ID" Font-Bold="True"></asp:Label> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> 
      <br /> 
      <br /> 
      <asp:Label ID="Label1" runat="server" Text="Start Date" Font-Bold="True"></asp:Label> 
      <input type="text" id="datepickerStart" runat="server" /> 
      &nbsp; 
     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="datepickerStart" ErrorMessage="*Mandatory field" ForeColor="Red"></asp:RequiredFieldValidator> 
      <br /> 
      <br /> 
      <asp:Label ID="Label2" runat="server" Text="End date" Font-Bold="True"></asp:Label> 
      &nbsp; &nbsp; 
      <input type="text" id="datepickerEnd" runat="server" /> 
      <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="datepickerEnd" ErrorMessage="*Mandatory field" ForeColor="Red"></asp:RequiredFieldValidator> 
      <br /> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 



      <br /> 

EDIT

<input type="text" id="txtSearch" class="autosuggest" /> 
      <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" > 

      <ContentTemplate> 
       <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label> &nbsp;&nbsp;&nbsp; 

       <br /> 
       <br /> 
       <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="20" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound"> 
        <HeaderStyle BackColor="#FFCC99" /> 

       </asp:GridView> 

      </ContentTemplate> 
      <Triggers> 
       <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanging" /> 
       <asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" /> 
      </Triggers> 
     </asp:UpdatePanel> 

     <br /> 
     <br /> 
     <br /> 
    </div> 

-Code hinter

[WebMethod] 
    public static List<string> GetAutoCompleteData(string Col3) 
    { 
     List<string> result = new List<string>(); 
     if ((dtClone != null) && (dtClone.Rows.Count > 0)) 
     { 
      DataRow[] foundRows; 
      string expression = "Col3 LIKE '%" + Col3 + "%'"; 

      // Use the Select method to find all rows matching the filter. 
      foundRows = dtClone.Select(expression); 
      for (int i = 0; i < foundRows.Length; i++) 
       result.Add(foundRows[i][2].ToString()); 
     } 
     return result; 

    } 

Das Problem ist nach Schaltfläche Klick Ereignis Autocomplete (jquery) für Suchvorgang funktioniert nicht. Bitte helfen Sie mir, wo das Problem ist. Wo ich falsch liege

+0

Was 'bedeuten nicht working'? –

+0

versuchen Sie, Ajax-Daten zu ändern: 'data: {Col3: document.getElementById ('txtSearch'). Value}' –

+0

Ich habe oben versucht, aber Auto Complete führt keine Aktion aus. Liegt es daran, dass sich das Textfeld innerhalb des Update-Fensters befindet? –

Antwort

2

Mit Blick auf Ihren Code sieht es so aus, als ob sich Ihr Suchtextfeld außerhalb des Update-Fensters befindet. So soll der folgende Code funktioniert:

$(document).ready(function() { 
    $(".autosuggest").autocomplete({ 
     source: function (request, response) { 
      var col3 = $("#txtSearch").val(); 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "CalendarDetails.aspx/GetAutoCompleteData", 
       data: { Col3: JSON.stringify(col3) }, 
       dataType: "json", 
       success: function (data) { 
        response(data.d); 
       }, 
       error: function (result) { 
        alert("Error"); 
       } 
      }); 
     },  
    }); 
}); 

Bei der Suche Textbox innerhalb des Update-Panel ist, dann wird es über Ajax geladen werden und Sie haben das jquery Ereignis zu einem DOM-Elemente über dem Update-Panel zu binden.

Update-Panel in einem div gewickelt:

<div id="someDivOutsideUpdatePanel"> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" > 
     <input type="text" id="txtSearch" class="autosuggest" /> 
    </asp:UpdatePanel> 
</div> 

Bind das Ereignis an den div:

$(document).ready(function() { 
    $("#someDivOutsideUpdatePanel .autosuggest").autocomplete({ 
     // Same code as above 
    }); 
}); 
+0

Ist es möglich, txtSearch auf Knopfklick Ereignis bei Code zu verbergen/anzuzeigen? –

+0

Zuvor habe ich aspx Textfeld Steuerelement jetzt ich ersetzte es mit HTML-Steuerelement und alles funktioniert jetzt gut. Nur ein Problem Ich möchte diese txtSearch (Textbox) nur zeigen, wenn auf die Schaltfläche geklickt wird und das zu Code hinter ist das möglich? –

+0

Sie können es durch Code hinter, aber ich persönlich bevorzuge jQuery basierte Lösung, wenn dies nur eine UI-basierte Anforderung ist. Überprüfen Sie das Beispiel hier: https://jsfiddle.net/tejsoft/q9s611m5/1/ – TejSoft

Verwandte Themen