Answered by:
GridView's edit command doesn't work with code in Page_load to check for postback

Question
-
User-1406853684 posted
Hi,I was just about to post a new thread but read yours and concluded we were probably having the same issue. Here is what I was going to post:
I created two updatepanels on a page. I put a gridview inside each updatepanel and set each updatepanel's updatemode to conditional.
The gridviews were bound in a programmatic way, using GridView1.DataSource=x and GridView1.DataBind() on page_load. The individual columns were defined in the GUI, with the datafield names corresponding to the appropriate column names being bound to the gridview.
I defined an edit button in both gridviews linked to an onRowCommand subroutine. When the user clicks the edit button, the query originally bound to the gridview is supposed to be called again, this time with different parameters, the gridview rebound to it and the updatepanel updated.
However, when the user clicks on this button—nothing happens. I put a break point in the row command subroutine and the application does go through the code—it just doesn't update the screen when it finishes.
My team has tried everything to get this to work. For the moment, we've wound up simply deleting the updatepanels and having a full page refresh (which works perfectly). But, from a user experience perspective, this is too slow.
Does anyone have any ideas as to what can be done to get an updatepanel to work with these two gridviews?
Thanks,
Ron
We do check on page load, however, to ensure that the request is not a postback. Could that be causing the problem?
Thanks,
RonTuesday, January 6, 2009 5:19 PM
Answers
-
User2077801861 posted
Hi Ron,
Happy New Year. After you change the EditIndex of the gridview, the gridview must be bound again. It's a good design to have the gridview binding code inside If(!Page.IsPostBack) in page load. All you need to do is add the gridview data binding code to your OnRowEditing event.
Important Note:-
If the Edit Button has command name "Edit", then create a GridView1_OnRowEditing() method and make it the event handler of the OnRowEditing event of your gridview. If you want to use the OnRowCommand event change the command name. "Edit" is an inbuilt .NET command name and would make your logic to malfunction. I have faced similar issues in the past.
Code Sample:-
protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { //Logic for obtaining data from database goes here. GridView1.DataSource = dsSourceData; GridView1.DataBind();
//Logic for obtaining data from database goes here.
GridView2.DataSource = dsSourceData;
GridView2.DataBind();
} //Rest of the Page_Load logic goes here. } protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e) { //Logic for obtaining data from database goes here. GridView2.DataSource = dsSourceData; GridView2.DataBind(); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 6, 2009 10:16 PM
All replies
-
User2143419647 posted
An update from a gridview in an updatepanel does count as a postback. I would have to see the full code behind. I have run into some problems in the past with viewstate as well. UpdatePanels with gridviews can get tricky from time to time.
Tuesday, January 6, 2009 10:03 PM -
User2077801861 posted
Hi Ron,
Happy New Year. After you change the EditIndex of the gridview, the gridview must be bound again. It's a good design to have the gridview binding code inside If(!Page.IsPostBack) in page load. All you need to do is add the gridview data binding code to your OnRowEditing event.
Important Note:-
If the Edit Button has command name "Edit", then create a GridView1_OnRowEditing() method and make it the event handler of the OnRowEditing event of your gridview. If you want to use the OnRowCommand event change the command name. "Edit" is an inbuilt .NET command name and would make your logic to malfunction. I have faced similar issues in the past.
Code Sample:-
protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { //Logic for obtaining data from database goes here. GridView1.DataSource = dsSourceData; GridView1.DataBind();
//Logic for obtaining data from database goes here.
GridView2.DataSource = dsSourceData;
GridView2.DataBind();
} //Rest of the Page_Load logic goes here. } protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e) { //Logic for obtaining data from database goes here. GridView2.DataSource = dsSourceData; GridView2.DataBind(); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 6, 2009 10:16 PM -
User-1406853684 posted
Thanks! That did the trick.
Ron
Thursday, January 8, 2009 5:59 PM