473,738 Members | 5,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGrid Edit Problem

A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:

<script runat="server">
Dim sqlConn As New SqlConnection(" .....")

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub

Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End

Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter

sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")

dSet = New DataSet
sqlDapter.Fill( dSet)

dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>

<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>

<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>

Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.

What am I doing wrong here?

Nov 28 '06 #1
9 2724
You must set the EditItemIndex of the grid.

In the edit command add this line before binding the grid.
dgUsers.EditIte mIndex = ea.Item.ItemInd ex

You could also add the line above to the FillDataGrid sub prior to the
databind as well.

Hope this helps.
rn**@rediffmail .com wrote:
A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:

<script runat="server">
Dim sqlConn As New SqlConnection(" .....")

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub

Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End

Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter

sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")

dSet = New DataSet
sqlDapter.Fill( dSet)

dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>

<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>

<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>

Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.

What am I doing wrong here?
Nov 28 '06 #2
Yeah...you are correct, Gozirra....I was missing the
dgUser.EditItem Index = ea.Item.ItemInd ex line.

Another problem I am facing is in the UpdateCommand event handler of
the DataGrid. When a row in the DataGrid is in the editable mode, the
second, third & fourth cells of that row i.e. FirstName, LastName &
Address will get replaced by TextBoxes. The current row in editable
mode will also display the "Update" & "Cancel" links. This is the code
in the UpdateCommand event handler of the DataGrid:

Sub UpdateUsers(ByV al obj As Object, ByVal ea As
DataGridCommand EventArgs)
'this code only show the code for the
'Address TextBox which is the 4th cell

Dim strAddress As String
Dim txtAddress As TextBox

txtAddress = CType(ea.Item.C ells(3).Control s(0), TextBox)
strAddress = txtAddress.Text

Response.Write( strAddress)

dgUsers.EditIte mIndex = -1
dgUsers.DataBin d()
End Sub

Now assume that the Text in the Address TextBox when the DataGrid is in
the editable mode is "House No. 45, Park Avenue Road". I change it to
"Room No. 65, Marine Lines" & then click the "Update" link. When the
page posts back, the Response.Write( strAddress) still displays the old
address i.e. "House No. 45, Park Avenue Road" & not the updated address
"Room No. 65, Marine Lines".

How do I retrieve the updated address after clicking the "Update" link?

Gozirra wrote:
You must set the EditItemIndex of the grid.

In the edit command add this line before binding the grid.
dgUsers.EditIte mIndex = ea.Item.ItemInd ex

You could also add the line above to the FillDataGrid sub prior to the
databind as well.

Hope this helps.
rn**@rediffmail .com wrote:
A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:

<script runat="server">
Dim sqlConn As New SqlConnection(" .....")

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub

Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End

Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter

sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")

dSet = New DataSet
sqlDapter.Fill( dSet)

dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>

<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>

<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>

Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.

What am I doing wrong here?
Nov 28 '06 #3
Gozirra, I resolved the problem finally. The updated values were not
reflected because the Page_Load sub was calling dgUsers.DataBin d after
every post back. Actually I came across a post which helped me resolve
the issue.

The post said that "If you are re-binding data on Page Load, the
control values would get reset to the original values". I couldn't
exactly understand why do the TextBox values get reset to the original
values if dgUsers.DataBin d is called after every post back.

Could you please throw some light on why does this happen?
rn**@rediffmail .com wrote:
Yeah...you are correct, Gozirra....I was missing the
dgUser.EditItem Index = ea.Item.ItemInd ex line.

Another problem I am facing is in the UpdateCommand event handler of
the DataGrid. When a row in the DataGrid is in the editable mode, the
second, third & fourth cells of that row i.e. FirstName, LastName &
Address will get replaced by TextBoxes. The current row in editable
mode will also display the "Update" & "Cancel" links. This is the code
in the UpdateCommand event handler of the DataGrid:

Sub UpdateUsers(ByV al obj As Object, ByVal ea As
DataGridCommand EventArgs)
'this code only show the code for the
'Address TextBox which is the 4th cell

Dim strAddress As String
Dim txtAddress As TextBox

txtAddress = CType(ea.Item.C ells(3).Control s(0), TextBox)
strAddress = txtAddress.Text

Response.Write( strAddress)

dgUsers.EditIte mIndex = -1
dgUsers.DataBin d()
End Sub

Now assume that the Text in the Address TextBox when the DataGrid is in
the editable mode is "House No. 45, Park Avenue Road". I change it to
"Room No. 65, Marine Lines" & then click the "Update" link. When the
page posts back, the Response.Write( strAddress) still displays the old
address i.e. "House No. 45, Park Avenue Road" & not the updated address
"Room No. 65, Marine Lines".

How do I retrieve the updated address after clicking the "Update" link?

Gozirra wrote:
You must set the EditItemIndex of the grid.

In the edit command add this line before binding the grid.
dgUsers.EditIte mIndex = ea.Item.ItemInd ex

You could also add the line above to the FillDataGrid sub prior to the
databind as well.

