2011-11-23 96 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合并为一个控件,因此它可以作为触发选项一起工作。 –