I'm just having an issue I'm hoping someone will be able to give me a few pointers. I have an asp page which is programatically creating multiple gridviews on the same aspx page depending on a variable set of values retrieved from the database.
As I am programmatically creating the GridView in code behind, I have to create the gridview gridviews each time the page loads, on postback, oninit etc. I currently create the gridviews within the OnInit function of the page. This all works fine except for when the user goes into row edit mode then clicks the Update (row update) button/link. This forces a postback on the grid which reloads the grid values from database before their values are updated thus preventing the RowUpdating event from ever launching. How do I get around this? I have tried capturing the command selected via the RowCommand event but this event only gets fired after the postback, so the oninit function gets called first before the Rowcommand so the command name I've captured is of little use to me.
I am creating the GridView as follows in the Oninit function
GridViewLineItem = New GridView
If bEnableEdit = True Then
GridViewLineItem.AutoGenerateEditButton = True
End If
If bEnableDelete = True Then
GridViewLineItem.AutoGenerateDeleteButton = True
End If
GridViewLineItem.AutoGenerateColumns = False
GridViewLineItem.EnableViewState = True
GridViewLineItem.Font.Name = "Arial"
GridViewLineItem.Font.Size = 8
GridViewLineItem.ForeColor = Drawing.Color.Black
GridViewLineItem.BackColor = Drawing.Color.White
GridViewLineItem.BorderColor = Drawing.Color.LightGray
GridViewLineItem.BorderStyle = BorderStyle.None
GridViewLineItem.BorderWidth = 1
GridViewLineItem.CellPadding = 4
GridViewLineItem.AlternatingRowStyle.BackColor = Drawing.Color.White
GridViewLineItem.FooterStyle.BackColor = Drawing.Color.BurlyWood
GridViewLineItem.HeaderStyle.BackColor = Drawing.Color.DarkGray
GridViewLineItem.HeaderStyle.Font.Bold = True
GridViewLineItem.HeaderStyle.ForeColor = Drawing.Color.Black
GridViewLineItem.PagerStyle.BackColor = Drawing.Color.Beige
GridViewLineItem.PagerStyle.ForeColor = Drawing.Color.Black
GridViewLineItem.PagerStyle.HorizontalAlign = HorizontalAlign.Right
GridViewLineItem.RowStyle.BackColor = Drawing.Color.Beige
GridViewLineItem.ID = "LineItemGridView" + strItemID
strCBItemID = strItemID
AddHandler GridViewLineItem.RowEditing, AddressOf GridViewLineItem_RowEditing
AddHandler GridViewLineItem.RowCancelingEdit, AddressOf GridViewLineItem_RowCancelingEdit
AddHandler GridViewLineItem.RowDataBound, AddressOf GridViewLineItem_RowDataBound
AddHandler GridViewLineItem.RowUpdating, AddressOf GridViewLineItem_RowUpdating
AddHandler GridViewLineItem.PageIndexChanging, AddressOf GridViewLineItem_PageIndexChanging
AddHandler GridViewLineItem.PreRender, AddressOf GridViewLineItem_PreRender
AddHandler GridViewLineItem.PageIndexChanged, AddressOf GridViewLineItem_PageIndexChanged
AddHandler GridViewLineItem.RowDeleting, AddressOf GridViewLineItem_OnDeleting
AddHandler GridViewLineItem.RowCommand, AddressOf GridViewLineItem_RowCommand
'bind data source
dt = New DataTable
oda = New OleDb.OleDbDataAdapter
oda.Fill(dt, rsGroup)
GridViewLineItem.DataSource = dt
GridViewLineItem.DataBind()
'Add gridview to page
tCell.Controls.Add(GridViewLineItem)
tRow.Cells.Add(tCell)
TableLineItem.Rows.Add(tRow)
LineItemPanel.Controls.Add(TableLineItem)
LineItemPanel.BorderStyle = BorderStyle.Solid
LineItemPanel.BorderWidth = 3
LineItemPanel.BorderColor = Drawing.Color.DarkGray
tGroupRow = New TableRow
tGroupCell = New TableCell
tGroupCell.ColumnSpan = TableGroupDetails.Rows.Item(0).Cells.Count
tGroupCell.Controls.Add(LineItemPanel)
tGroupRow.Cells.Add(tGroupCell)
TableGroupDetails.Rows.Add(tGroupRow)
Can someone plese give me some pointers in how I should skip the databind of the gridview when the user clicks on the row update? I can't tie the Gridview's Databind indisriminately around a page.IsPostback = false as if I do this, The grid will only be loaded once and another other valid postsbacks such as when the user clicks on to get into row edit mode would cause an empty grid.
Thanks
Thanks a million SomeGuy! Moving the GridView bind to Page.PreRender resolved the issue. My flow of events used to be Page.Oninit (where gridview gets created on runtime) -> Page.Load (gridview bind) -> GridView.RowUpdating (where i was commiting the changes to database). It is now Page.OnInit (gridview creation) -> GridView.RowUpdating (committing change to database) -> Page.PreRender (where the gridview is being bound). Only Issue now though is I'm rendering an ajaxcombobox for the gridview's edit mode through iTemplate. The user selection for this is lost by the time GridView.RowUpdating event is called. All the other fields seuch as textboxes retain the user input value. Going to dig into this alittle further to see what I can find.
P.S. I can't post this as a comment under your post for some reason...
P.P.S fixed issue with Ajax Combobox in gridview edit mode. had to set the enableviewstate to true
Dim AjaxComb As New AjaxControlToolkit.ComboBox
AjaxComb.EnableViewState = True
which is alittle weird... you'd think its default would have been true...