简体   繁体   English

如何通过TextBox控件触发UpdatePanel?

[英]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. 我想通过搜索字段的keydown触发更新,所以我用一个单击按钮的jQuery语句“伪装”它。 I'm wondering... there must be a better way... right!? 我想知道......一定有更好的方法......对吧!?

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) 您只需要为updatepanel提供一个ID(此处为updatePanel)

Execute that code on a keyup or whenever you are ready for it. 在密钥上或在您准备好的时候执行该代码。

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/ http://remy.supertext.ch/2007/06/see-search-results-as-you-type-an-aspnet-ajax-control/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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