Datagrid on WebPart: After postback Data is disappearing

Discussion in 'ASP .Net Web Controls' started by Dan, Feb 21, 2007.

  1. Dan

    Dan Guest

    I am having a problem with losing data from my datagrid after it posts back.

    The page runs the populate data sub at the end of CreateChildControls and
    fills my DataGrid. After I postback by clicking the cmdUpdate button, I
    would expect oTable.Items.Count to have the same # it was before I did the
    postback, but instead its zero.

    Thanks for any help...

    Below is some of the sample code:


    Protected WithEvents oTable As New System.Web.UI.WebControls.DataGrid
    Protected WithEvents cmdUpdate As New System.Web.UI.WebControls.LinkButton

    Protected Overrides Sub CreateChildControls()
    oTable.EnableViewState = True
    Me.Controls.Add(oTable)
    Me.Controls.Add(cmdUpdate)
    End Sub

    Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles cmdUpdate.Click
    sDebug = oTable.Items.Count
    End Sub

    Protected Overrides Sub RenderWebPart(ByVal output As
    System.Web.UI.HtmlTextWriter)
    Me.EnsureChildControls()
    PopulateData()
    ......
    End Sub
     
    Dan, Feb 21, 2007
    #1
    1. Advertising

  2. Dan

    Mark Nelson Guest

    Dan,

    I suspect that the event cmdUpdate_Click is not getting fired. Please check
    whether you have defined the event hanlder in the CreateChildControls method
    (where the defniton for the control lives)
    Have you enabled postback for this control ?

    Could you please post me the code for CreateChildControls, i can have a look
    at that/
    The problem is definitely with the events not getting fired inside your
    custom control.
    --
    Thanks & Regards,
    Mark Nelson


    "Dan" wrote:

    > I am having a problem with losing data from my datagrid after it posts back.
    >
    > The page runs the populate data sub at the end of CreateChildControls and
    > fills my DataGrid. After I postback by clicking the cmdUpdate button, I
    > would expect oTable.Items.Count to have the same # it was before I did the
    > postback, but instead its zero.
    >
    > Thanks for any help...
    >
    > Below is some of the sample code:
    >
    >
    > Protected WithEvents oTable As New System.Web.UI.WebControls.DataGrid
    > Protected WithEvents cmdUpdate As New System.Web.UI.WebControls.LinkButton
    >
    > Protected Overrides Sub CreateChildControls()
    > oTable.EnableViewState = True
    > Me.Controls.Add(oTable)
    > Me.Controls.Add(cmdUpdate)
    > End Sub
    >
    > Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles cmdUpdate.Click
    > sDebug = oTable.Items.Count
    > End Sub
    >
    > Protected Overrides Sub RenderWebPart(ByVal output As
    > System.Web.UI.HtmlTextWriter)
    > Me.EnsureChildControls()
    > PopulateData()
    > ......
    > End Sub
    >
     
    Mark Nelson, Feb 22, 2007
    #2
    1. Advertising

  3. Dan

    Dan Guest

    Mark, thanks for the reply, but I know that the event is definately being
    fired as I am outputting the sDebug on the RenderControl:

    here is more of the code:


    Protected WithEvents oTable As New System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblPageXofY As New System.Web.UI.WebControls.Label
    Protected WithEvents lblCurPage As New System.Web.UI.WebControls.TextBox
    Protected WithEvents lblFilter As New System.Web.UI.WebControls.TextBox
    Protected WithEvents lblLastProvider As New
    System.Web.UI.WebControls.TextBox
    Protected WithEvents cmdUpdate As New System.Web.UI.WebControls.LinkButton
    Protected WithEvents lblPrev As New System.Web.UI.WebControls.LinkButton
    Protected WithEvents lblNext As New System.Web.UI.WebControls.LinkButton
    Protected WithEvents dlAlpha As New System.Web.UI.WebControls.DataList


    Dim sDebug As String = ""

    Protected Overrides Sub RenderWebPart(ByVal output As
    System.Web.UI.HtmlTextWriter)
    Try
    cmdUpdate.RenderControl(output)
    output.Write("<BR>")
    oTable.RenderControl(output)
    If sDebug.Length > 0 Then
    output.Write("<BR><span style='color:blue'>" & sDebug &
    "</SPAN><BR>")
    End If
    End Sub

    Protected Overrides Sub CreateChildControls()
    Try
    sDebug = sDebug & "CreateChildControls<BR>"
    AlphaList()
    lblPrev.Text = "Previous"
    lblNext.Text = "Next"
    lblPageXofY.Text = "Page X of Y"
    lblPrev.Font.Bold = True
    lblNext.Font.Bold = True
    dlAlpha.RepeatDirection = RepeatDirection.Horizontal
    dlAlpha.RepeatLayout = RepeatLayout.Flow
    cmdUpdate.Text = "Update"
    oTable.AutoGenerateColumns = False
    Me.Controls.Add(dlAlpha)
    Me.Controls.Add(lblPrev)
    Me.Controls.Add(lblNext)
    Me.Controls.Add(lblPageXofY)
    Me.Controls.Add(oTable)
    Me.Controls.Add(lblCurPage)
    Me.Controls.Add(lblFilter)
    Me.Controls.Add(lblLastProvider)
    Me.Controls.Add(cmdUpdate)
    Catch ex As Exception
    sError = sError & "Child Control:" & ex.Message & "<BR>"
    End Try
    End Sub

    Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles cmdUpdate.Click
    Try
    sDebug = sDebug & "UpdateClick<BR>"
    sDebug = sDebug & "# items:" & oTable.Items.Count & "<BR>"
    Catch ex As Exception

    End Try
    End Sub

    "Mark Nelson" wrote:

    > Dan,
    >
    > I suspect that the event cmdUpdate_Click is not getting fired. Please check
    > whether you have defined the event hanlder in the CreateChildControls method
    > (where the defniton for the control lives)
    > Have you enabled postback for this control ?
    >
    > Could you please post me the code for CreateChildControls, i can have a look
    > at that/
    > The problem is definitely with the events not getting fired inside your
    > custom control.
    > --
    > Thanks & Regards,
    > Mark Nelson
    >
    >
    > "Dan" wrote:
    >
    > > I am having a problem with losing data from my datagrid after it posts back.
    > >
    > > The page runs the populate data sub at the end of CreateChildControls and
    > > fills my DataGrid. After I postback by clicking the cmdUpdate button, I
    > > would expect oTable.Items.Count to have the same # it was before I did the
    > > postback, but instead its zero.
    > >
    > > Thanks for any help...
    > >
    > > Below is some of the sample code:
    > >
    > >
    > > Protected WithEvents oTable As New System.Web.UI.WebControls.DataGrid
    > > Protected WithEvents cmdUpdate As New System.Web.UI.WebControls.LinkButton
    > >
    > > Protected Overrides Sub CreateChildControls()
    > > oTable.EnableViewState = True
    > > Me.Controls.Add(oTable)
    > > Me.Controls.Add(cmdUpdate)
    > > End Sub
    > >
    > > Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As
    > > System.EventArgs) Handles cmdUpdate.Click
    > > sDebug = oTable.Items.Count
    > > End Sub
    > >
    > > Protected Overrides Sub RenderWebPart(ByVal output As
    > > System.Web.UI.HtmlTextWriter)
    > > Me.EnsureChildControls()
    > > PopulateData()
    > > ......
    > > End Sub
    > >
     
    Dan, Feb 22, 2007
    #3
    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. anars
    Replies:
    0
    Views:
    386
    anars
    Jan 7, 2007
  2. =?Utf-8?B?dmFuZGls?=
    Replies:
    2
    Views:
    1,222
    Eliyahu Goldin
    Jan 25, 2007
  3. GJH

    Custom Datagrid disappearing on Postback

    GJH, Dec 4, 2006, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    153
  4. Alex

    Dropdown in webpart lost its state after postback

    Alex, Dec 1, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    474
  5. GJH

    Custom Datagrid disappearing on Postback

    GJH, Dec 4, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    113
Loading...

Share This Page