R
Ryan
I am trying to create a custom control that would allow an
administrator to update user email addresses and to lock/unlock
accounts. The problem is that oldvalues and newvalues event arguments
of the onupdate command are blank when the control is used, making it
difficult to determine whether a value change has occured that
requires and update. I have an idea as to a way to do a workaround
using the view state, but I am curious as to what might be going
wrong. I also thought that people might find the code to-date helpful
in some of their projects:
If anyone knows why the oldvalues and newvalues aren't getting passed,
please let me know! Here is the code:
<ToolboxData("<{0}:EditUserControl runat=server></
{0}:EditUserControl>")> _
Public Class EditUserControl
Inherits GridView
Private mbrshpConnectionString As String
Private mbrshpDataSource As New SqlDataSource
Private bfUserID As New BoundField
Private bfUserName As New BoundField
Private bfEmail As New BoundField
Private chkbxfIsLockedOut As New CheckBoxField
Private cfCommand As New CommandField
<Browsable(False)> _
Shadows ReadOnly Property DataMember() As String
Get
Stop
EnsureChildControls()
Return mbrshpDataSource.ID
End Get
End Property
<Browsable(False)> _
Shadows ReadOnly Property DataSourceID() As String
Get
EnsureChildControls()
Return mbrshpDataSource.ID
End Get
End Property
Protected Overrides Sub CreateChildControls()
If Not Page.IsPostBack Then
AutoGenerateColumns = False
mbrshpDataSource.ID = "mbrshpDataSource"
If MyBase.Columns.Count = 0 Then
cfCommand.ButtonType = ButtonType.Button
cfCommand.EditText = "Edit"
cfCommand.UpdateText = "Update"
cfCommand.CancelText = "Cancel"
cfCommand.ShowCancelButton = True
cfCommand.ShowEditButton = True
cfCommand.CausesValidation = False
MyBase.Columns.Add(cfCommand)
bfUserID.HeaderText = "User ID"
bfUserID.Visible = False
bfUserID.DataField = "UserID"
MyBase.Columns.Add(bfUserID)
bfUserName.HeaderText = "Login ID"
bfUserName.DataField = "UserName"
bfUserName.ReadOnly = True
MyBase.Columns.Add(bfUserName)
bfEmail.HeaderText = "Email"
bfEmail.DataField = "Email"
MyBase.Columns.Add(bfEmail)
chkbxfIsLockedOut.HeaderText = "Account Locked"
chkbxfIsLockedOut.DataField = "IsLockedOut"
MyBase.Columns.Add(chkbxfIsLockedOut)
End If
End If
End Sub
Protected Sub OnEdit(ByVal sender As Object, ByVal e As
GridViewEditEventArgs) Handles MyBase.RowEditing
sender.EditIndex = e.NewEditIndex
sender.databind()
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs) Handles MyBase.RowUpdating
Dim i As Integer
If Not DesignMode Then
Page.Response.Write("Here they are:")
For i = 0 To e.OldValues.Count - 1
Page.Response.Write(e.OldValues(i).ToString)
Next
For i = 0 To e.NewValues.Count - 1
Page.Response.Write(e.NewValues(i).ToString)
Next
End If
sender.databind()
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim curPage As System.Web.UI.Page
curPage = Page
If mbrshpConnectionString = "" Then
If Not Me.DesignMode Then
mbrshpConnectionString =
CustomWebControls.ConfigurationAccessMethods.GetDefaultMemberStoreConnectionString(curPage.Request.ApplicationPath)
End If
End If
mbrshpDataSource.ID = "mbrshpDataSource"
mbrshpDataSource.ConnectionString = mbrshpConnectionString
mbrshpDataSource.SelectCommandType =
SqlDataSourceCommandType.StoredProcedure
mbrshpDataSource.SelectCommand =
"dbo.aspnet_Membership_GetAllUsers"
mbrshpDataSource.SelectParameters.Add("ApplicationName",
Membership.ApplicationName)
mbrshpDataSource.SelectParameters.Add("PageIndex",
Me.PageIndex)
mbrshpDataSource.SelectParameters.Add("PageSize",
Me.PageSize)
MyBase.DataSource = mbrshpDataSource
Dim datakeys(0) As String
datakeys(0) = "UserID"
MyBase.DataKeyNames = datakeys
MyBase.EnsureDataBound()
If Not curPage.IsPostBack Then
MyBase.DataBind()
End If
End Sub
End Class
administrator to update user email addresses and to lock/unlock
accounts. The problem is that oldvalues and newvalues event arguments
of the onupdate command are blank when the control is used, making it
difficult to determine whether a value change has occured that
requires and update. I have an idea as to a way to do a workaround
using the view state, but I am curious as to what might be going
wrong. I also thought that people might find the code to-date helpful
in some of their projects:
If anyone knows why the oldvalues and newvalues aren't getting passed,
please let me know! Here is the code:
<ToolboxData("<{0}:EditUserControl runat=server></
{0}:EditUserControl>")> _
Public Class EditUserControl
Inherits GridView
Private mbrshpConnectionString As String
Private mbrshpDataSource As New SqlDataSource
Private bfUserID As New BoundField
Private bfUserName As New BoundField
Private bfEmail As New BoundField
Private chkbxfIsLockedOut As New CheckBoxField
Private cfCommand As New CommandField
<Browsable(False)> _
Shadows ReadOnly Property DataMember() As String
Get
Stop
EnsureChildControls()
Return mbrshpDataSource.ID
End Get
End Property
<Browsable(False)> _
Shadows ReadOnly Property DataSourceID() As String
Get
EnsureChildControls()
Return mbrshpDataSource.ID
End Get
End Property
Protected Overrides Sub CreateChildControls()
If Not Page.IsPostBack Then
AutoGenerateColumns = False
mbrshpDataSource.ID = "mbrshpDataSource"
If MyBase.Columns.Count = 0 Then
cfCommand.ButtonType = ButtonType.Button
cfCommand.EditText = "Edit"
cfCommand.UpdateText = "Update"
cfCommand.CancelText = "Cancel"
cfCommand.ShowCancelButton = True
cfCommand.ShowEditButton = True
cfCommand.CausesValidation = False
MyBase.Columns.Add(cfCommand)
bfUserID.HeaderText = "User ID"
bfUserID.Visible = False
bfUserID.DataField = "UserID"
MyBase.Columns.Add(bfUserID)
bfUserName.HeaderText = "Login ID"
bfUserName.DataField = "UserName"
bfUserName.ReadOnly = True
MyBase.Columns.Add(bfUserName)
bfEmail.HeaderText = "Email"
bfEmail.DataField = "Email"
MyBase.Columns.Add(bfEmail)
chkbxfIsLockedOut.HeaderText = "Account Locked"
chkbxfIsLockedOut.DataField = "IsLockedOut"
MyBase.Columns.Add(chkbxfIsLockedOut)
End If
End If
End Sub
Protected Sub OnEdit(ByVal sender As Object, ByVal e As
GridViewEditEventArgs) Handles MyBase.RowEditing
sender.EditIndex = e.NewEditIndex
sender.databind()
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs) Handles MyBase.RowUpdating
Dim i As Integer
If Not DesignMode Then
Page.Response.Write("Here they are:")
For i = 0 To e.OldValues.Count - 1
Page.Response.Write(e.OldValues(i).ToString)
Next
For i = 0 To e.NewValues.Count - 1
Page.Response.Write(e.NewValues(i).ToString)
Next
End If
sender.databind()
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim curPage As System.Web.UI.Page
curPage = Page
If mbrshpConnectionString = "" Then
If Not Me.DesignMode Then
mbrshpConnectionString =
CustomWebControls.ConfigurationAccessMethods.GetDefaultMemberStoreConnectionString(curPage.Request.ApplicationPath)
End If
End If
mbrshpDataSource.ID = "mbrshpDataSource"
mbrshpDataSource.ConnectionString = mbrshpConnectionString
mbrshpDataSource.SelectCommandType =
SqlDataSourceCommandType.StoredProcedure
mbrshpDataSource.SelectCommand =
"dbo.aspnet_Membership_GetAllUsers"
mbrshpDataSource.SelectParameters.Add("ApplicationName",
Membership.ApplicationName)
mbrshpDataSource.SelectParameters.Add("PageIndex",
Me.PageIndex)
mbrshpDataSource.SelectParameters.Add("PageSize",
Me.PageSize)
MyBase.DataSource = mbrshpDataSource
Dim datakeys(0) As String
datakeys(0) = "UserID"
MyBase.DataKeyNames = datakeys
MyBase.EnsureDataBound()
If Not curPage.IsPostBack Then
MyBase.DataBind()
End If
End Sub
End Class