Hope this helps.
rn**@rediffmail .com wrote:
A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:
>
<script runat="server">
Dim sqlConn As New SqlConnection(" .....")
>
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub
>
Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End
>
Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter
>
sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")
>
dSet = New DataSet
sqlDapter.Fill( dSet)
>
dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>
>
<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>
>
<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>
>
<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>
>
<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>
>
Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.
>
What am I doing wrong here?
Nov 28 '06 #4
I'm glad. I created a small test and everything was working fine. I
was getting ready to ask you if you were rebinding somewhere before the
update event.

It all has to do with the page lifecycle. The Page_Load event fires
after the viewstate is loaded but before the update event. When the
grid is rebound to the data, the viewstate values are overwritten and
the edited data is lost (edited data comes from ViewState). So when
the update event is fired later in the cycle, it reads the data and as
you found out, you are left looking at the same values prior to the
edit.

This link in MSDN will show you the order in which events are fired.
Its actually for Mobile web forms but it holds true for standard pages
as well.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.MOBEMBDEV.v1 0.en/mwsdk/html/mwconUnderstand ingtheLifeCycle ofaMobileContro l.htm
r...@rediffmail .com wrote:
Gozirra, I resolved the problem finally. The updated values were not
reflected because the Page_Load sub was calling dgUsers.DataBin d after
every post back. Actually I came across a post which helped me resolve
the issue.

The post said that "If you are re-binding data on Page Load, the
control values would get reset to the original values". I couldn't
exactly understand why do the TextBox values get reset to the original
values if dgUsers.DataBin d is called after every post back.

Could you please throw some light on why does this happen?
rn**@rediffmail .com wrote:
Yeah...you are correct, Gozirra....I was missing the
dgUser.EditItem Index = ea.Item.ItemInd ex line.

Another problem I am facing is in the UpdateCommand event handler of
the DataGrid. When a row in the DataGrid is in the editable mode, the
second, third & fourth cells of that row i.e. FirstName, LastName &
Address will get replaced by TextBoxes. The current row in editable
mode will also display the "Update" & "Cancel" links. This is the code
in the UpdateCommand event handler of the DataGrid:

Sub UpdateUsers(ByV al obj As Object, ByVal ea As
DataGridCommand EventArgs)
'this code only show the code for the
'Address TextBox which is the 4th cell

Dim strAddress As String
Dim txtAddress As TextBox

txtAddress = CType(ea.Item.C ells(3).Control s(0), TextBox)
strAddress = txtAddress.Text

Response.Write( strAddress)

dgUsers.EditIte mIndex = -1
dgUsers.DataBin d()
End Sub

Now assume that the Text in the Address TextBox when the DataGrid is in
the editable mode is "House No. 45, Park Avenue Road". I change it to
"Room No. 65, Marine Lines" & then click the "Update" link. When the
page posts back, the Response.Write( strAddress) still displays the old
address i.e. "House No. 45, Park Avenue Road" & not the updated address
"Room No. 65, Marine Lines".

How do I retrieve the updated address after clicking the "Update" link?

Gozirra wrote:
You must set the EditItemIndex of the grid.
>
In the edit command add this line before binding the grid.
dgUsers.EditIte mIndex = ea.Item.ItemInd ex
>
You could also add the line above to the FillDataGrid sub prior to the
databind as well.
>
Hope this helps.
>
>
rn**@rediffmail .com wrote:
A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:

<script runat="server">
Dim sqlConn As New SqlConnection(" .....")

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub

Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End

Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter

sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")

dSet = New DataSet
sqlDapter.Fill( dSet)

dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>

<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>

<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>

Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.

What am I doing wrong here?
Nov 28 '06 #5
Gozirra, could you pleae clarify a few doubts?

Assume that the DataGrid gets bound in the Page_Load sub after every
post back. When the page loads for the first time, the DataGrid is not
in the editable mode. Assume that the second cell in the first row of
the DataGrid (which is a BoundColumn) is "rn5a". Next I click the
"Edit" link (EditCommandCol umn). The DataGrid changes to the editable
mode & the second cell in the first row changes to a TextBox with the
Text "rn5a" i.e.

CType(ea.Item.C ells(1).Control s(0), TextBox).Text = "rn5a"

The "Edit" link also gets replaced with "Update" & "Cancel" links. Now
the ViewState of the second cell in the first row of the DataGrid is
"rn5a".

Next I change the Text in this TextBox to "Gozirra" & click the
"Update" link. The ViewState changes to "Gozirra" but since the
DataGrid gets re-bound to the data in the Page_Load sub, "Gozirra"
which comes from the ViewState is lost & "Gozirra" gets overwritten
with the initial value "rn5a". In other words, the second cell in the
first row of the DataGrid reverts back to "rn5a". After this, the
UpdateCommand event of the DataGrid fires which finds that
CType(ea.Item.C ells(1).Control s(0), TextBox).Text is still equal to
"rn5a" & hence the final output is that the original value remains &
the updated value is lost. & this happens because on every post back,
the DataGrid is re-bound in the Page_Load sub before retrieving the
updated value. If the DataGrid is not re-bound after every post back in
the Page_Load sub, the UpdateCommand event (which fires after
Page_Load) would ensure that the updated value is first retrieved &
then the DataGrid is re-bound to the data.

Is this how the logic flows? Please correct me if I am wrong?
Gozirra wrote:
I'm glad. I created a small test and everything was working fine. I
was getting ready to ask you if you were rebinding somewhere before the
update event.

