views:

4540

answers:

5

My understanding of the order of page events is this:

Page : Load

Control : DataBind (for a GridView or whatever)

Control : Load

Control : Clicked (for a Button)

Page: PreRender

Control : PreRender

(There are lots of others - but these are the ones I'm interested in)

The important thing to notice here is that the button's click event comes after the gridview's bind event. If the button causes a change to the data, the GridView displays the old data. I could rebind the control in the PreRender event, but that seems totally ugly.

This must be a very common pattern (a button that updates data). How do I put this together so that the GridView binds to the data after the Button click changes it?

A: 

The only reason the button click event comes after GridView bind is because you programmed your page to do that . I don't see any problem in binding the control in the PreRender event, in fact that is the only way to take action after a control event (such as Button onclick).

Otávio Décio
+1  A: 

ASP.NET does, by default, lots of binding and rebinding. Rebinding after a click event is normal.

Mufasa
+1  A: 

The answer was in the Button Click event, after the data has been changed, call DataBind() on the page to cause the GridView (and anything else that needs it) to rebind. I didn't realise you could do that.

Thank you Ocdecio & Mufasa - I would mark your answers as helpful, but I got no rep yet.

TallGuy
I got no rep yet either, not compared to ocdecio anyway! :)
Mufasa
A: 
enter code here

SqlConnection con = new SqlConnection(_conString); SqlCommand cmd = new SqlCommand("dbo.products_sel", con);

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

// Initialize command cmd.Connection = con; cmd.CommandType = CommandType.StoredProcedure;

DataSet ds = new DataSet();

con.Open(); adapter.Fill(ds, "Employees");

// Create parameters cmd.Parameters.AddWithValue("@SortExpression", sortExpression); cmd.Parameters.AddWithValue("@StartRowIndex", startRowIndex); cmd.Parameters.AddWithValue("@MaximumRows", maximumRows);

// Execute command //con.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection) ;

Tonmoy
A: 

didnt understand i want to use the button to link to another page in the asp.net but i couldnt so please wat is the code for that

asmaa