Preserve Data to Edit on DataGrid When Filtering

G

Gummy

Hello All,

I have a webpage that has two dropdown listboxes. Based on what is selected
in these dropdown listboxes, it filters a DataGrid . That works fine.

In the DataGrid , when I go to edit a row, I change the textbox (or other
control), click Update, but it doesn't save my data. I then did this...

In Page_Load, I added code to filter the DataGrid only when it was Not
Page.IsPostBack. Then the editing/updating of the DataGrid data works fine.
But, of course, I lose the ability to filter the DataGrid.

Obviously the problem is with the Postback. But how am I able to have both
the postback and the editing available simultaneously?

I had heard that I should stay away from Session variables, but that seems
that it might work here. Is there another way to do this without Session
variables?

Thank you for the help.
 
D

Darren Kopp

You could always do ViewState["FilterVar1"] = "accounting" or something
similar if you didn't want to do session. Some code might help as your
problem could be in several areas (i'm guessing it's a re-binding problem).

-Darren Kopp
http://blog.secudocs.com
 
G

Gummy

Darren,

Thank you for the information. I have to go look at exactly what ViewState
does (I'm a newbie).

If you want, here's the code. I hope it helps and isn't too messy. As an
aside, I am getting very random "Object reference not set to an instance of
an object" error that I am having problem tracking down. If you see
something, please let me know.

Thank you so much for the help,

----------------------------------------
Public Class CreateParent
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents chkboxMaxDate As System.Web.UI.WebControls.CheckBox
Protected WithEvents dgParents As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region


Protected WithEvents ucStructureRollupDropDown As New
SelectTypeDropDownBoxes
Protected WithEvents HeaderTab1 As New HeaderTab


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here


If Not Page.IsPostBack Then

FillDataGrid_Parent_Filter(ucStructureRollupDropDown.StructureValue,
ucStructureRollupDropDown.RollupValue)
End If

End Sub


Private Sub FillDataGrid_Parent()
Dim strConn As String
Dim strSQL As String
Dim dt As New RobData.DataTransfer
Dim dv As DataView


Try
strConn = dt.OracleConnect("rptpr01", "rptuser", "r00ster")

' strSQL = String.Format("SELECT * from RLUP_PARENT")
strSQL = String.Format("select a.*, rt.rollupdesc,
st.structuredesc from rlup_parent a" & _
" join rlup_rolluptype rt on a.rollupid = rt.rollupid " & _
" join rlup_structuretype st on a.structureid = st.structureid " &
_
" order by a.parentdesc, a.effdt desc")

dv = dt.GetDataSet(strSQL, strConn).Tables(0).DefaultView

dgParents.DataSource = dv 'dt.GetDataSet(strSQL, strConn)

' TempDataView = dt.GetDataSet(strSQL,
strConn).Tables(0).DefaultView

dgParents.DataBind()

Catch ex As Exception
Throw ex

End Try

End Sub

Private Sub FillDataGrid_Parent_Filter(ByVal _structureID As String,
ByVal _rollupID As String)

Dim strConn As String
Dim strSQL As String
Dim dt As RobData.DataTransfer

Try
strConn = dt.OracleConnect("rptpr01", "rptuser", "r00ster")
strSQL = String.Format("select a.*, rt.rollupdesc,
st.structuredesc from rlup_parent a" & _
" join rlup_rolluptype rt on a.rollupid = rt.rollupid " & _
" join rlup_structuretype st on a.structureid = st.structureid
" & _
" where a.structureid Like '{0}%' and a.rollupid like '{1}%' " &
_
" order by a.parentdesc, a.effdt desc", _structureID,
_rollupID)

dgParents.DataSource = dt.GetDataSet(strSQL, strConn)
dgParents.DataBind()

Catch ex As Exception
Throw ex


End Try


End Sub




Private Sub dgParents_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgParents.EditCommand
Dim strSQL As String
Dim strConnect As String
Dim strProjectID As String
Dim dtEffectDate As Date

Dim strConn As String
Dim dt As RobData.DataTransfer
Try

'Item that it is going to be edited
dgParents.EditItemIndex = e.Item.ItemIndex


strConn = dt.OracleConnect("rptpr01", "rptuser", "r00ster")
strSQL = String.Format("select a.*, rt.rollupdesc,
st.structuredesc from rlup_parent a" & _
" join rlup_rolluptype rt on a.rollupid = rt.rollupid " & _
" join rlup_structuretype st on a.structureid = st.structureid
" & _
" order by a.parentdesc, a.effdt desc")


dgParents.DataSource = dt.GetDataSet(strSQL, strConn)
dgParents.DataBind()

Catch ex As Exception
Throw ex

End Try


' Session("AddMode") = False
End Sub

Private Sub dgParents_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgParents.UpdateCommand
Dim strSQL As String
Dim strConnect As String
Dim strParentID As String

Dim strParentDesc As String
Dim dtEffectDate As String
Dim strRollupID As String
Dim strStructureID As String

