Custom Control from GridView - OldValues/NewValues not showing

Discussion in 'ASP .Net Web Controls' started by Ryan, Jun 18, 2007.

  1. Ryan

    Ryan Guest

    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
     
    Ryan, Jun 18, 2007
    #1
    1. Advertising

  2. Ryan

    Ryan Guest

    My thought for a work around was to add the old items from my control
    at the edit event and persist them in the control state. This works
    for the old items. But the new values are also not being passed and I
    do not know how to get the values from the control once the update
    event has fired. The control comes over with no rows... Does anyone
    have any ideas as to how I might get the entered values from the
    gridview?
     
    Ryan, Jun 20, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Olbert

    GridView: Empty OldValues, NewValues

    Mark Olbert, Jan 19, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    12,440
    jfortier2
    Jul 22, 2006
  2. loga123
    Replies:
    0
    Views:
    1,484
    loga123
    Jun 23, 2006
  3. Replies:
    1
    Views:
    1,078
  4. xs8899
    Replies:
    0
    Views:
    815
    xs8899
    May 22, 2009
  5. Pulkit Sharma

    Gridview e.OldValues and e.NewValues are nothing

    Pulkit Sharma, Jul 24, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    1,384
    Rachna
    Nov 19, 2007
Loading...

Share This Page