C# – UpdatePanel in Asp.NET postback on enter key press

asp.netasp.net-ajaxcupdatepanel

Hello there I have an updatepanel setup. I have set its update mode to be conditional so that it only fires when the trigger events happens. Inside the updatepanel I have set the triggers to be some image controls and the event name to be click. Everything's fine till now.

But now I have also a textbox setup. When ever I enter some other text and press enter it causes the postback and the update panel takes control. Even though I have set the autoPostBack property of the textbox to be false and the updatePanel UpdateMode to be Conditional. Could somebody please point out the problem. What am I doing wrong? I only want the updatepanel to take charge when the trigger events occur for this updatePanel

Here's my code for updatepanel and textBox . .

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

Best Answer

Do you have a submit button setup too on the page? Hitting enter is causing the form submit and hence, UpdatePanel could be performing the update.

If you're not sure, post your full HTML and JS here.

Update: Also, if this is the only textbox on the page, the Submit on Enter is the default behavior. In that case, add a eventhandler to cancel the default behvior like this:

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>

codebehind:

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