[2005] Viewstate and datasets
I am having trouble wrapping my head around the viewstate usage. Let's say I have a dataset I need to keep between various postbacks, why would this not work?
Assuming a gridview set up as such:
HTML Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Style="z-index: 100;
left: 103px; position: absolute; top: 67px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="ClientName" HeaderText="ClientName" SortExpression="ClientName" />
<asp:BoundField DataField="ClientRate" HeaderText="ClientRate" SortExpression="ClientRate" />
<asp:BoundField DataField="ClientHours" HeaderText="ClientHours" SortExpression="ClientHours" />
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Button ID="but" Text="Delete Row" CommandName = "Delete" runat ="server"/>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
When I click the delete button, I want to put the dataset in the viewstate. What I find happens when I step through with the debugger is that the page load event fires before the rowdeleting event of the gridview. It is in the row deleting event I manipulate the dataset and store it in the viewstate. However, this obviously doesn't work as by the time the load event fires, my dataset is set to nothing. How should I be handling this?
Re: [2005] Viewstate and datasets
You don't need to put the dataset in viewstate the grid will have veiwstate enable by default. You can put the dataset & grid databind code in a seperate sub and call it at the appropiate time to show changed data like
Code:
sub page_load(.....)
if not postback then
bindGrid()
end if
end sub
private sub bindGrid()
'get data
'bind grid
end sub
sub grid_rowdeleting(.....)
'delete record in DB
bindGrid() 'get changed records
end sub
Is this what you need?
Re: [2005] Viewstate and datasets
Well, that would work, except I need the user to be able to undo deletions. Say for instance on load I read a set of records for this user from the database. The user can edit them and delete, but the transaction isn't committed until save it pressed. So, if the user hits cancel, the information in the dataset is disregarded. Otherwise, I keep a generic list of strings with record ID's that are deleted if the user hits save. (this would also I assume go into viewstate at the same time)
Re: [2005] Viewstate and datasets
The easiest option is only have a save button and all grid rows are editable also have a checkbox they can tick to delete that row. When they click save delete all checked rows and update the rest.
It's possible to do what you say, I think it's a more win forms approach though.
Having said that It seams only the page_load was interfearing with your viewstate plans so just limit it to not ispostback the first time then use ispostback to load the dataset from viewstate and update/delete rows from it in the grid events and rebind the grid to it
Code:
private DS as dataset 'page scope
sub page_load.....
if not page ispostback then
'fill DS, viewstate and bind first time
else
'fill DS from viewstate - fires on post back before other events
end if
end sub
sub grid_edit....
'update DS with edited grid row
'save DS to viewstate
'bindgrid to DS
end
sub grid_delete...
'delete row from DS
'save DS to viewstate
'bindgrid to DS
end sub
sub btn_save....
'do your database stuff with DS
end sub