views:

261

answers:

1

I've got a gridview connected to an objectdatasource which is bound to some custom objects in my code (code is below). The problem I'm having is that the value passed into my update method is the old value, not the new value. Thoughts?

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

and the markup:

<%@ 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>
+1  A: 

Just found the solution.

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

Also, discovered that you can't edit the users during execution of the application.

MasterMax1313