Dim strConn As String
Dim dt As RobData.DataTransfer
Try


With e.Item
strParentID = CType(.Cells(0).Controls(0), TextBox).Text
strParentDesc = CType(.Cells(1).Controls(0), TextBox).Text
dtEffectDate = "2005-DEC-01" ' "to_date('" &
Format(CType(.Cells(2).Controls(0), TextBox).Text(), "yyyy-MMM-dd hh:mm:ss
tt") & "', 'yyyy/mm/dd:hh:mi:ssam')"
strRollupID = CType(.Cells(3).Controls(1),
DropDownList).SelectedItem.Value
strStructureID = CType(.Cells(4).Controls(1),
DropDownList).SelectedItem.Value
End With

strConn = dt.OracleConnect("rptpr01", "rptuser", "r00ster")
strSQL = String.Format("Update rlup_parent set ParentDesc =
'{0}', EFFDT = '{1}', RollupID = '{2}', structureid = '{3}' where parentid
= '{4}'", strParentDesc, dtEffectDate, strRollupID, strStructureID,
strParentID)
dt.ExecuteSQL(strSQL, strConn)

dgParents.EditItemIndex = -1


FillDataGrid_Parent_Filter(ucStructureRollupDropDown.StructureValue,
ucStructureRollupDropDown.RollupValue)

Catch ex As Exception
Throw ex

End Try


End Sub


Private Sub ddlStructure_BindData(ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)


Try
Dim strConn As String
Dim strSQL As String
Dim dt As New RobData.DataTransfer
Dim drStructure As OleDb.OleDbDataReader
Dim structItem As New ListItem

If e.Item.ItemType = ListItemType.EditItem Then
'Code to get the structure type
strConn = dt.OracleConnect("rptpr01", "rptuser", "r00ster")
strSQL = String.Format("SELECT * from RLUP_structureType")
drStructure = dt.GetDataReader(strSQL, strConn)
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)

Dim strStructureSelection As String = DRV("structuredesc")

Dim ddlStruct As New DropDownList

ddlStruct = CType(e.Item.FindControl("ddlStructureEdit"),
DropDownList)


Do While drStructure.Read 'Fills the drop down list box
structItem = New
ListItem(drStructure("structuredesc").ToString,
drStructure("structureID").ToString)
ddlStruct.Items.Add(structItem)
Loop


Dim itemStructure As New ListItem

itemStructure =
ddlStruct.Items.FindByText(strStructureSelection)
itemStructure.Selected = True
drStructure.Close()

End If

Catch ex As Exception
Throw ex
End Try
End Sub

Private Sub ddlRollup_BindData(ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
Try
If e.Item.ItemType = ListItemType.EditItem Then
Dim strConn As String
Dim strSQL As String
Dim dt As New RobData.DataTransfer
Dim drRollup As OleDb.OleDbDataReader
Dim drStructure As OleDb.OleDbDataReader
Dim rollupItem As New ListItem

'Code to get the rollup type
strConn = dt.OracleConnect("rptpr01", "rptuser", "r00ster")
strSQL = String.Format("SELECT * from RLUP_RollupType")
drRollup = dt.GetDataReader(strSQL, strConn)

Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim strRollupSelection As String = DRV("rollupdesc")

Dim ddlRollup As New DropDownList
ddlRollup = CType(e.Item.FindControl("ddlRollupEdit"),
DropDownList)

Do While drRollup.Read 'Fills the drop down list box
rollupItem = New
ListItem(drRollup("RollupDesc").ToString, drRollup("RollupID").ToString)
ddlRollup.Items.Add(rollupItem)
Loop

Dim item As New ListItem

item = ddlRollup.Items.FindByText(strRollupSelection)
item.Selected = True
drRollup.Close()

End If

Catch ex As Exception
Throw ex
End Try

End Sub



Private Sub dgParents_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgParents.ItemDataBound

Try

If e.Item.ItemType = ListItemType.EditItem Then
ddlStructure_BindData(e)
ddlRollup_BindData(e)
End If

Catch ex As Exception
Throw ex
End Try
End Sub
End Class




Darren Kopp said:
You could always do ViewState["FilterVar1"] = "accounting" or something
similar if you didn't want to do session. Some code might help as your
problem could be in several areas (i'm guessing it's a re-binding problem).

-Darren Kopp
http://blog.secudocs.com

Gummy said:
Hello All,

I have a webpage that has two dropdown listboxes. Based on what is
selected
in these dropdown listboxes, it filters a DataGrid . That works fine.

In the DataGrid , when I go to edit a row, I change the textbox (or other
control), click Update, but it doesn't save my data. I then did this...

In Page_Load, I added code to filter the DataGrid only when it was Not
Page.IsPostBack. Then the editing/updating of the DataGrid data works
fine.
But, of course, I lose the ability to filter the DataGrid.

Obviously the problem is with the Postback. But how am I able to have both
the postback and the editing available simultaneously?

I had heard that I should stay away from Session variables, but that seems
that it might work here. Is there another way to do this without Session
variables?

Thank you for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top