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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚找到解决方案。
此外,还发现您无法在应用程序执行期间编辑用户。
Just found the solution.
Also, discovered that you can't edit the users during execution of the application.