It all has to do with the page lifecycle. The Page_Load event fires
after the viewstate is loaded but before the update event. When the
grid is rebound to the data, the viewstate values are overwritten and
the edited data is lost (edited data comes from ViewState). So when
the update event is fired later in the cycle, it reads the data and as
you found out, you are left looking at the same values prior to the
edit.

This link in MSDN will show you the order in which events are fired.
Its actually for Mobile web forms but it holds true for standard pages
as well.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.MOBEMBDEV.v1 0.en/mwsdk/html/mwconUnderstand ingtheLifeCycle ofaMobileContro l.htm
r...@rediffmail .com wrote:
Gozirra, I resolved the problem finally. The updated values were not
reflected because the Page_Load sub was calling dgUsers.DataBin d after
every post back. Actually I came across a post which helped me resolve
the issue.

The post said that "If you are re-binding data on Page Load, the
control values would get reset to the original values". I couldn't
exactly understand why do the TextBox values get reset to the original
values if dgUsers.DataBin d is called after every post back.

Could you please throw some light on why does this happen?
rn**@rediffmail .com wrote:
Yeah...you are correct, Gozirra....I was missing the
dgUser.EditItem Index = ea.Item.ItemInd ex line.
>
Another problem I am facing is in the UpdateCommand event handler of
the DataGrid. When a row in the DataGrid is in the editable mode, the
second, third & fourth cells of that row i.e. FirstName, LastName &
Address will get replaced by TextBoxes. The current row in editable
mode will also display the "Update" & "Cancel" links. This is the code
in the UpdateCommand event handler of the DataGrid:
>
Sub UpdateUsers(ByV al obj As Object, ByVal ea As
DataGridCommand EventArgs)
'this code only show the code for the
'Address TextBox which is the 4th cell
>
Dim strAddress As String
Dim txtAddress As TextBox
>
txtAddress = CType(ea.Item.C ells(3).Control s(0), TextBox)
strAddress = txtAddress.Text
>
Response.Write( strAddress)
>
dgUsers.EditIte mIndex = -1
dgUsers.DataBin d()
End Sub
>
Now assume that the Text in the Address TextBox when the DataGrid is in
the editable mode is "House No. 45, Park Avenue Road". I change it to
"Room No. 65, Marine Lines" & then click the "Update" link. When the
page posts back, the Response.Write( strAddress) still displays the old
address i.e. "House No. 45, Park Avenue Road" & not the updated address
"Room No. 65, Marine Lines".
>
How do I retrieve the updated address after clicking the "Update" link?
>
Gozirra wrote:
You must set the EditItemIndex of the grid.

In the edit command add this line before binding the grid.
dgUsers.EditIte mIndex = ea.Item.ItemInd ex

You could also add the line above to the FillDataGrid sub prior to the
databind as well.

Hope this helps.


rn**@rediffmail .com wrote:
A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:
>
<script runat="server">
Dim sqlConn As New SqlConnection(" .....")
>
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub
>
Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End
>
Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter
>
sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")
>
dSet = New DataSet
sqlDapter.Fill( dSet)
>
dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>
>
<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>
>
<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>
>
<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>
>
<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>
>
Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.
>
What am I doing wrong here?
Nov 28 '06 #6
Gozirra, could you pleae clarify a few doubts?

Assume that the DataGrid gets bound in the Page_Load sub after every
post back. When the page loads for the first time, the DataGrid is not
in the editable mode. Assume that the second cell in the first row of
the DataGrid (which is a BoundColumn) is "rn5a". Next I click the
"Edit" link (EditCommandCol umn). The DataGrid changes to the editable
mode & the second cell in the first row changes to a TextBox with the
Text "rn5a" i.e.

CType(ea.Item.C ells(1).Control s(0), TextBox).Text = "rn5a"

The "Edit" link also gets replaced with "Update" & "Cancel" links. Now
the ViewState of the second cell in the first row of the DataGrid is
"rn5a".

Next I change the Text in this TextBox to "Gozirra" & click the
"Update" link. The ViewState changes to "Gozirra" but since the
DataGrid gets re-bound to the data in the Page_Load sub, "Gozirra"
which comes from the ViewState is lost & "Gozirra" gets overwritten
with the initial value "rn5a". In other words, the second cell in the
first row of the DataGrid reverts back to "rn5a". After this, the
UpdateCommand event of the DataGrid fires which finds that
CType(ea.Item.C ells(1).Control s(0), TextBox).Text is still equal to
"rn5a" & hence the final output is that the original value remains &
the updated value is lost. & this happens because on every post back,
the DataGrid is re-bound in the Page_Load sub before retrieving the
updated value. If the DataGrid is not re-bound after every post back in
the Page_Load sub, the UpdateCommand event (which fires after
Page_Load) would ensure that the updated value is first retrieved &
then the DataGrid is re-bound to the data.

Is this how the logic flows? Please correct me if I am wrong?
Gozirra wrote:
I'm glad. I created a small test and everything was working fine. I
was getting ready to ask you if you were rebinding somewhere before the
update event.

