472,956 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,956 software developers and data experts.

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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub

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

Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>

<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>

<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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 2654
You must set the EditItemIndex of the grid.

In the edit command add this line before binding the grid.
dgUsers.EditItemIndex = ea.Item.ItemIndex

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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub

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

Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>

<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>

<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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.EditItemIndex = ea.Item.ItemIndex 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(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
'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.Cells(3).Controls(0), TextBox)
strAddress = txtAddress.Text

Response.Write(strAddress)

dgUsers.EditItemIndex = -1
dgUsers.DataBind()
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.EditItemIndex = ea.Item.ItemIndex

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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub

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

Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>

<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>

<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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.DataBind 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.DataBind 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.EditItemIndex = ea.Item.ItemIndex 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(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
'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.Cells(3).Controls(0), TextBox)
strAddress = txtAddress.Text

Response.Write(strAddress)

dgUsers.EditItemIndex = -1
dgUsers.DataBind()
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.EditItemIndex = ea.Item.ItemIndex

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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub
>
Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
FillDataGrid(ea.Item.ItemIndex)
End
>
Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>
>
<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>
>
<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>
>
<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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.v10.en/mwsdk/html/mwconUnderstandingtheLifeCycleofaMobileControl.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.DataBind 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.DataBind 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.EditItemIndex = ea.Item.ItemIndex 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(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
'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.Cells(3).Controls(0), TextBox)
strAddress = txtAddress.Text

Response.Write(strAddress)

dgUsers.EditItemIndex = -1
dgUsers.DataBind()
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.EditItemIndex = ea.Item.ItemIndex
>
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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub

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

Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>

<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>

<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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 (EditCommandColumn). 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.Cells(1).Controls(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.Cells(1).Controls(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.v10.en/mwsdk/html/mwconUnderstandingtheLifeCycleofaMobileControl.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.DataBind 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.DataBind 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.EditItemIndex = ea.Item.ItemIndex 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(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
'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.Cells(3).Controls(0), TextBox)
strAddress = txtAddress.Text
>
Response.Write(strAddress)
>
dgUsers.EditItemIndex = -1
dgUsers.DataBind()
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.EditItemIndex = ea.Item.ItemIndex

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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub
>
Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
FillDataGrid(ea.Item.ItemIndex)
End
>
Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>
>
<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>
>
<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>
>
<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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 (EditCommandColumn). 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.Cells(1).Controls(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.Cells(1).Controls(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.v10.en/mwsdk/html/mwconUnderstandingtheLifeCycleofaMobileControl.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.DataBind 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.DataBind 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.EditItemIndex = ea.Item.ItemIndex 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(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
'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.Cells(3).Controls(0), TextBox)
strAddress = txtAddress.Text
>
Response.Write(strAddress)
>
dgUsers.EditItemIndex = -1
dgUsers.DataBind()
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.EditItemIndex = ea.Item.ItemIndex

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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub
>
Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
FillDataGrid(ea.Item.ItemIndex)
End
>
Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>
>
<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>
>
<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>
>
<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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 (EditCommandColumn). 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.Cells(1).Controls(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.Cells(1).Controls(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.v10.en/mwsdk/html/mwconUnderstandingtheLifeCycleofaMobileControl.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.DataBind 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.DataBind 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.EditItemIndex = ea.Item.ItemIndex 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(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
'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.Cells(3).Controls(0), TextBox)
strAddress = txtAddress.Text

Response.Write(strAddress)

dgUsers.EditItemIndex = -1
dgUsers.DataBind()
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.EditItemIndex = ea.Item.ItemIndex
>
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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub

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

Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>

<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>

<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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 "battlefield" :-)

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 (EditCommandColumn). 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.Cells(1).Controls(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.Cells(1).Controls(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.v10.en/mwsdk/html/mwconUnderstandingtheLifeCycleofaMobileControl.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.DataBind 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.DataBind 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.EditItemIndex = ea.Item.ItemIndex 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(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
'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.Cells(3).Controls(0), TextBox)
strAddress = txtAddress.Text
>
Response.Write(strAddress)
>
dgUsers.EditItemIndex = -1
dgUsers.DataBind()
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.EditItemIndex = ea.Item.ItemIndex

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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub
>
Sub EditData(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
FillDataGrid(ea.Item.ItemIndex)
End
>
Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>
>
<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>
>
<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>
>
<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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 "battlefield" :-)

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 (EditCommandColumn). 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.Cells(1).Controls(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.Cells(1).Controls(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.v10.en/mwsdk/html/mwconUnderstandingtheLifeCycleofaMobileControl.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.DataBind 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.DataBind 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.EditItemIndex = ea.Item.ItemIndex 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(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
'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.Cells(3).Controls(0), TextBox)
strAddress = txtAddress.Text

Response.Write(strAddress)

dgUsers.EditItemIndex = -1
dgUsers.DataBind()
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.EditItemIndex = ea.Item.ItemIndex
>
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 EditCommandColumn 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.IsPostBack) Then
FillDataGrid()
End If
End Sub

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

Sub FillDataGrid(Optional 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.DataSource = dSet
dgUsers.DataBind()
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditData" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server"><%#
Container.DataItem("UserName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="FirstName" HeaderText="First Name"/>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"/>
<asp:BoundColumn DataField="Address" HeaderText="Address"/>

<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="Edit" UpdateText="UPDATE"/>

<asp:ButtonColumn CommandName="Delete" HeaderText="Delete"
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
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...
2
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,...
1
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...
0
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 =...
0
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...
12
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...
4
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...
5
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...
2
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.