2014-02-13 106 views
2

我遇到了一些問題。我有一些asp.net控件包裝在更新面板中,但是當我點擊提交按鈕時,它跳轉到頁面的頂部。我在這裏閱讀了一堆帖子,他們要麼使用一些javascript,要麼在頁面指令中設置MaintainPagePostion爲「true」。我試圖將其設置爲true,但沒有奏效。我真的不想用JavaScript腳本來完成這個任務。我的印象是這是使用更新面板的好處之一。但是,我發現最令人困惑的部分是,它習慣於不這樣做。我不記得在網站上改變任何會導致這種情況的東西。任何有關這個問題的幫助表示讚賞。謝謝。即使使用更新面板,頁面也會跳轉到頁面的頂部

這是我正在使用的代碼。

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
<ContentTemplate> 
    <asp:Panel ID="pnlEmailStuff" runat="server"> 
     Name: <asp:TextBox ID="txtName" runat="server" Width="202px"></asp:TextBox><br /> 
     Email: <asp:TextBox ID="txtEmail" runat="server" Width="203px"></asp:TextBox><br /> 
       <span style="font-size:12px; font-weight:normal; margin-left:55px;">**Please double check email**</span><br /> 
     Message:<br /> 
     <asp:TextBox ID="txtMessage" runat="server" Width="370px" TextMode="MultiLine" Font-Names="Tahoma" Font-Size="Small" Height="75px"></asp:TextBox><br /> 
     <asp:Label ID="lblEmailError" runat="server" Text="" Font-Size="Small" ForeColor="Red"></asp:Label> 
     <asp:ImageButton Height="25px" Width="60px" CssClass="EmailSubmit" ImageUrl="Images/MailingListBtnSubmit2.png" ID="btnSubmit" runat="server" onclick="btnSubmit_Click"/> 
    </asp:Panel> 
    <asp:Panel ID="pnlThankYou" runat="server" Visible="false"> 
     <p style="text-align:center; font-size:30px;">Thank you!<br /><span style="font-size:20px;">Your Email has been sucessfully submitted.</span></p> 
    </asp:Panel> 
</ContentTemplate> 

+0

嗯,我想我知道這是我的命。我的應用程序中有一些頁面路由。我做了一個項目的副本,並刪除了我的global.asax文件中的頁面路由代碼,它的工作原理與以前一樣。 – Vandel212

回答

0

有您指定的更新面板的觸發器?如果您在觸發器部分指定了觸發器,則更新面板將更新而不會跳到頂部。您還需要提供updatemode =「conditional」。它可以這樣做:

<asp:UpdatePanel ID="ex" runat="server" UpdateMode="Conditional"> 
<ContentTemplate> 
//your controls here 
</ContentTemplate> 
<Triggers> 
<asp:AsyncPostBackTrigger ControlID="yourbuttonid" /> 
</Triggers> 
</asp:UpdatePanel> 
+0

剛剛嘗試過,依然如此。 – Vandel212

+0

嘗試在您的提交按鈕中單擊此事件:UpdatePanel1.Update() – nitinvertigo

0

我認爲,它不是跳轉到頁面的頂部。它刷新頁面。什麼是您的更新面板的UpdateMode?是否有條件?如果是有條件的,請檢查觸發器。 ControlID應該是按鈕ID,EventName ='點擊'。然後檢查更新面板的區域。

示例代碼這裏:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
<ContentTemplate> 
    <asp:Panel ID="pnlEmailStuff" runat="server"> 
     Name: <asp:TextBox ID="txtName" runat="server" Width="202px"></asp:TextBox><br /> 
     Email: <asp:TextBox ID="txtEmail" runat="server" Width="203px"></asp:TextBox><br /> 
       <span style="font-size:12px; font-weight:normal; margin-left:55px;">**Please double check email**</span><br /> 
     Message:<br /> 
     <asp:TextBox ID="txtMessage" runat="server" Width="370px" TextMode="MultiLine" Font-Names="Tahoma" Font-Size="Small" Height="75px"></asp:TextBox><br /> 
     <asp:Label ID="lblEmailError" runat="server" Text="" Font-Size="Small" ForeColor="Red"></asp:Label> 
     <asp:ImageButton Height="25px" Width="60px" CssClass="EmailSubmit" ImageUrl="Images/MailingListBtnSubmit2.png" ID="btnSubmit" runat="server" onclick="btnSubmit_Click"/> 
    </asp:Panel> 
    <asp:Panel ID="pnlThankYou" runat="server" Visible="false"> 
     <p style="text-align:center; font-size:30px;">Thank you!<br /><span style="font-size:20px;">Your Email has been sucessfully submitted.</span></p> 
    </asp:Panel> 
</ContentTemplate> 
<Triggers> 
      <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" /> 
</Triggers> 
</asp:UpdatePanel> 
+0

感謝您的建議。我嘗試將更新模式設置爲條件並添加觸發器。就像我說的,過去一直很好。這就是爲什麼我很困惑。我編輯了我的帖子以顯示我正在使用的代碼。我希望有所幫助。 – Vandel212

+0

嗨Randell,我給我的第一個答案添加了示例代碼。 – Cuhara

4

你可以做到這一點在4種方式:

  1. 從代碼的背後 - Page.MaintainScrollPositionOnPostBack = true;

  2. 從Page指令 - MaintainScrollPositionOnPostback="true"

  3. 從Web.config - <pages maintainScrollPositionOnPostBack="true" />

  4. 使用Javascript。您可以使用以下鏈接中的代碼。它的工作對我來說 -

http://weblogs.asp.net/andrewfrederick/archive/2008/03/04/maintain-scroll-position-after-asynchronous-postback.aspx

+0

順便說一下,你使用的瀏覽器是什麼?有時,這些純粹是瀏覽器的具體問題,在這種情況下,javascript是最好的選擇,因爲這些代碼駐留在客戶端並且獨立於呈現問題。 –

+0

我試過Chrome和Fire Fox。我已經嘗試了#3。謝謝你的迴應。我在某處看到維護滾動位置僅適用於IE瀏覽器。 – Vandel212

+0

出於同樣的原因,在這種情況下,javascript是最好的選擇。大多數瀏覽器對這種語言都有強大的支持。 –

0

好謝謝大家給了我的建議。事實證明,頁面路由是造成這個問題的原因。因此,所有我必須做的就是它的工作是補充,而忽略該頁面線在我的RegisterRoutes代碼塊:

void RegisterRoutes(RouteCollection routes) 
    { 
     routes.Ignore("Mobile"); 
     routes.Ignore("Booking.aspx*");//<---- This Fixed it. 
     routes.MapPageRoute("Gallery", "Gallery/{Name}", "~/Gallery.aspx"); 
     routes.Ignore("*");//<---- This is better for me. It acts as a catch all. 
    } 

這幫助我解決這個問題:http://forums.asp.net/t/1743640.aspx

編輯

我添加了「routes.Ignore(」「);」代碼來充當一個捕獲所有,所以我真的不需要特別忽略「Booking.aspx」。請記住,雖然順序在這裏很重要。 Ignore(「」)必須是最後一個,否則其他路由都不會起作用。

再次感謝大家。

0

請嘗試PageRequestManager處理

<script> 
       Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); 



     function EndRequestHandler(sender, args) { 
      try { 

       sender._controlIDToFocus = null; 


      } 
      catch (e) { 
      } 
     } 

</script> 
相關問題