Welcome Guest Search | Active Topics | Sign In | Register

empty AddedItems and ChangedItems in postback Options
Leitor79
Posted: Sunday, October 31, 2010 6:01:04 PM
Rank: Member
Groups: Member

Joined: 10/29/2010
Posts: 11
Hi,

Im an ASP newbie and a EO's very newbie. I'm trying to update my DB after editing my grid. I know that this is a two-step thing. I edit the grid then I should check the grid.AddedItems or grid.ChangedItems for the new and updated items.

I've been searching and reading all the afternoon and I couldn't find the answer. After a postback, bot collection has zero lenght. I couldn't find what I'm doing wrong. I paste my definitions here:

The grid definition:

Quote:
<eo:Grid ID="MyGrid" runat="server" AllowNewItem="True" RunningMode="Client"
BorderColor="#C7D1DF" ColumnHeaderAscImage="00050303" ColumnHeaderDescImage="00050304"
ColumnHeaderDividerImage="00050302" Font-Bold="False" Font-Italic="False" Font-Names="Verdana"
Font-Overline="False" Font-Size="9pt" Font-Strikeout="False" Font-Underline="False"
GridLineColor="199, 209, 223" GridLines="Both" Height="213px" ItemHeight="19"
Width="726px">


The columns:
Quote:

<Columns>
<eo:ButtonColumn Width="25" ButtonType="LinkButton" ButtonText="Go">
</eo:ButtonColumn>
<eo:TextBoxColumn Width="200" HeaderText="Name">
</eo:TextBoxColumn>
<eo:EditCommandColumn CancelText="X" EditText="Edit" UpdateText="V" Width="50">
</eo:EditCommandColumn>
</Columns>


The button (just in case):
Quote:
<asp:Button ID="Button1" runat="server" Text="Button" />


The server event:
Quote:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

System.Diagnostics.Debug.Print("added: " & MyGrid.AddedItems.Length)
System.Diagnostics.Debug.Print("updated: " & MyGrid.ChangedItems.Length)

End Sub



The 2 values printed are always zero.

Of course, I can see the grid, I can edit elements (after clicking the "edit" button) and I can add new elements. The grid is binded with an object collection, I don't know if this is important.


Regards,

eo_support
Posted: Monday, November 1, 2010 9:39:41 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Try to explicitly populate the Grid by setting the Grid's DataSource and DataBind method. That way you can control precisely when the data binding occurs. The problem you are seeing is almost always caused by an unnecessary rebind after the page is posted back. The correct flow should be the data binding only occurs when the page is initially loaded, but not during post back because as long as data binding occurs, any changes you made to the grid will be discarded.

Thanks!
Leitor79
Posted: Monday, November 1, 2010 10:08:49 AM
Rank: Member
Groups: Member

Joined: 10/29/2010
Posts: 11
Hi!

Thank you for your fast answer!

I'm binding my grid to a datasource as you say. On the page_load event I have this code (and nowhere else):

Quote:

MyGrid.DataSource = LoadItems
MyGrid.Columns(1).DataField = "Name"

MyGrid.RecordCount = LoadItems.Count
MyGrid.DataBind()


Just in case, "LoadItems" is a function that returns a System.Collections.ObjectModel.Collection(Of MyItem), and it seems to be working OK as I see the grid populated.

Thank you,








eo_support wrote:
Hi,

Try to explicitly populate the Grid by setting the Grid's DataSource and DataBind method. That way you can control precisely when the data binding occurs. The problem you are seeing is almost always caused by an unnecessary rebind after the page is posted back. The correct flow should be the data binding only occurs when the page is initially loaded, but not during post back because as long as data binding occurs, any changes you made to the grid will be discarded.

Thanks!
eo_support
Posted: Monday, November 1, 2010 10:26:11 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

That's precisely the problem. You should only do data binding inside "If Not Page.IsPostBack". Initial load fires your Page_Load, so does post back. You must distinguish the two scenarios and make sure you only data binding during initial load.

Thanks
Leitor79
Posted: Monday, November 1, 2010 11:28:34 AM
Rank: Member
Groups: Member

Joined: 10/29/2010
Posts: 11
Excellent, that's what happened. What a nice support!



eo_support wrote:
Hi,

That's precisely the problem. You should only do data binding inside "If Not Page.IsPostBack". Initial load fires your Page_Load, so does post back. You must distinguish the two scenarios and make sure you only data binding during initial load.

Thanks


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.