2010-03-24 69 views
1

我有一個gridview連接到一個objectdatasource,它綁定到我的代碼中的一些自定義對象(代碼如下)。我遇到的問題是傳遞到我的更新方法中的值是舊值,而不是新值。思考?ObjectDataSource UpdateMethod傳遞更新值

Imports System.Configuration 
Imports System.Web.Configuration 
Imports System.Security.Cryptography 
Imports System.Collections.Generic 

Partial Public Class ManageUsersControl 
    Inherits System.Web.UI.UserControl 
    Dim auth As AuthenticationSection 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     Users.DataBind() 
    End Sub 
End Class 

Public Class Users 
    Private sName As String 
    Private sPassword As String 

    Public Sub New() 

    End Sub 

    Public Sub New(ByVal nm As String, ByVal pass As String) 

     Name = nm 
     Password = pass 
    End Sub 

    Public Property Name() As String 
     Get 
      Return sName 
     End Get 
     Set(ByVal value As String) 
      sName = value 
     End Set 
    End Property 

    Public Property Password() As String 
     Get 
      Return sPassword 
     End Get 
     Set(ByVal value As String) 
      sPassword = value 
     End Set 
    End Property 
End Class 

Public Class UserData 
    Dim auth As AuthenticationSection 
    Shared userTable As List(Of Users) 

    Public Sub New() 
     auth = CType(WebConfigurationManager.GetSection("system.web/authentication"), AuthenticationSection) 
    End Sub 

    Public Function CreateData() As List(Of Users) 

     Dim dt As New List(Of Users) 
     For Each user As FormsAuthenticationUser In auth.Forms.Credentials.Users 
      dt.Add(New Users(user.Name, user.Password)) 
     Next 

     userTable = dt 

     Return userTable 
    End Function 

    Public Function SelectMethod() As List(Of Users) 
     If userTable Is Nothing Then 
      Return CreateData() 
     Else 
      Return userTable 
     End If 
    End Function 

    Public Function UpdateMethod(ByVal userInfo As Users) As Integer 
     Dim user As FormsAuthenticationUser = auth.Forms.Credentials.Users(userInfo.Name) 
     Dim pass As String 
     Dim sha As New SHA1CryptoServiceProvider() 
     Dim enc As New System.Text.ASCIIEncoding() 

     pass = enc.GetString(sha.ComputeHash(enc.GetBytes(userInfo.Password))) 
     userTable.Add(New Users(userInfo.Name, pass)) 
     user.Password = pass 
     Return 1 
    End Function 
End Class 

和標記:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ManageUsers.ascx.vb" Inherits="mystuff.ManageUsersControl" %> 

<asp:GridView ID="Users" runat="server" AutoGenerateColumns="False" 
    AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" 
    DataSourceID="UsersData"> 
    <Columns> 
     <asp:BoundField DataField="Name" HeaderText="User Name" /> 
     <asp:TemplateField HeaderText="Password" > 
      <InsertItemTemplate> 
       <asp:TextBox runat="server" ID="InsertPassword" Text='<%# Bind("Password") %>' /> 
      </InsertItemTemplate> 
      <EditItemTemplate> 
       <asp:TextBox runat="server" ID="EditPassword" Text='<%# Bind("Password") %>' /> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label runat="server">*****</asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 
<asp:ObjectDataSource ID="UsersData" 
         DataObjectTypeName="mystuff.Users" 
         UpdateMethod="UpdateMethod" 
         SelectMethod="SelectMethod" 
         TypeName="mystuff.UserData" 
         runat="server" 
    OldValuesParameterFormatString="original_{0}"></asp:ObjectDataSource> 

回答

2

就找到了解決辦法。

Partial Public Class ManageUsersControl 
    Inherits System.Web.UI.UserControl 
    Dim auth As AuthenticationSection 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     If Not IsPostBack Then Users.DataBind() 
    End Sub 
End Class 

此外,發現您不能在執行應用程序期間編輯用戶。