It all has to do with the page lifecycle. The Page_Load event fires
after the viewstate is loaded but before the update event. When the
grid is rebound to the data, the viewstate values are overwritten and
the edited data is lost (edited data comes from ViewState). So when
the update event is fired later in the cycle, it reads the data and as
you found out, you are left looking at the same values prior to the
edit.

This link in MSDN will show you the order in which events are fired.
Its actually for Mobile web forms but it holds true for standard pages
as well.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.MOBEMBDEV.v1 0.en/mwsdk/html/mwconUnderstand ingtheLifeCycle ofaMobileContro l.htm
r...@rediffmail .com wrote:
Gozirra, I resolved the problem finally. The updated values were not
reflected because the Page_Load sub was calling dgUsers.DataBin d after
every post back. Actually I came across a post which helped me resolve
the issue.

The post said that "If you are re-binding data on Page Load, the
control values would get reset to the original values". I couldn't
exactly understand why do the TextBox values get reset to the original
values if dgUsers.DataBin d is called after every post back.

Could you please throw some light on why does this happen?
rn**@rediffmail .com wrote:
Yeah...you are correct, Gozirra....I was missing the
dgUser.EditItem Index = ea.Item.ItemInd ex line.
>
Another problem I am facing is in the UpdateCommand event handler of
the DataGrid. When a row in the DataGrid is in the editable mode, the
second, third & fourth cells of that row i.e. FirstName, LastName &
Address will get replaced by TextBoxes. The current row in editable
mode will also display the "Update" & "Cancel" links. This is the code
in the UpdateCommand event handler of the DataGrid:
>
Sub UpdateUsers(ByV al obj As Object, ByVal ea As
DataGridCommand EventArgs)
'this code only show the code for the
'Address TextBox which is the 4th cell
>
Dim strAddress As String
Dim txtAddress As TextBox
>
txtAddress = CType(ea.Item.C ells(3).Control s(0), TextBox)
strAddress = txtAddress.Text
>
Response.Write( strAddress)
>
dgUsers.EditIte mIndex = -1
dgUsers.DataBin d()
End Sub
>
Now assume that the Text in the Address TextBox when the DataGrid is in
the editable mode is "House No. 45, Park Avenue Road". I change it to
"Room No. 65, Marine Lines" & then click the "Update" link. When the
page posts back, the Response.Write( strAddress) still displays the old
address i.e. "House No. 45, Park Avenue Road" & not the updated address
"Room No. 65, Marine Lines".
>
How do I retrieve the updated address after clicking the "Update" link?
>
Gozirra wrote:
You must set the EditItemIndex of the grid.

In the edit command add this line before binding the grid.
dgUsers.EditIte mIndex = ea.Item.ItemInd ex

You could also add the line above to the FillDataGrid sub prior to the
databind as well.

Hope this helps.


rn**@rediffmail .com wrote:
A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:
>
<script runat="server">
Dim sqlConn As New SqlConnection(" .....")
>
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub
>
Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End
>
Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter
>
sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")
>
dSet = New DataSet
sqlDapter.Fill( dSet)
>
dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>
>
<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>
>
<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>
>
<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>
>
<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>
>
Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.
>
What am I doing wrong here?
Nov 28 '06 #7
You are mostly correct. I want to clarify one thing that I kind of
lazily stated incorrectly before.

ViewState is actually used to repopulate the data of the controls on
postback. So Initialize runs which creates the controls in memory.
Then the state of the new controls are set using the viewstate. Then
any postback data is checked and the controls updated with any changes
that may be neccessary. In our case, the initial load of the viewstate
put the value rn5a into the textbox. It is during the processing of
the postback data that this value is actually changed to Gozirra. Then
page_load and other events follow, including the update. The final
state of the controls before render is saved to the viewstate for use
during the next trip.

I hope that all makes sense. If not, I'll continue to monitor this
thread and try to answer any other questions that you may have. But I
would say that you have a pretty good understanding of what is going on
and how to code appropriately.
rn**@rediffmail .com wrote:
Gozirra, could you pleae clarify a few doubts?

Assume that the DataGrid gets bound in the Page_Load sub after every
post back. When the page loads for the first time, the DataGrid is not
in the editable mode. Assume that the second cell in the first row of
the DataGrid (which is a BoundColumn) is "rn5a". Next I click the
"Edit" link (EditCommandCol umn). The DataGrid changes to the editable
mode & the second cell in the first row changes to a TextBox with the
Text "rn5a" i.e.

CType(ea.Item.C ells(1).Control s(0), TextBox).Text = "rn5a"

The "Edit" link also gets replaced with "Update" & "Cancel" links. Now
the ViewState of the second cell in the first row of the DataGrid is
"rn5a".

Next I change the Text in this TextBox to "Gozirra" & click the
"Update" link. The ViewState changes to "Gozirra" but since the
DataGrid gets re-bound to the data in the Page_Load sub, "Gozirra"
which comes from the ViewState is lost & "Gozirra" gets overwritten
with the initial value "rn5a". In other words, the second cell in the
first row of the DataGrid reverts back to "rn5a". After this, the
UpdateCommand event of the DataGrid fires which finds that
CType(ea.Item.C ells(1).Control s(0), TextBox).Text is still equal to
"rn5a" & hence the final output is that the original value remains &
the updated value is lost. & this happens because on every post back,
the DataGrid is re-bound in the Page_Load sub before retrieving the
updated value. If the DataGrid is not re-bound after every post back in
the Page_Load sub, the UpdateCommand event (which fires after
Page_Load) would ensure that the updated value is first retrieved &
then the DataGrid is re-bound to the data.

