2016-12-02 123 views
6

我想調用jquery自動完成功能,但按下按鈕後。在按鈕點擊事件gridview和文本框是可見的,否則它們是不可見的。按鈕點擊事件後調用jquery自動完成

下面是我的代碼 腳本

$(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 /> 

編輯

<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> 

代碼背後

[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; 

    } 

問題出在搜索操作的按鈕點擊事件自動完成(jquery)不起作用。 請幫我解決問題。我錯在哪裏

+0

什麼意思'是不是working'? –

+0

嘗試將ajax數據更改爲:data:{Col3:document.getElementById('txtSearch')。value}' –

+0

我已經在上面嘗試過了,但auto complete不會執行任何操作。是因爲更新面板內的文本框? –

回答

2

看着你的代碼,它看起來像你的搜索文本框在更新面板之外。所以下面的代碼應工作:

$(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"); 
       } 
      }); 
     },  
    }); 
}); 

在情況下,搜索文本框是更新面板,然後它會通過AJAX加載裏面,你必須jQuery的事件綁定更新面板上方的DOM元素。

更新面板包裹一個div內:

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

綁定事件到div:

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

是否有可能隱藏/顯示txtSearch上的按鈕點擊事件後面的代碼? –

+0

此前我已經使用aspx文本框控件,現在我用html控件替換它,現在所有工作都正常。只有一個問題,我想顯示這個txtSearch(文本框)只有當按鈕被點擊,並在代碼後面是可能的? –

+0

你可以通過後面的代碼來實現,但我個人更喜歡基於jQuery的解決方案,如果這只是一個基於UI的需求。檢查這裏的例子:https://jsfiddle.net/tejsoft/q9s611m5/1/ – TejSoft

相關問題