ObjectDataSource UpdateMethod 传递更新值

发布于 2024-08-26 11:33:39 字数 3822 浏览 2 评论 0原文

我有一个连接到对象数据源的网格视图,该对象数据源绑定到我的代码中的一些自定义对象(代码如下)。我遇到的问题是传递到更新方法的值是旧值,而不是新值。想法?

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2024-09-02 11:33:39

刚刚找到解决方案。

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

此外,还发现您无法在应用程序执行期间编辑用户。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文