[RESOLVED] ImageButton always calls page_load event....

I'm using ASP.NET 3.5 and C#.

I load an SqlDataSource into a DataView on my Page_Load event. My SqlDataSource queries a random record from the database. On my page I have an ImageButton, which when clicked uses a Response.Redirect("AnotherPage.aspx + "random id from database"). My problem is that when the button is clicked it fires the Page_Load event before the Response.Redirect takes place in the button_click event. This causes the current data(the users "id") that I had pulled from the database to be overwritten with new data(a different "id") from the database. So essentially the the page is redirecting to the 2nd random id that is queried from the db after the button is clicked and Page_Load is fired again, not the original id. Here's my code, hope it makes sense.

Here's my Page_Load:

DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

id = dv[0]["Id"].ToString();

Then my Button_Click Event:

protected void btn1_Click(object sender, ImageClickEventArgs e)
        Response.Redirect("~/Profile.aspx?id=" + id1);

Can I put my dataview in another event or method so it's not called when a user clicks an ImageButton. Also I want the DataView to be repopulated on a postback/page refresh, just not on button clicks. How?


The page life cycle will work like that only.

In the Page_Load event you need to use Page.IsPostBack checking to ensure that during the post back it does not update the original value.

So fetching of the random id should be as below

in Page_Load event , give this condition

if (!Page.IsPostBack) {

    randomID = your code;


If Not IsPostBack on Page load will solve your problem

thoughts on "[RESOLVED] ImageButton always calls page_load event...."

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>