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