Is this how the logic flows? Please correct me if I am wrong?
Gozirra wrote:
I'm glad. I created a small test and everything was working fine. I
was getting ready to ask you if you were rebinding somewhere before the
update event.

It all has to do with the page lifecycle. The Page_Load event fires
after the viewstate is loaded but before the update event. When the
grid is rebound to the data, the viewstate values are overwritten and
the edited data is lost (edited data comes from ViewState). So when
the update event is fired later in the cycle, it reads the data and as
you found out, you are left looking at the same values prior to the
edit.

This link in MSDN will show you the order in which events are fired.
Its actually for Mobile web forms but it holds true for standard pages
as well.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.MOBEMBDEV.v1 0.en/mwsdk/html/mwconUnderstand ingtheLifeCycle ofaMobileContro l.htm
r...@rediffmail .com wrote:
Gozirra, I resolved the problem finally. The updated values were not
reflected because the Page_Load sub was calling dgUsers.DataBin d after
every post back. Actually I came across a post which helped me resolve
the issue.
>
The post said that "If you are re-binding data on Page Load, the
control values would get reset to the original values". I couldn't
exactly understand why do the TextBox values get reset to the original
values if dgUsers.DataBin d is called after every post back.
>
Could you please throw some light on why does this happen?
>
>
rn**@rediffmail .com wrote:
Yeah...you are correct, Gozirra....I was missing the
dgUser.EditItem Index = ea.Item.ItemInd ex line.

Another problem I am facing is in the UpdateCommand event handler of
the DataGrid. When a row in the DataGrid is in the editable mode, the
second, third & fourth cells of that row i.e. FirstName, LastName &
Address will get replaced by TextBoxes. The current row in editable
mode will also display the "Update" & "Cancel" links. This is the code
in the UpdateCommand event handler of the DataGrid:

Sub UpdateUsers(ByV al obj As Object, ByVal ea As
DataGridCommand EventArgs)
'this code only show the code for the
'Address TextBox which is the 4th cell

Dim strAddress As String
Dim txtAddress As TextBox

txtAddress = CType(ea.Item.C ells(3).Control s(0), TextBox)
strAddress = txtAddress.Text

Response.Write( strAddress)

dgUsers.EditIte mIndex = -1
dgUsers.DataBin d()
End Sub

Now assume that the Text in the Address TextBox when the DataGrid is in
the editable mode is "House No. 45, Park Avenue Road". I change it to
"Room No. 65, Marine Lines" & then click the "Update" link. When the
page posts back, the Response.Write( strAddress) still displays the old
address i.e. "House No. 45, Park Avenue Road" & not the updated address
"Room No. 65, Marine Lines".

How do I retrieve the updated address after clicking the "Update" link?

Gozirra wrote:
You must set the EditItemIndex of the grid.
>
In the edit command add this line before binding the grid.
dgUsers.EditIte mIndex = ea.Item.ItemInd ex
>
You could also add the line above to the FillDataGrid sub prior to the
databind as well.
>
Hope this helps.
>
>
rn**@rediffmail .com wrote:
A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:

<script runat="server">
Dim sqlConn As New SqlConnection(" .....")

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub

Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End

Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter

sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")

dSet = New DataSet
sqlDapter.Fill( dSet)

dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>

<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>

<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>

Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.

What am I doing wrong here?
Nov 28 '06 #8
>>But I would say that you have a pretty good understanding of what is going on
>and how to code appropriately.
I am flattered...... .just joking :-)
>I hope that all makes sense. If not, I'll continue to monitor this
thread and try to answer any other questions that you may have.
Well, as of now, I don't have any other queries on this topic but if I
encounter any problems, I will definitely get back to you. & why
wouldn't I? You really seem to have the knack of explaining things very
very lucidly. This isn't the first time you have answered my post & I
have always found that my problems do get resolved as soon as you enter
the "battlefiel d" :-)

Thanks a lot of all your help.

If you don't mind, may I know from which part of the world you are? I
am from Mumbai, India.
Gozirra wrote:
You are mostly correct. I want to clarify one thing that I kind of
lazily stated incorrectly before.

ViewState is actually used to repopulate the data of the controls on
postback. So Initialize runs which creates the controls in memory.
Then the state of the new controls are set using the viewstate. Then
any postback data is checked and the controls updated with any changes
that may be neccessary. In our case, the initial load of the viewstate
put the value rn5a into the textbox. It is during the processing of
the postback data that this value is actually changed to Gozirra. Then
page_load and other events follow, including the update. The final
state of the controls before render is saved to the viewstate for use
during the next trip.

I hope that all makes sense. If not, I'll continue to monitor this
thread and try to answer any other questions that you may have. But I
would say that you have a pretty good understanding of what is going on
and how to code appropriately.
rn**@rediffmail .com wrote:
Gozirra, could you pleae clarify a few doubts?

