簡體   English   中英

按下 Enter 鍵時 Asp.NET 回發中的 UpdatePanel

[英]UpdatePanel in Asp.NET postback on enter key press

您好,我有一個更新updatepanel設置。 我已將其更新模式設置為有條件的,以便它僅在觸發事件發生時觸發。 在更新updatepanel我將觸發器設置為一些圖像控件,並將事件名稱設置為單擊。 到目前為止一切都很好。

但現在我也有一個textbox設置。 當我輸入一些其他文本並按回車鍵時,它會導致回發並且更新面板會獲得控制權。 即使我已將textboxautoPostBack屬性設置為 false 並將updatePanel UpdateMode設置為 Conditional。 有人可以指出問題。 我究竟做錯了什么? 我只希望updatepanel在此updatePanel發生觸發事件時負責

這是我的updatepaneltextBox代碼。 .

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="ImageButtonLeftArrow" EventName="click" />
                <asp:AsyncPostBackTrigger ControlID="ImageButtonRightArrow" EventName="click" />
            </Triggers>
            <ContentTemplate>

                <asp:AdRotator ID="AdRotator1" runat="server"
                AdvertisementFile="~/MainImages.xml" Height="650" Width="934" 
                onadcreated="AdRotator1_AdCreated" CssClass="borderClassForAdRotator" />

                <asp:UpdateProgress ID="UpdateProgress1" runat="server" >
                    <ProgressTemplate>
                        <div >
                        <img src="images/loading.gif" class="overLayImage" />
                        </div>
                    </ProgressTemplate>
                </asp:UpdateProgress>

            </ContentTemplate>
        </asp:UpdatePanel>
 <asp:TextBox ID="search" CssClass="search" runat="server" AutoPostBack="false" ></asp:TextBox>

您在頁面上也有submit按鈕設置嗎? 按 Enter 會導致表單提交,因此 UpdatePanel 可能正在執行更新。

如果您不確定,請在此處發布您的完整 HTML 和 JS。

更新:此外,如果這是頁面上唯一的文本框,則按 Enter 提交是默認行為。 在這種情況下,添加一個事件處理程序來取消默認行為,如下所示:

JS:

<script type="text/javascript">
    function disableSubmitOnEnter(e)
     {
         var key;      
         if(window.event)
               key = window.event.keyCode; //IE
          else
               key = e.which; //firefox      

         return (key != 13);
     }
</script>

代碼隱藏:

search.Attributes.Add("onkeypress", "return disableSubmitOnEnter();");

如果上述答案不起作用,請使用此單行代碼。

<asp:TextBox ID="search" CssClass="search" runat="server" AutoPostBack="false" onkeydown = "return (event.keyCode!=13);" ></asp:TextBox>

把你的TextBox在一個UpdatePanel和設置AutoPostBack的財產TextBoxtrue。 這種方法對我有用。

對 onkeydown 事件使用 Javascript 函數,如果是回車鍵,則返回 false。 (回車鍵是keycode 13)

if(event.keycode == 13) return false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM