2011-11-23 94 views
6

考慮下面的代碼:如何通過TextBox控件觸發UpdatePanel?

<label>Search:</label><asp:TextBox runat="server" ID="search" ClientIDMode="Static" OnKeyUp="$('#searchButton').click();" /><asp:Button runat="server" ID="searchButton" ClientIDMode="Static" /> 
<asp:UpdatePanel runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
     <asp:GridView runat="server" DataSourceID="EntityDataSource1" 
      AllowPaging="True" AllowSorting="True" AutoGenerateColumns="true" PageSize="20" 
      Width="400" /> 
    </ContentTemplate> 
    <Triggers> 
     <asp:AsyncPostBackTrigger ControlID="searchButton" /> 
    </Triggers> 
</asp:UpdatePanel> 

該按鈕將觸發面板的更新。我想通過搜索字段的關鍵字來觸發更新,所以我用一個單擊按鈕的jQuery語句「僞裝」它。我想知道...必須有更好的方式......對!

+0

可能這會幫助你 http://stackoverflow.com/questions/1009086/how-to-make-an-asp-net-textbox-fire-its-ontextchanged-event-fire-in-an -ajax-upd – coder

+0

@Kiran搜索字段在更新面板之外,這使得它不同於問題1009086,因爲如果我使用自動提交,頁面將被提交。也許雷米是對的,我應該寫我自己的控制它... –

+0

是的,我也同意。因爲我現在也在做同樣的事情可能是他的權利。 – coder

回答

3

你可以做到這一點刷新UpdatePanel的無按鈕:

<script type="text/javascript"> 

    function refreshPanel() { 
     __doPostBack('<%= updatePanel.UniqueID %>', ''); 
    } 

</script> 
<label>Search:</label> 
<asp:TextBox runat="server" ID="search" 
       ClientIDMode="Static" OnKeyUp="refreshPanel();" /> 
<asp:UpdatePanel runat="server" ID="updatePanel"> 

你只需要給你的更新面板一個ID(更新面板在這裏)

執行密碼上的代碼或任何時候你準備好了。

+0

它提交頁面。我想做一個AJAX回發,導致不刷新頁面。 –

+0

原來的<% %>不能設置爲OnKeyUp屬性:( –

+0

@ KeesC.Bakker:只需將代碼放在一個單獨的函數中,然後在你的鍵盤中調用該函數。 –

1

的聯繫是有點outdates,但應該差不多做你想要什麼:
http://remy.supertext.ch/2007/06/see-search-results-as-you-type-an-aspnet-ajax-control/

+0

嗯......我很希望.net會在它的工具箱裏有一些聰明的東西...... –

+0

不是我知道的,但是我的代碼幾乎可以滿足你的需求,不是嗎? – Remy

+0

是的。我想將DelayedSubmitExtender和TextBox合併爲一個控件,因此它可以作爲觸發選項一起工作。 –