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>

person CodeMonkey1313    schedule 24.03.2010    source источник


Ответы (1)


Только что нашел решение.

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

Также обнаружил, что нельзя редактировать пользователей во время выполнения приложения.

person CodeMonkey1313    schedule 24.03.2010