OnClick or IsPostBack

subaru_sti

Active member
Joined
Jul 4, 2009
Messages
34
Programming Experience
Beginner
Hello,

I'm building a web form and need some guidance - I haven't done this since VBscript. I'm not sure if I should be using OnClick or checking in IsPostBack. Here is my code:

default.aspx:


VB.NET:
<asp:Button ID="btnYes" runat="server" Text="Yes" onclick="btnYes_Click" />

Code Behind:

VB.NET:
Protected Sub btnYes_Click(sender As Object, e As System.EventArgs) Handles btnYes.Click
        DoABunchOfStuff(True)
        Response.Redirect("default.aspx")
End Sub

I am using a master page and in both my master page and my default page code behind, I'm doing a bunch of stuff in Page_Load, like database queries, setup, etc... When I use the onclick method, the code in both my master page Page_Load and default.aspx Page_Load gets executed but then I do a redirect anyways so it then executes that code a second time.

I need to know what is the recommended way to do this? I could wrap all of the code in Page_Load in an IsPostBack if statement so that it gets skipped when I click the button. I remember the old way of doing it is that you checked IsPostBack and then if it was posted back, you could do all your OnClick stuff in there.
 
Back
Top