G
Guest
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.EmployeeManager"
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
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.EmployeeManager"
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