Solved

Simple ASP.NET Data Question (I hope!)

Posted on 2009-04-14
2
179 Views
Last Modified: 2012-05-06
I am primarily a Forms programmer. I am now delving into web programming. I am working through some of the data-access examples at http://www.asp.net/learn/data-access. The sample code to follow will be modified slightly from the example at the site.

My question is what is happening with my code/page in the code below? Specifically, if the page load event finishes before the update product function is executed, why would my page refresh data? Is page load not the last function to run? I added the IsPostBack lines to see if I could make the data refresh with one click of the button. Is this good practice?

I may have more questions depending on the answer :)
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using DAL_Tutorial.NorthwindTableAdapters;
 

public partial class AllProducts : System.Web.UI.Page

{

    ProductsBLL productLogic;
 

    protected void Page_Load(object sender, EventArgs e)

    {

        productLogic = new ProductsBLL();
 

        if (!IsPostBack)

        {

            GridView1.DataSource = productLogic.GetProductsByCategoryID(1);

            GridView1.DataBind();

        }

    }
 

    protected void UpdateProduct(object sender, EventArgs e)

    {

        try

        {

            productLogic.UpdateProduct(this.newName.Text, 1, 1, null, 14m, 10, null, null, false, 1);
 

            if (IsPostBack)

            {

                GridView1.DataSource = productLogic.GetProductsByCategoryID(1);

                GridView1.DataBind();

            }

        }

        catch (ArgumentException ae)

        {

            Response.Write("There was a problem: " + ae.Message);

        }

    }

}

Open in new window

0
Comment
Question by:käµfm³d   👽
2 Comments
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 500 total points
ID: 24141204
Hi,

Please check the below link for Asp.Net Page life cycle.
http://msdn.microsoft.com/en-us/library/ms178472.aspx

In the current scenario Page_Load will be executed first. I believe the UpdateProduct is the handler for the Button Click Event. It will be executed next. That's why you are preventing the Grid not to be binded again in the postback first by having a validation if (!IsPostBack) or (!Page.IsPostBack)

The validation if (IsPostBack) in UpdateProduct function is not required because the control will come here only after the postback.
0
 
LVL 75

Author Closing Comment

by:käµfm³d 👽
ID: 31570062
Excellent. This is what I needed to know. Thanks again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Needing an event fired when a row is added on a datagridview 4 36
Two different visual studio versions 3 23
Split in Javascript 5 31
Error on link 14 38
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now