Assume that the DataGrid gets bound in the Page_Load sub after every
post back. When the page loads for the first time, the DataGrid is not
in the editable mode. Assume that the second cell in the first row of
the DataGrid (which is a BoundColumn) is "rn5a". Next I click the
"Edit" link (EditCommandCol umn). The DataGrid changes to the editable
mode & the second cell in the first row changes to a TextBox with the
Text "rn5a" i.e.

CType(ea.Item.C ells(1).Control s(0), TextBox).Text = "rn5a"

The "Edit" link also gets replaced with "Update" & "Cancel" links. Now
the ViewState of the second cell in the first row of the DataGrid is
"rn5a".

Next I change the Text in this TextBox to "Gozirra" & click the
"Update" link. The ViewState changes to "Gozirra" but since the
DataGrid gets re-bound to the data in the Page_Load sub, "Gozirra"
which comes from the ViewState is lost & "Gozirra" gets overwritten
with the initial value "rn5a". In other words, the second cell in the
first row of the DataGrid reverts back to "rn5a". After this, the
UpdateCommand event of the DataGrid fires which finds that
CType(ea.Item.C ells(1).Control s(0), TextBox).Text is still equal to
"rn5a" & hence the final output is that the original value remains &
the updated value is lost. & this happens because on every post back,
the DataGrid is re-bound in the Page_Load sub before retrieving the
updated value. If the DataGrid is not re-bound after every post back in
the Page_Load sub, the UpdateCommand event (which fires after
Page_Load) would ensure that the updated value is first retrieved &
then the DataGrid is re-bound to the data.

Is this how the logic flows? Please correct me if I am wrong?
Gozirra wrote:
I'm glad. I created a small test and everything was working fine. I
was getting ready to ask you if you were rebinding somewhere before the
update event.
>
It all has to do with the page lifecycle. The Page_Load event fires
after the viewstate is loaded but before the update event. When the
grid is rebound to the data, the viewstate values are overwritten and
the edited data is lost (edited data comes from ViewState). So when
the update event is fired later in the cycle, it reads the data and as
you found out, you are left looking at the same values prior to the
edit.
>
This link in MSDN will show you the order in which events are fired.
Its actually for Mobile web forms but it holds true for standard pages
as well.
>
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.MOBEMBDEV.v1 0.en/mwsdk/html/mwconUnderstand ingtheLifeCycle ofaMobileContro l.htm
>
>
r...@rediffmail .com wrote:
Gozirra, I resolved the problem finally. The updated values were not
reflected because the Page_Load sub was calling dgUsers.DataBin d after
every post back. Actually I came across a post which helped me resolve
the issue.

The post said that "If you are re-binding data on Page Load, the
control values would get reset to the original values". I couldn't
exactly understand why do the TextBox values get reset to the original
values if dgUsers.DataBin d is called after every post back.

Could you please throw some light on why does this happen?


rn**@rediffmail .com wrote:
Yeah...you are correct, Gozirra....I was missing the
dgUser.EditItem Index = ea.Item.ItemInd ex line.
>
Another problem I am facing is in the UpdateCommand event handler of
the DataGrid. When a row in the DataGrid is in the editable mode, the
second, third & fourth cells of that row i.e. FirstName, LastName &
Address will get replaced by TextBoxes. The current row in editable
mode will also display the "Update" & "Cancel" links. This is the code
in the UpdateCommand event handler of the DataGrid:
>
Sub UpdateUsers(ByV al obj As Object, ByVal ea As
DataGridCommand EventArgs)
'this code only show the code for the
'Address TextBox which is the 4th cell
>
Dim strAddress As String
Dim txtAddress As TextBox
>
txtAddress = CType(ea.Item.C ells(3).Control s(0), TextBox)
strAddress = txtAddress.Text
>
Response.Write( strAddress)
>
dgUsers.EditIte mIndex = -1
dgUsers.DataBin d()
End Sub
>
Now assume that the Text in the Address TextBox when the DataGrid is in
the editable mode is "House No. 45, Park Avenue Road". I change it to
"Room No. 65, Marine Lines" & then click the "Update" link. When the
page posts back, the Response.Write( strAddress) still displays the old
address i.e. "House No. 45, Park Avenue Road" & not the updated address
"Room No. 65, Marine Lines".
>
How do I retrieve the updated address after clicking the "Update" link?
>
Gozirra wrote:
You must set the EditItemIndex of the grid.

In the edit command add this line before binding the grid.
dgUsers.EditIte mIndex = ea.Item.ItemInd ex

You could also add the line above to the FillDataGrid sub prior to the
databind as well.

Hope this helps.


rn**@rediffmail .com wrote:
A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:
>
<script runat="server">
Dim sqlConn As New SqlConnection(" .....")
>
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub
>
Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End
>
Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter
>
sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")
>
dSet = New DataSet
sqlDapter.Fill( dSet)
>
dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>
>
<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>
>
<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>
>
<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>
>
<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>
>
Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.
>
What am I doing wrong here?
Nov 28 '06 #9
I appreciate the compliment. I always feel I ramble on more than is
neccessary but its nice to know that I was able to help.

I currently reside in St. Louis, Missouri USA.

rn**@rediffmail .com wrote:
>But I would say that you have a pretty good understanding of what is going on
and how to code appropriately.

