Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger an UpdatePanel by a TextBox control?

Consider the following code:

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

The button will trigger an update of the panel. I wanted to trigger an update by a keydown of the search field, so I'm 'faking' it with a jQuery statement that clicks the button. I'm wondering... there must be a better way... right!?

like image 887
Kees C. Bakker Avatar asked Nov 23 '11 21:11

Kees C. Bakker


2 Answers

You can do this to refresh your updatepanel without the button:

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

You just need to give your updatepanel an ID (updatePanel here)

Execute that code on a keyup or whenever you are ready for it.

like image 80
Erik Dekker Avatar answered Oct 06 '22 19:10

Erik Dekker


The link is a bit outdates, but should pretty much do what you want:
http://remy.supertext.ch/2007/06/see-search-results-as-you-type-an-aspnet-ajax-control/

like image 34
Remy Avatar answered Oct 06 '22 21:10

Remy