2012-05-24 108 views
0

這是我的情況。我有一個網頁表單頁面,當整個頁面每次點擊一個控件時都會滾動到頂部,所以我正在嘗試將更新面板應用到我的頁面,這讓人討厭。從更新面板刷新整個頁面

取決於各個更新面板的內容是否處於正確的狀態,提交按鈕可以或不可以點擊。

當點擊提交按鈕時,它可能會影響頁面上的任何控件。

我知道我可以使用更新面板的<triggers>標記元素來做一些操作,但我不希望必須使用自己的觸發器將所有內容放到更新面板中,我寧願提交按鈕只是重新加載整個頁面。

爲了簡單起見,我把一個示例項目放在一起來表示我的頁面。它有一個「重置」按鈕,代表「提交」按鈕。

母版頁:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication1.SiteMaster" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head runat="server"> 
    <title></title> 
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" /> 
    <asp:ContentPlaceHolder ID="HeadContent" runat="server"> 
    </asp:ContentPlaceHolder> 
</head> 
<body> 
    <form runat="server"> 
    <asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" /> 

    <div class="page"> 
     <div class="header"> 
      <div class="title"> 
       <h1> 
        My ASP.NET Application 
       </h1> 
      </div> 
      <div class="loginDisplay"> 
       <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false"> 
        <AnonymousTemplate> 
         [ <a href="~/Account/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ] 
        </AnonymousTemplate> 
        <LoggedInTemplate> 
         Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>! 
         [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ] 
        </LoggedInTemplate> 
       </asp:LoginView> 
      </div> 
      <div class="clear hideSkiplink"> 
       <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"> 
        <Items> 
         <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/> 
         <asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/> 
        </Items> 
       </asp:Menu> 
      </div> 
     </div> 
     <div class="main"> 
      <asp:ContentPlaceHolder ID="MainContent" runat="server"/> 
     </div> 
     <div class="clear"> 
     </div> 
    </div> 
    <div class="footer"> 

    </div> 
    </form> 
</body> 
</html> 

默認頁面:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 



<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 

</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 

    <asp:Label runat=server Text="This lable represents things not in update panels" ID="label1"></asp:Label> 

    <asp:UpdatePanel ID="UpdatePanel1" runat=server> 
     <ContentTemplate> 
      <table> 
       <tr> 
        <td><asp:Button ID="button1" runat="server" OnClick="Button1_click" Text="Button 1" /></td> 
        <td><asp:TextBox ID="textBox1" runat="server" Text="StartText" Enabled="false" /></td> 
       </tr> 
      </table> 
     </ContentTemplate> 
    </asp:UpdatePanel> 

    <asp:UpdatePanel ID="UpdatePanel2" runat=server> 
     <ContentTemplate> 
      <table> 
       <tr> 
        <td><asp:Button ID="button2" runat="server" OnClick="Button2_click" Text="Button 2" /></td> 
        <td><asp:TextBox ID="textBox2" runat="server" Text="StartText" Enabled="false" /></td> 
       </tr> 
      </table> 
     </ContentTemplate> 
    </asp:UpdatePanel> 

    <asp:UpdatePanel ID="UpdatePanelReset" runat=server> 
     <ContentTemplate> 
      <asp:Button ID="reset" runat="server" OnClick="Reset_click" Text="reset" /> 
     </ContentTemplate> 
     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="button1" EventName="click" /> 
      <asp:AsyncPostBackTrigger ControlID="button2" EventName="click" /> 
     </Triggers> 
    </asp:UpdatePanel> 

</asp:Content> 

後面的代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Xml.Linq; 

namespace WebApplication1 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_click(object sender, EventArgs e) 
     { 
      textBox1.Enabled = !textBox1.Enabled; 
      reset.Enabled = !textBox1.Enabled && !textBox2.Enabled; 
     } 

     protected void Button2_click(object sender, EventArgs e) 
     { 
      textBox2.Enabled = !textBox2.Enabled; 
      reset.Enabled = !textBox1.Enabled && !textBox2.Enabled; 
     } 

     protected void Reset_click(object sender, EventArgs e) 
     { 
      textBox1.Text = "StartText"; 
      textBox2.Text = "StartText"; 
      label1.Text = "reset button clicked"; 
     } 
    } 
} 

回答

1

只是PostBackTrigger添加到最後的UpdatePanel。

<asp:PostBackTrigger ControlID="reset" />