I am flattered...... .just joking :-)
I hope that all makes sense. If not, I'll continue to monitor this
thread and try to answer any other questions that you may have.

Well, as of now, I don't have any other queries on this topic but if I
encounter any problems, I will definitely get back to you. & why
wouldn't I? You really seem to have the knack of explaining things very
very lucidly. This isn't the first time you have answered my post & I
have always found that my problems do get resolved as soon as you enter
the "battlefiel d" :-)

Thanks a lot of all your help.

If you don't mind, may I know from which part of the world you are? I
am from Mumbai, India.
Gozirra wrote:
You are mostly correct. I want to clarify one thing that I kind of
lazily stated incorrectly before.

ViewState is actually used to repopulate the data of the controls on
postback. So Initialize runs which creates the controls in memory.
Then the state of the new controls are set using the viewstate. Then
any postback data is checked and the controls updated with any changes
that may be neccessary. In our case, the initial load of the viewstate
put the value rn5a into the textbox. It is during the processing of
the postback data that this value is actually changed to Gozirra. Then
page_load and other events follow, including the update. The final
state of the controls before render is saved to the viewstate for use
during the next trip.

I hope that all makes sense. If not, I'll continue to monitor this
thread and try to answer any other questions that you may have. But I
would say that you have a pretty good understanding of what is going on
and how to code appropriately.
rn**@rediffmail .com wrote:
Gozirra, could you pleae clarify a few doubts?
>
Assume that the DataGrid gets bound in the Page_Load sub after every
post back. When the page loads for the first time, the DataGrid is not
in the editable mode. Assume that the second cell in the first row of
the DataGrid (which is a BoundColumn) is "rn5a". Next I click the
"Edit" link (EditCommandCol umn). The DataGrid changes to the editable
mode & the second cell in the first row changes to a TextBox with the
Text "rn5a" i.e.
>
CType(ea.Item.C ells(1).Control s(0), TextBox).Text = "rn5a"
>
The "Edit" link also gets replaced with "Update" & "Cancel" links. Now
the ViewState of the second cell in the first row of the DataGrid is
"rn5a".
>
Next I change the Text in this TextBox to "Gozirra" & click the
"Update" link. The ViewState changes to "Gozirra" but since the
DataGrid gets re-bound to the data in the Page_Load sub, "Gozirra"
which comes from the ViewState is lost & "Gozirra" gets overwritten
with the initial value "rn5a". In other words, the second cell in the
first row of the DataGrid reverts back to "rn5a". After this, the
UpdateCommand event of the DataGrid fires which finds that
CType(ea.Item.C ells(1).Control s(0), TextBox).Text is still equal to
"rn5a" & hence the final output is that the original value remains &
the updated value is lost. & this happens because on every post back,
the DataGrid is re-bound in the Page_Load sub before retrieving the
updated value. If the DataGrid is not re-bound after every post back in
the Page_Load sub, the UpdateCommand event (which fires after
Page_Load) would ensure that the updated value is first retrieved &
then the DataGrid is re-bound to the data.
>
Is this how the logic flows? Please correct me if I am wrong?
>
>
Gozirra wrote:
I'm glad. I created a small test and everything was working fine. I
was getting ready to ask you if you were rebinding somewhere before the
update event.

It all has to do with the page lifecycle. The Page_Load event fires
after the viewstate is loaded but before the update event. When the
grid is rebound to the data, the viewstate values are overwritten and
the edited data is lost (edited data comes from ViewState). So when
the update event is fired later in the cycle, it reads the data and as
you found out, you are left looking at the same values prior to the
edit.

This link in MSDN will show you the order in which events are fired.
Its actually for Mobile web forms but it holds true for standard pages
as well.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.MOBEMBDEV.v1 0.en/mwsdk/html/mwconUnderstand ingtheLifeCycle ofaMobileContro l.htm


r...@rediffmail .com wrote:
Gozirra, I resolved the problem finally. The updated values were not
reflected because the Page_Load sub was calling dgUsers.DataBin d after
every post back. Actually I came across a post which helped me resolve
the issue.
>
The post said that "If you are re-binding data on Page Load, the
control values would get reset to the original values". I couldn't
exactly understand why do the TextBox values get reset to the original
values if dgUsers.DataBin d is called after every post back.
>
Could you please throw some light on why does this happen?
>
>
rn**@rediffmail .com wrote:
Yeah...you are correct, Gozirra....I was missing the
dgUser.EditItem Index = ea.Item.ItemInd ex line.

Another problem I am facing is in the UpdateCommand event handler of
the DataGrid. When a row in the DataGrid is in the editable mode, the
second, third & fourth cells of that row i.e. FirstName, LastName &
Address will get replaced by TextBoxes. The current row in editable
mode will also display the "Update" & "Cancel" links. This is the code
in the UpdateCommand event handler of the DataGrid:

Sub UpdateUsers(ByV al obj As Object, ByVal ea As
DataGridCommand EventArgs)
'this code only show the code for the
'Address TextBox which is the 4th cell

Dim strAddress As String
Dim txtAddress As TextBox

txtAddress = CType(ea.Item.C ells(3).Control s(0), TextBox)
strAddress = txtAddress.Text

Response.Write( strAddress)

dgUsers.EditIte mIndex = -1
dgUsers.DataBin d()
End Sub

