hi guys, i got a problem with the following script. though it serves exactly the functions i want, i don't quite understand the mechanism.
my question is:"when did the session("cart") get updated? i don't see statement like---session("cart")="datatable object" in the script, but the session actually get updated after running the codes!!!
following is the script which i wrote and tested.
=====================================
the functionality is perfect, coz i just followed an example in msdn library:" http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.gridview.rowupdating.aspx ”
<script runat="server" language="vb">
dim objdt as datatable
dim objdr as datarow
protected sub page_load(byval sender as object, byval e as system.eventargs) handles me.load
If Not Page.IsPostBack Then
objdt=session("cart")
gv.DataSource = objDT
gv.DataBind()
end if
response.Write(gv.rows(0).cells(2).text)
end sub
Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)
objdt = Session("Cart")
objdt.Rows(e.Item.ItemIndex).Delete()
Session("Cart") = objDT
gv.DataSource = objDT
gv.DataBind()
end sub
Protected Sub rowediting(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
gv.EditIndex = e.NewEditIndex
binddata()
End Sub
Protected Sub cancelingedit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
gv.EditIndex = -1
binddata()
End Sub
Protected Sub Updating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
objdt=session("cart")
Dim row As GridViewRow = gv.Rows(e.RowIndex)
objdt.Rows(gv.EditIndex)("quantity") = (CType((row.Cells(2).Controls(0)), TextBox)).Text
gv.EditIndex = -1
bindData()
End Sub
Private Sub BindData()
gv.DataSource = session("cart")
gv.DataBind()
End Sub
Ok. I got it. Wht's happing is that you have a global variable of DataTable Type.
dim objdt as datatable
Now when you write this statement.
objdt=session("cart")
it set the memory reference of datatable object in objdt variable from session. And now objdt is pointing to same memory location, to which session("cart") is pointing.
So now when in the below "Updating" function, you update "objdt" datatable. it updates the object of same memory location, to which session("cart") is pointing. That's why your session variable get updated.
Protected Sub Updating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
objdt=session("cart")
Dim row As GridViewRow = gv.Rows(e.RowIndex) objdt.Rows(gv.EditIndex)("quantity") = (CType((row.Cells(2).Controls(0)), TextBox)).Text
gv.EditIndex = -1
bindData()
End Sub
Hope it will help.
If my post solves your problem, please mark it as an answer.
Marked as answer by anguswong on Nov 24, 2009 03:20 AM
anguswong
Member
13 Points
30 Posts
session and gridview
Nov 24, 2009 01:17 AM|LINK
hi guys, i got a problem with the following script. though it serves exactly the functions i want, i don't quite understand the mechanism.
my question is:"when did the session("cart") get updated? i don't see statement like---session("cart")="datatable object" in the script, but the session actually get updated after running the codes!!!
following is the script which i wrote and tested.
=====================================
the functionality is perfect, coz i just followed an example in msdn library:" http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.gridview.rowupdating.aspx ”
<script runat="server" language="vb">
dim objdt as datatable
dim objdr as datarow
protected sub page_load(byval sender as object, byval e as system.eventargs) handles me.load
If Not Page.IsPostBack Then
objdt=session("cart")
gv.DataSource = objDT
gv.DataBind()
end if
response.Write(gv.rows(0).cells(2).text)
end sub
Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)
objdt = Session("Cart")
objdt.Rows(e.Item.ItemIndex).Delete()
Session("Cart") = objDT
gv.DataSource = objDT
gv.DataBind()
end sub
Protected Sub rowediting(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
gv.EditIndex = e.NewEditIndex
binddata()
End Sub
Protected Sub cancelingedit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
gv.EditIndex = -1
binddata()
End Sub
Protected Sub Updating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
objdt=session("cart")
Dim row As GridViewRow = gv.Rows(e.RowIndex)
objdt.Rows(gv.EditIndex)("quantity") = (CType((row.Cells(2).Controls(0)), TextBox)).Text
gv.EditIndex = -1
bindData()
End Sub
Private Sub BindData()
gv.DataSource = session("cart")
gv.DataBind()
End Sub
</script>
ASP.NET2.0 data binding please help..
Danish Ali
Contributor
3384 Points
468 Posts
Re: session and gridview
Nov 24, 2009 01:23 AM|LINK
Here it is getting update.
Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)
objdt = Session("Cart")
objdt.Rows(e.Item.ItemIndex).Delete()
Session("Cart") = objDT
gv.DataSource = objDT
gv.DataBind()
end sub
anguswong
Member
13 Points
30 Posts
Re: session and gridview
Nov 24, 2009 01:43 AM|LINK
thanks for ur reply.
but actually "sub delete_item()" didn't get involved in the whole process coz i had not use that part yet. i was only
using the editing function~
Danish Ali
Contributor
3384 Points
468 Posts
Re: session and gridview
Nov 24, 2009 02:02 AM|LINK
Ok. I got it. Wht's happing is that you have a global variable of DataTable Type.
dim objdt as datatable
Now when you write this statement.
objdt=session("cart")
it set the memory reference of datatable object in objdt variable from session. And now objdt is pointing to same memory location, to which session("cart") is pointing.
So now when in the below "Updating" function, you update "objdt" datatable. it updates the object of same memory location, to which session("cart") is pointing. That's why your session variable get updated.
Protected Sub Updating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
objdt=session("cart")
Dim row As GridViewRow = gv.Rows(e.RowIndex)
objdt.Rows(gv.EditIndex)("quantity") = (CType((row.Cells(2).Controls(0)), TextBox)).Text
gv.EditIndex = -1
bindData()
End Sub
Hope it will help.
anguswong
Member
13 Points
30 Posts
Re: session and gridview
Nov 24, 2009 03:19 AM|LINK
i don't know what exactly is happening inside the machines, but it makes sense. thanks a lot dude~~