UpdatePanel does not prevent button from reload page

痞子三分冷 提交于 2019-12-11 00:26:44

问题


I just wanted to short this question's example by making a short prototype to test the UpdatePanel functionality to disable ASP.NET page reload on linkbutton click. I have seen this approach in questions like this. In order to test this I have created a new ASP.NET WebForms application, added an ASP LinkButton, surronded by an UpdatePanel. I have added breakpoints on both OnClick and PageLoad events, in Visual Studio .cs file. This is the code in ASP file:

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:LinkButton ID="LinkButton1" OnClick="OnLinkClick" runat="server">Click me, no reload, I promise!</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>

And the .cs file code:

    protected void Page_Load(object sender, EventArgs e)
    {
        //page reload things...
    }

    protected void OnLinkClick(object sender, EventArgs e)
    {
        //on click thigs...
    }

What happens is that everytime I click on the LinkButton, the first breakpoint is triggered is the PageLoad one and then the OnClick one. I thought that using UpdatePanel will avoid this behavior.

What Am I doing wrong, or why shouldn't I expect the PageLoad not to be invoked? Is this a good way to test if the LinkButton click reloads the page?


回答1:


As mentioned by Clint B, the processing of postback events in code-behind is normal. You can test your UpdatePanel with two Labels, one inside and one outside of the UpdatePanel:

<asp:Label ID="lbl1" runat="server" Text="Label 1" BackColor="Aqua" />
<asp:UpdatePanel ID="up1" runat="server">
    <ContentTemplate>
        <asp:Label ID="lbl2" runat="server" Text="Label 2" BackColor="Yellow" />
        <asp:LinkButton ID="LinkButton1" runat="server" Text="Update, no reload!" OnClick="OnLinkClick" />
    </ContentTemplate>
</asp:UpdatePanel>

The event handler of the LinkButton updates both Labels:

protected void OnLinkClick(object sender, EventArgs e)
{
    lbl1.Text = DateTime.Now.ToString();
    lbl2.Text = DateTime.Now.ToString();
}

If the partial refresh works, the changes made to lbl2 are visible in the page, while lbl1 still displays its original text.




回答2:


Even though you are doing a partial post back, the framework still executes the page life cycle which includes Page_Load. But you will only be able to effect changes in the browser to the controls that are inside the update panel.

As techspider mentioned, you need to assign a post back trigger. So your example should look like this.

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
   <asp:LinkButton ID="LinkButton1" OnClick="OnLinkClick" runat="server">Click me, no reload, I promise!</asp:LinkButton>
</ContentTemplate>
<Triggers>
  <asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click" /> 
</Triggers>
</asp:UpdatePanel>



回答3:


I found that when i use LinkButton inside an updatepanel, page is doing postback (page is fully refreshed in the browser) UNLESS i indicate an ID and ClientIDMode="AutoID" on the LinkButton control.



来源:https://stackoverflow.com/questions/37997032/updatepanel-does-not-prevent-button-from-reload-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!