473,763 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="Emplo yee3.aspx.vb"
Inherits="Organ ization_Employe e3" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="EmployeeFor mView" DataSourceID="E mployeeData"
runat="server"
DataKeyNames="O rganizationEmpl oyeeID" DefaultMode="Ed it" >
<EditItemTempla te>
<asp:LinkButt on ID="UpdateButto n" runat="server"
Text="Update" CommandName="Up date" />
<asp:TextBox ID="FirstName" runat="server" Text='<%#
Bind("FirstName ") %>' />
</EditItemTemplat e>
</asp:FormView>
<asp:ObjectData Source ID="EmployeeDat a" runat="server"
TypeName="Ism.P risma.Business. Organization.Em ployeeManager"
SelectMethod="S electOne"
UpdateMethod="U pdateTest" >
<SelectParamete rs>
<asp:QueryStrin gParameter Name="organizat ionEmployeeID"
QueryStringFiel d="Organization EmployeeID" />
</SelectParameter s>
</asp:ObjectDataS ource>
</div>
</form>
</body>
</html>
Public Shared Function UpdateTest(ByVa l organizationEmp loyeeID As
Integer, _
ByVal firstName As String) As Integer

Return -1

End Function

Apr 6 '06 #1
3 3431
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="Emplo yee3.aspx.vb"
Inherits="Organ ization_Employe e3" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="EmployeeFor mView" DataSourceID="E mployeeData"
runat="server"
DataKeyNames="O rganizationEmpl oyeeID" DefaultMode="Ed it" >
<EditItemTempla te>
<asp:LinkButt on ID="UpdateButto n" runat="server"
Text="Update" CommandName="Up date" />
<asp:TextBox ID="FirstName" runat="server" Text='<%#
Bind("FirstName ") %>' />
</EditItemTemplat e>
</asp:FormView>
<asp:ObjectData Source ID="EmployeeDat a" runat="server"
TypeName="Ism.P risma.Business. Organization.Em ployeeManager"
SelectMethod="S electOne"
UpdateMethod="U pdateTest" >
<SelectParamete rs>
<asp:QueryStrin gParameter Name="organizat ionEmployeeID"
QueryStringFiel d="Organization EmployeeID" />
</SelectParameter s>
</asp:ObjectDataS ource>
</div>
</form>
</body>
</html>
Public Shared Function UpdateTest(ByVa l organizationEmp loyeeID 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 programmaticall y 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**********@d iscussions.micr osoft.com> wrote in message
news:87******** *************** ***********@mic rosoft.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="Emplo yee3.aspx.vb"
Inherits="Organ ization_Employe e3" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="EmployeeFor mView" DataSourceID="E mployeeData"
runat="server"
DataKeyNames="O rganizationEmpl oyeeID" DefaultMode="Ed it" >
<EditItemTempla te>
<asp:LinkButt on ID="UpdateButto n" runat="server"
Text="Update" CommandName="Up date" />
<asp:TextBox ID="FirstName" runat="server" Text='<%#
Bind("FirstName ") %>' />
</EditItemTemplat e>
</asp:FormView>
<asp:ObjectData Source ID="EmployeeDat a" runat="server"
TypeName="Ism.P risma.Business. Organization.Em ployeeManager"
SelectMethod="S electOne"
UpdateMethod="U pdateTest" >
<SelectParamete rs>
<asp:QueryStrin gParameter Name="organizat ionEmployeeID"
QueryStringFiel d="Organization EmployeeID" />
</SelectParameter s>
</asp:ObjectDataS ource>
</div>
</form>
</body>
</html>
Public Shared Function UpdateTest(ByVa l organizationEmp loyeeID 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
2649
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 and this causes control B to be created, when this is done the VIEWSTATE is lost for CONTROL B. In the EVENT that causes CONTROL B to be created I have to set
3
1315
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 a button click event I have bps_found=true ViewState("ps_found") = bps_found 'save value in viewstate then a redirect to another page, passing string information. in the page load I have bps_found = ViewState("ps_found") 'get value from...
7
2044
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 them, or how do we get that value? I always have to set the ViewState("FirstName") = txtBox.text myself before a postback is done, otherwise, the value of ViewState("FirstName") is always empty; there doesn't seem to be a way to retrieve that...
6
4416
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 collection seems to break -- or at least that's my theory for now. Here's what I do; to add an item I do that following on the PostBack of a button: 1. Create a Button object and set its properties
5
2198
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 some of the information in the controls cannot be accepted. But then the FormView binds to the datasource again, and the input in all contols is lost. And the user will have to type it all over again. In ASP.NET 1.1 we could simply say: If Not...
4
13540
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 database complains about a primary key violation (which is what I want) and an exception is thrown. The OnInserted event of the SqlDataSource provides access to the exception so, presumably, you can provide nice handling for various errors. For...
0
1230
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 Update method. This behavior is fine when update method succeeds. But the problem is that when for some reason (e.g. a business rule "start date can not be prior to 1/1/2006") business layer rejects the data and Update method returns False (or...
0
2474
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 can't I stop the formview from posting and return back to my page with all my viewstate data intact so the users can correct any NON-VALIDATION issue and re-attempt to update? Why do I need this? Say I want to bounce some of my data against
2
4503
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 Panel becomes visible. pnlGridViewSearchList pnlFormView The problem that I am having is that if I set the FormView Panel's
0
9386
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,...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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,...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
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
5270
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...
1
3917
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
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.