Now assume that the Text in the Address TextBox when the DataGrid is in
the editable mode is "House No. 45, Park Avenue Road". I change it to
"Room No. 65, Marine Lines" & then click the "Update" link. When the
page posts back, the Response.Write( strAddress) still displays the old
address i.e. "House No. 45, Park Avenue Road" & not the updated address
"Room No. 65, Marine Lines".

How do I retrieve the updated address after clicking the "Update" link?

Gozirra wrote:
You must set the EditItemIndex of the grid.
>
In the edit command add this line before binding the grid.
dgUsers.EditIte mIndex = ea.Item.ItemInd ex
>
You could also add the line above to the FillDataGrid sub prior to the
databind as well.
>
Hope this helps.
>
>
rn**@rediffmail .com wrote:
A Form has a DataGrid which displays records from a SQL Server 2005 DB
table. Users can modify the records using this DataGrid for which I am
using EditCommandColu mn in the DataGrid. This is the code:

<script runat="server">
Dim sqlConn As New SqlConnection(" .....")

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
FillDataGrid()
End If
End Sub

Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommand EventArgs)
FillDataGrid(ea .Item.ItemIndex )
End

Sub FillDataGrid(Op tional ByVal EditIndex As Integer = -1)
Dim dSet As DataSet
Dim sqlDapter As SqlDataAdapter

sqlDapter = New SqlDataAdapter( "SELECT * FROM Users", sqlConn)
Response.Write( "EditIndex: " & EditIndex & "<br>")

dSet = New DataSet
sqlDapter.Fill( dSet)

dgUsers.DataSou rce = dSet
dgUsers.DataBin d()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand=" EditData" runat="server">
<Columns>
<asp:TemplateCo lumn HeaderText="Use rName">
<ItemTemplate >
<asp:Label ID="lblUserName " runat="server"> <%#
Container.DataI tem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st Name"/>
<asp:BoundColum n DataField="Last Name" HeaderText="Las t Name"/>
<asp:BoundColum n DataField="Addr ess" HeaderText="Add ress"/>

<asp:EditComman dColumn CancelText="CAN CEL" EditText="EDIT"
HeaderText="Edi t" UpdateText="UPD ATE"/>

<asp:ButtonColu mn CommandName="De lete" HeaderText="Del ete"
Text="DELETE"/>
</Columns>
</asp:DataGrid>
</form>

Now when the page loads for the first time, as expected, the DataGrid
displays all the records with each row accompanied by the "Edit" link.
But when the "Edit" link of any of the rows is clicked, the
BoundColumns (i.e. FirstName, LastName & Address) do not change to
TextBoxes.

What am I doing wrong here?
Nov 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2547
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called "Resource" with the following structure : Resource{,en,fr,es} Yes.. these are the only languages supported actually. A couple of rows in that table would look like this :
2
2338
by: Sky | last post by:
Hello: Another question about trying to wring functionality from a DataGrid... Have a DB table of "Contacts" -- 14 or more fields per record Show in datagrid -- but only 5 columns (First,Last, Fax, Phone, Category). Put an Edit column at the end... Now what?! If you go into Edit mode -- you can only edit 5 cells -- not all the rest of the Record's fields...not enough!
1
4335
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls (watch the layout, some have child controls):
0
473
by: Steve | last post by:
I have a datagrid that is created at run time DataGrid dgG = new DataGrid(); BoundColumn bcB; dgG.CellPadding = 5; dgG.CellSpacing = 0; dgG.GridLines = GridLines.Both; dgG.CssClass = "SectionTableLines"; dgG.DataKeyField = "PlanWorkOrderID";
0
1986
by: Colin Ramsay | last post by:
Hi all, I don't normally post swathes of code like this but I am truly banging my head off my desk here... I've dynamically created a datagrid within a usercontrol. There are two columns which contain buttons to Edit & Delete rows. For some reason, the ItemCommand event which these should be connected to isn't firing.
12
2009
by: Daniel Walzenbach | last post by:
Hi, I want to display a Label in a DataGrid according to some condition. I therefore check whether the condition is true in the ItemDateBound EventHandler of the DataGrid. Unfortunately the conversion is extremely costly in performance. Does anybody know how I could set the label (of the whole content of the TableCell) to .Visible = False without converting e.Item.Controls(2) to a System.Web.UI.WebControls.Label?
4
3496
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a snippet from my .css file: *************************** body { margin:0; padding:0;
5
1442
by: Kurt Schroeder | last post by:
I'm using VS.net. I have created a component with the basic set of database commands. This component returns a record set that is bound to a datagrid. I want to add a dynamic sort, but am not sure how to do this. If the query were on the same webform this would not be a problem, i'd jist add it to the select string. but..... using the component designer in vs.net i'm not sure how to do this, nor am i sure how to call the fill with the sort...
2
3375
by: Deepesh | last post by:
Good day, I have a specific case of the DataGrid in my solution which is causing the ItemCommand Event Not Firing. So I'm creating a "Skinnable" set of controls. I seperate the actual ASCX file and .CS file. When I initialize my .CS file, in that code there is a method that goes: Page.LoadControl(FILENAME) Which associates a .ascx file with my .CS file, allowing me to plug in any
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.