473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

FormView viewstate always gets lost - why?

I asked this question earlier, but unfortunately the two replies I got did
not solve the problem. Here it is again, but now with the code:

After an Update my FormView always loses its viewstate values. The field
values in the FormView are always overwritten by the results of the Update
method in the business layer. No matter what I do, the databind always takes
place, even when I don't want it to.

See the example below. This is a simple form, with just one button and one
textbox. The value that I type in the textbox gets lost after I click on the
Update button. In this example, the business layer Update method consists of
only line of code. It refuses to save the new data because it decides that it
is not valid. The Update method returns -1 (or it can throw an exception,
that doesn't really matter). Afterwards I want the textbox to keep the value
that it contained before the update. But it doesn't. A new databind is
performed and the original data is fetched from the database using the
SelectOne method. How can I prevent this from happening?

The code-behind does not contain any code in this example. I tried
intervening with just about every event that the formview and datasource
controls have. With no results. (I got the suggestion to cancel the
ModeChanging event, but that did not work, and I'm not even changing modes
here.)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Employee3.aspx.vb"
Inherits="Organization_Employee3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="EmployeeFormView" DataSourceID="EmployeeData"
runat="server"
DataKeyNames="OrganizationEmployeeID" DefaultMode="Edit" >
<EditItemTemplate>
<asp:LinkButton ID="UpdateButton" runat="server"
Text="Update" CommandName="Update" />
<asp:TextBox ID="FirstName" runat="server" Text='<%#
Bind("FirstName") %>' />
</EditItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="EmployeeData" runat="server"
TypeName="Ism.Prisma.Business.Organization.Employe eManager"
SelectMethod="SelectOne"
UpdateMethod="UpdateTest" >
<SelectParameters>
<asp:QueryStringParameter Name="organizationEmployeeID"
QueryStringField="OrganizationEmployeeID" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
Public Shared Function UpdateTest(ByVal organizationEmployeeID As
Integer, _
ByVal firstName As String) As Integer

Return -1

End Function

Apr 6 '06 #1
3 3401
The behaviour you describe is expected, sureley the problem is not that the
formview will not keep it's viewstate data, but that you are not validating
the data entered into the textbox, so that it is valid for the datasource.
Once you only allow valid data, your problem goes away because the textbox
will re-bind to the valid data it has just updated the underlying data source
with. I can't understand why you would want the formview to remember the
invalid value that just caused an exception/error.

"Jurgen Appelo" wrote:
I asked this question earlier, but unfortunately the two replies I got did
not solve the problem. Here it is again, but now with the code:

After an Update my FormView always loses its viewstate values. The field
values in the FormView are always overwritten by the results of the Update
method in the business layer. No matter what I do, the databind always takes
place, even when I don't want it to.

See the example below. This is a simple form, with just one button and one
textbox. The value that I type in the textbox gets lost after I click on the
Update button. In this example, the business layer Update method consists of
only line of code. It refuses to save the new data because it decides that it
is not valid. The Update method returns -1 (or it can throw an exception,
that doesn't really matter). Afterwards I want the textbox to keep the value
that it contained before the update. But it doesn't. A new databind is
performed and the original data is fetched from the database using the
SelectOne method. How can I prevent this from happening?

The code-behind does not contain any code in this example. I tried
intervening with just about every event that the formview and datasource
controls have. With no results. (I got the suggestion to cancel the
ModeChanging event, but that did not work, and I'm not even changing modes
here.)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Employee3.aspx.vb"
Inherits="Organization_Employee3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="EmployeeFormView" DataSourceID="EmployeeData"
runat="server"
DataKeyNames="OrganizationEmployeeID" DefaultMode="Edit" >
<EditItemTemplate>
<asp:LinkButton ID="UpdateButton" runat="server"
Text="Update" CommandName="Update" />
<asp:TextBox ID="FirstName" runat="server" Text='<%#
Bind("FirstName") %>' />
</EditItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="EmployeeData" runat="server"
TypeName="Ism.Prisma.Business.Organization.Employe eManager"
SelectMethod="SelectOne"
UpdateMethod="UpdateTest" >
<SelectParameters>
<asp:QueryStringParameter Name="organizationEmployeeID"
QueryStringField="OrganizationEmployeeID" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
Public Shared Function UpdateTest(ByVal organizationEmployeeID As
Integer, _
ByVal firstName As String) As Integer

Return -1

End Function

Apr 6 '06 #2
> The behaviour you describe is expected, sureley the problem is not that the
formview will not keep it's viewstate data, but that you are not validating
the data entered into the textbox, so that it is valid for the datasource.
Once you only allow valid data, your problem goes away because the textbox
will re-bind to the valid data it has just updated the underlying data source
with.
In my opinion it is up to the business layer to decide whether data supplied
by the user interface is valid. For example: the user interface cannot verify
whether a unique code is really unique. The BLL should verify that.

Of course it would be possible to do a separate call into the BLL to ask if
proposed data is valid, and then afterwards do a second call to Update. But I
think that is an ugly solution. And besides, suppose the user interface
programmer forgets to verify the data? The Update method should still enforce
business logic before persisting the data and deny any attempts to save
incorrect data.

I really don't understand why I cannot change the current behavior. It it is
indeed by design it is a flawed design, IMHO.
I can't understand why you would want the formview to remember the
invalid value that just caused an exception/error.


The user should be enable to change the data when it is invalid. It is bad
GUI design to force him to re-enter all information, including the values
that were correct but did not get saved to the database.

Apr 6 '06 #3
I believe that by using the CommandName in your LinkButton and setting it to
"Update", you are triggering a default reaction to the FormView that will
attempt to update your database and then databind the "new" results, which
would mean that the old data will appear if the update fails. Thus, you
need to programmatically control the update which will then require you to
directly databind the data yourself meaning that if the data is invalid, you
would skip the databinding.

I'm still coming up to speed on the FormView control myself, so there are a
lot of things to discover under the hood.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

"Jurgen Appelo" <Ju**********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
I asked this question earlier, but unfortunately the two replies I got did
not solve the problem. Here it is again, but now with the code:

After an Update my FormView always loses its viewstate values. The field
values in the FormView are always overwritten by the results of the Update
method in the business layer. No matter what I do, the databind always
takes
place, even when I don't want it to.

See the example below. This is a simple form, with just one button and one
textbox. The value that I type in the textbox gets lost after I click on
the
Update button. In this example, the business layer Update method consists
of
only line of code. It refuses to save the new data because it decides that
it
is not valid. The Update method returns -1 (or it can throw an exception,
that doesn't really matter). Afterwards I want the textbox to keep the
value
that it contained before the update. But it doesn't. A new databind is
performed and the original data is fetched from the database using the
SelectOne method. How can I prevent this from happening?

The code-behind does not contain any code in this example. I tried
intervening with just about every event that the formview and datasource
controls have. With no results. (I got the suggestion to cancel the
ModeChanging event, but that did not work, and I'm not even changing modes
here.)

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Employee3.aspx.vb"
Inherits="Organization_Employee3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="EmployeeFormView" DataSourceID="EmployeeData"
runat="server"
DataKeyNames="OrganizationEmployeeID" DefaultMode="Edit" >
<EditItemTemplate>
<asp:LinkButton ID="UpdateButton" runat="server"
Text="Update" CommandName="Update" />
<asp:TextBox ID="FirstName" runat="server" Text='<%#
Bind("FirstName") %>' />
</EditItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="EmployeeData" runat="server"
TypeName="Ism.Prisma.Business.Organization.Employe eManager"
SelectMethod="SelectOne"
UpdateMethod="UpdateTest" >
<SelectParameters>
<asp:QueryStringParameter Name="organizationEmployeeID"
QueryStringField="OrganizationEmployeeID" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
Public Shared Function UpdateTest(ByVal organizationEmployeeID As
Integer, _
ByVal firstName As String) As Integer

Return -1

End Function

Apr 13 '06 #4

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

Similar topics

3
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
3
by: Paul | last post by:
Hi I am setting a boolean value to true on a page and writing the results to viewstate Just above the web form designer generated code I have Dim bps_found As Boolean 'visible to this module in...
7
by: et | last post by:
I'm not sure I understand the use of the ViewState. Do I understand correctly that values of controls are automatically held in a hidden control called ViewState? If so, then why can't we get...
6
by: Peter Zolja | last post by:
Hi, I'm building a webcontrol that contains a dynamic list of other controls. My problem is that when I add or remove an item the synchronization between the ViewState and the Controls...
5
by: Jurgen Appelo | last post by:
I'm at a loss here... My FormView control automatically performs a databind at each postback on the server. But in some cases I don't want this to happen. Like when the business layer decides that...
4
by: Rob | last post by:
Hey all, So.. a simple FormView/SqlDataSource to handle inserting records into a table. The table has a primary key that the user enters (eg DiscountCode). If the user enters a duplicate the...
0
by: sanjeev06 | last post by:
When Updating using a FormView and ObjectDataSource, the formview always does the data-binding of its controls and the field values in the FormView are always overwritten by the results of the...
0
by: Jason | last post by:
I'm reading that this is by design.. but It's just killing me. I see all sorts of nice events - onclick, onupdating, onupdating .. I can cancel the update with e.cancel etc... Great. So why...
2
by: sck10 | last post by:
Hello, I have a web page that has a GridView and a FormView, each in its own panel. The GridView shows a list of records in a database. When a row in the GridView is selected the FormView...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.