Custom Datagrid disappearing on Postback

Discussion in 'ASP .Net Building Controls' started by GJH, Dec 4, 2006.

  1. GJH

    GJH Guest

    I have a Web Custom Control datagrid written in VB.NET and when the page
    does a postback from a Selected Index or Sort command the datagrid
    disappears??? anyone know why?
    thanks

    here is the control's code:

    Imports System.ComponentModel

    Imports System.Web.UI

    <DefaultProperty("Text"), ToolboxData("<{0}:dgTest
    runat=server></{0}:dgTest>")> Public Class dgTest

    Inherits System.Web.UI.WebControls.DataGrid

    Dim _text As String

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]()
    As String

    Get

    Return _text

    End Get

    Set(ByVal Value As String)

    _text = Value

    End Set

    End Property

    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)

    Me.EnsureChildControls()

    MyBase.Render(output)

    End Sub

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)

    Me.EnsureChildControls()

    End Sub

    End Class




    Here is my webpage

    Dim mySort As String

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    If Not Page.IsPostBack Then

    BindList()

    End If

    End Sub

    Private Sub BindList(Optional ByVal sort As String = "")

    Dim query As String = "select * from employees"

    Dim da As New SqlDataAdapter(query, cnn)

    Dim ds As New DataSet

    Try

    da.Fill(ds, "employees")

    ds.Tables(0).DefaultView.Sort = sort



    DgTest1.DataSource = ds

    DgTest1.DataMember = "employees"

    DgTest1.DataBind()

    Catch ex As Exception

    Response.Write(ex.Message)

    End Try

    End Sub

    Private Sub GJHAddToCartDataGrid1_SortCommand(ByVal source As Object, ByVal
    e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
    GJHAddToCartDataGrid1.SortCommand

    mySort = e.SortExpression & " ASC"

    BindList(mySort)

    End Sub

    Private Sub DgTest1_SelectedIndexChanged(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles DgTest1.SelectedIndexChanged

    Response.Write("hi")

    End Sub
    GJH, Dec 4, 2006
    #1
    1. Advertising

  2. GJH

    Jeff Guest

    My guess would be that since youre populating your datagrid in the code
    behind, that the data is not being saved in the viewstate.

    What happens when you take the if statement out of the Page.Load
    handler and just call BindList every time the page loads?

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    BindList()

    End Sub



    GJH wrote:
    > I have a Web Custom Control datagrid written in VB.NET and when the page
    > does a postback from a Selected Index or Sort command the datagrid
    > disappears??? anyone know why?
    > thanks
    >
    > here is the control's code:
    >
    > Imports System.ComponentModel
    >
    > Imports System.Web.UI
    >
    > <DefaultProperty("Text"), ToolboxData("<{0}:dgTest
    > runat=server></{0}:dgTest>")> Public Class dgTest
    >
    > Inherits System.Web.UI.WebControls.DataGrid
    >
    > Dim _text As String
    >
    > <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]()
    > As String
    >
    > Get
    >
    > Return _text
    >
    > End Get
    >
    > Set(ByVal Value As String)
    >
    > _text = Value
    >
    > End Set
    >
    > End Property
    >
    > Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
    >
    > Me.EnsureChildControls()
    >
    > MyBase.Render(output)
    >
    > End Sub
    >
    > Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    >
    > Me.EnsureChildControls()
    >
    > End Sub
    >
    > End Class
    >
    >
    >
    >
    > Here is my webpage
    >
    > Dim mySort As String
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >
    > If Not Page.IsPostBack Then
    >
    > BindList()
    >
    > End If
    >
    > End Sub
    >
    > Private Sub BindList(Optional ByVal sort As String = "")
    >
    > Dim query As String = "select * from employees"
    >
    > Dim da As New SqlDataAdapter(query, cnn)
    >
    > Dim ds As New DataSet
    >
    > Try
    >
    > da.Fill(ds, "employees")
    >
    > ds.Tables(0).DefaultView.Sort = sort
    >
    >
    >
    > DgTest1.DataSource = ds
    >
    > DgTest1.DataMember = "employees"
    >
    > DgTest1.DataBind()
    >
    > Catch ex As Exception
    >
    > Response.Write(ex.Message)
    >
    > End Try
    >
    > End Sub
    >
    > Private Sub GJHAddToCartDataGrid1_SortCommand(ByVal source As Object, ByVal
    > e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
    > GJHAddToCartDataGrid1.SortCommand
    >
    > mySort = e.SortExpression & " ASC"
    >
    > BindList(mySort)
    >
    > End Sub
    >
    > Private Sub DgTest1_SelectedIndexChanged(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles DgTest1.SelectedIndexChanged
    >
    > Response.Write("hi")
    >
    > End Sub
    Jeff, Dec 4, 2006
    #2
    1. Advertising

  3. GJH

    GJH Guest

    Thanks Jeff,
    if I remove the "IF" in the page load statement then yes it works, but then
    I am hitting the database on every postback.
    With the MS datagrdd I can just fill the dataset/bind the grid on the
    initial page load in the IF Not IsPostBack statement

    What is my datagrid missing thats different from MS standard datagrid?

    thanks,
    Greg


    "Jeff" <> wrote in message
    news:...
    > My guess would be that since youre populating your datagrid in the code
    > behind, that the data is not being saved in the viewstate.
    >
    > What happens when you take the if statement out of the Page.Load
    > handler and just call BindList every time the page loads?
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >
    > BindList()
    >
    > End Sub
    >
    >
    >
    > GJH wrote:
    >> I have a Web Custom Control datagrid written in VB.NET and when the page
    >> does a postback from a Selected Index or Sort command the datagrid
    >> disappears??? anyone know why?
    >> thanks
    >>
    >> here is the control's code:
    >>
    >> Imports System.ComponentModel
    >>
    >> Imports System.Web.UI
    >>
    >> <DefaultProperty("Text"), ToolboxData("<{0}:dgTest
    >> runat=server></{0}:dgTest>")> Public Class dgTest
    >>
    >> Inherits System.Web.UI.WebControls.DataGrid
    >>
    >> Dim _text As String
    >>
    >> <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    >> [Text]()
    >> As String
    >>
    >> Get
    >>
    >> Return _text
    >>
    >> End Get
    >>
    >> Set(ByVal Value As String)
    >>
    >> _text = Value
    >>
    >> End Set
    >>
    >> End Property
    >>
    >> Protected Overrides Sub Render(ByVal output As
    >> System.Web.UI.HtmlTextWriter)
    >>
    >> Me.EnsureChildControls()
    >>
    >> MyBase.Render(output)
    >>
    >> End Sub
    >>
    >> Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    >>
    >> Me.EnsureChildControls()
    >>
    >> End Sub
    >>
    >> End Class
    >>
    >>
    >>
    >>
    >> Here is my webpage
    >>
    >> Dim mySort As String
    >>
    >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    >> System.EventArgs) Handles MyBase.Load
    >>
    >> If Not Page.IsPostBack Then
    >>
    >> BindList()
    >>
    >> End If
    >>
    >> End Sub
    >>
    >> Private Sub BindList(Optional ByVal sort As String = "")
    >>
    >> Dim query As String = "select * from employees"
    >>
    >> Dim da As New SqlDataAdapter(query, cnn)
    >>
    >> Dim ds As New DataSet
    >>
    >> Try
    >>
    >> da.Fill(ds, "employees")
    >>
    >> ds.Tables(0).DefaultView.Sort = sort
    >>
    >>
    >>
    >> DgTest1.DataSource = ds
    >>
    >> DgTest1.DataMember = "employees"
    >>
    >> DgTest1.DataBind()
    >>
    >> Catch ex As Exception
    >>
    >> Response.Write(ex.Message)
    >>
    >> End Try
    >>
    >> End Sub
    >>
    >> Private Sub GJHAddToCartDataGrid1_SortCommand(ByVal source As Object,
    >> ByVal
    >> e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
    >> GJHAddToCartDataGrid1.SortCommand
    >>
    >> mySort = e.SortExpression & " ASC"
    >>
    >> BindList(mySort)
    >>
    >> End Sub
    >>
    >> Private Sub DgTest1_SelectedIndexChanged(ByVal sender As System.Object,
    >> ByVal e As System.EventArgs) Handles DgTest1.SelectedIndexChanged
    >>
    >> Response.Write("hi")
    >>
    >> End Sub

    >
    GJH, Dec 4, 2006
    #3
  4. GJH

    Jeff Guest

    You can save the data into the ViewState object. Basically, the
    viewstate is an object that holds all the information for each of the
    controls on the page across postbacks. Then on postback, instead of
    loading from the database, you load from the ViewState.

    Not being familiar with your application, I cant say exactly what would
    be the best way to accomplish this. Off the top of my head, you might
    want to try to load the database information into a DataTable, bind the
    DataGrid to the Table, then save the DataTable into the viewstate. On
    postback, load from the DataTable.

    GJH wrote:
    > Thanks Jeff,
    > if I remove the "IF" in the page load statement then yes it works, but then
    > I am hitting the database on every postback.
    > With the MS datagrdd I can just fill the dataset/bind the grid on the
    > initial page load in the IF Not IsPostBack statement
    >
    > What is my datagrid missing thats different from MS standard datagrid?
    >
    > thanks,
    > Greg
    >
    >
    > "Jeff" <> wrote in message
    > news:...
    > > My guess would be that since youre populating your datagrid in the code
    > > behind, that the data is not being saved in the viewstate.
    > >
    > > What happens when you take the if statement out of the Page.Load
    > > handler and just call BindList every time the page loads?
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > >
    > > BindList()
    > >
    > > End Sub
    > >
    > >
    > >
    > > GJH wrote:
    > >> I have a Web Custom Control datagrid written in VB.NET and when the page
    > >> does a postback from a Selected Index or Sort command the datagrid
    > >> disappears??? anyone know why?
    > >> thanks
    > >>
    > >> here is the control's code:
    > >>
    > >> Imports System.ComponentModel
    > >>
    > >> Imports System.Web.UI
    > >>
    > >> <DefaultProperty("Text"), ToolboxData("<{0}:dgTest
    > >> runat=server></{0}:dgTest>")> Public Class dgTest
    > >>
    > >> Inherits System.Web.UI.WebControls.DataGrid
    > >>
    > >> Dim _text As String
    > >>
    > >> <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    > >> [Text]()
    > >> As String
    > >>
    > >> Get
    > >>
    > >> Return _text
    > >>
    > >> End Get
    > >>
    > >> Set(ByVal Value As String)
    > >>
    > >> _text = Value
    > >>
    > >> End Set
    > >>
    > >> End Property
    > >>
    > >> Protected Overrides Sub Render(ByVal output As
    > >> System.Web.UI.HtmlTextWriter)
    > >>
    > >> Me.EnsureChildControls()
    > >>
    > >> MyBase.Render(output)
    > >>
    > >> End Sub
    > >>
    > >> Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    > >>
    > >> Me.EnsureChildControls()
    > >>
    > >> End Sub
    > >>
    > >> End Class
    > >>
    > >>
    > >>
    > >>
    > >> Here is my webpage
    > >>
    > >> Dim mySort As String
    > >>
    > >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > >> System.EventArgs) Handles MyBase.Load
    > >>
    > >> If Not Page.IsPostBack Then
    > >>
    > >> BindList()
    > >>
    > >> End If
    > >>
    > >> End Sub
    > >>
    > >> Private Sub BindList(Optional ByVal sort As String = "")
    > >>
    > >> Dim query As String = "select * from employees"
    > >>
    > >> Dim da As New SqlDataAdapter(query, cnn)
    > >>
    > >> Dim ds As New DataSet
    > >>
    > >> Try
    > >>
    > >> da.Fill(ds, "employees")
    > >>
    > >> ds.Tables(0).DefaultView.Sort = sort
    > >>
    > >>
    > >>
    > >> DgTest1.DataSource = ds
    > >>
    > >> DgTest1.DataMember = "employees"
    > >>
    > >> DgTest1.DataBind()
    > >>
    > >> Catch ex As Exception
    > >>
    > >> Response.Write(ex.Message)
    > >>
    > >> End Try
    > >>
    > >> End Sub
    > >>
    > >> Private Sub GJHAddToCartDataGrid1_SortCommand(ByVal source As Object,
    > >> ByVal
    > >> e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
    > >> GJHAddToCartDataGrid1.SortCommand
    > >>
    > >> mySort = e.SortExpression & " ASC"
    > >>
    > >> BindList(mySort)
    > >>
    > >> End Sub
    > >>
    > >> Private Sub DgTest1_SelectedIndexChanged(ByVal sender As System.Object,
    > >> ByVal e As System.EventArgs) Handles DgTest1.SelectedIndexChanged
    > >>
    > >> Response.Write("hi")
    > >>
    > >> End Sub

    > >
    Jeff, Dec 4, 2006
    #4
  5. GJH

    GJH Guest

    Thanks again Jeff,
    Thats what I ended up doing was saving to the viewstate. my only thing is,
    its not acting like a standard datagrid like Microsoft's does. usually I
    just create a function that fills the dataset and binds the grid and I dont
    have to keep calling the bindgrid() part ever page load.

    In my dgTest.vb file, am I missing code? Do I need to enable the ViewState
    here to keep the grid visible?

    Imports System.ComponentModel
    Imports System.Web.UI
    <DefaultProperty("Text"), ToolboxData("<{0}:dgTest
    runat=server></{0}:dgTest>")> Public Class dgTest
    Inherits System.Web.UI.WebControls.DataGrid
    Dim _text As String
    <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]()
    As String
    Get
    Return _text
    End Get
    Set(ByVal Value As String)
    _text = Value
    End Set
    End Property

    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)

    Me.EnsureChildControls()

    MyBase.Render(output)

    End Sub

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)

    Me.EnsureChildControls()

    End Sub







    "Jeff" <> wrote in message
    news:...
    > You can save the data into the ViewState object. Basically, the
    > viewstate is an object that holds all the information for each of the
    > controls on the page across postbacks. Then on postback, instead of
    > loading from the database, you load from the ViewState.
    >
    > Not being familiar with your application, I cant say exactly what would
    > be the best way to accomplish this. Off the top of my head, you might
    > want to try to load the database information into a DataTable, bind the
    > DataGrid to the Table, then save the DataTable into the viewstate. On
    > postback, load from the DataTable.
    >
    > GJH wrote:
    >> Thanks Jeff,
    >> if I remove the "IF" in the page load statement then yes it works, but
    >> then
    >> I am hitting the database on every postback.
    >> With the MS datagrdd I can just fill the dataset/bind the grid on the
    >> initial page load in the IF Not IsPostBack statement
    >>
    >> What is my datagrid missing thats different from MS standard datagrid?
    >>
    >> thanks,
    >> Greg
    >>
    >>
    >> "Jeff" <> wrote in message
    >> news:...
    >> > My guess would be that since youre populating your datagrid in the code
    >> > behind, that the data is not being saved in the viewstate.
    >> >
    >> > What happens when you take the if statement out of the Page.Load
    >> > handler and just call BindList every time the page loads?
    >> >
    >> > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    >> > System.EventArgs) Handles MyBase.Load
    >> >
    >> > BindList()
    >> >
    >> > End Sub
    >> >
    >> >
    >> >
    >> > GJH wrote:
    >> >> I have a Web Custom Control datagrid written in VB.NET and when the
    >> >> page
    >> >> does a postback from a Selected Index or Sort command the datagrid
    >> >> disappears??? anyone know why?
    >> >> thanks
    >> >>
    >> >> here is the control's code:
    >> >>
    >> >> Imports System.ComponentModel
    >> >>
    >> >> Imports System.Web.UI
    >> >>
    >> >> <DefaultProperty("Text"), ToolboxData("<{0}:dgTest
    >> >> runat=server></{0}:dgTest>")> Public Class dgTest
    >> >>
    >> >> Inherits System.Web.UI.WebControls.DataGrid
    >> >>
    >> >> Dim _text As String
    >> >>
    >> >> <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    >> >> [Text]()
    >> >> As String
    >> >>
    >> >> Get
    >> >>
    >> >> Return _text
    >> >>
    >> >> End Get
    >> >>
    >> >> Set(ByVal Value As String)
    >> >>
    >> >> _text = Value
    >> >>
    >> >> End Set
    >> >>
    >> >> End Property
    >> >>
    >> >> Protected Overrides Sub Render(ByVal output As
    >> >> System.Web.UI.HtmlTextWriter)
    >> >>
    >> >> Me.EnsureChildControls()
    >> >>
    >> >> MyBase.Render(output)
    >> >>
    >> >> End Sub
    >> >>
    >> >> Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    >> >>
    >> >> Me.EnsureChildControls()
    >> >>
    >> >> End Sub
    >> >>
    >> >> End Class
    >> >>
    >> >>
    >> >>
    >> >>
    >> >> Here is my webpage
    >> >>
    >> >> Dim mySort As String
    >> >>
    >> >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    >> >> System.EventArgs) Handles MyBase.Load
    >> >>
    >> >> If Not Page.IsPostBack Then
    >> >>
    >> >> BindList()
    >> >>
    >> >> End If
    >> >>
    >> >> End Sub
    >> >>
    >> >> Private Sub BindList(Optional ByVal sort As String = "")
    >> >>
    >> >> Dim query As String = "select * from employees"
    >> >>
    >> >> Dim da As New SqlDataAdapter(query, cnn)
    >> >>
    >> >> Dim ds As New DataSet
    >> >>
    >> >> Try
    >> >>
    >> >> da.Fill(ds, "employees")
    >> >>
    >> >> ds.Tables(0).DefaultView.Sort = sort
    >> >>
    >> >>
    >> >>
    >> >> DgTest1.DataSource = ds
    >> >>
    >> >> DgTest1.DataMember = "employees"
    >> >>
    >> >> DgTest1.DataBind()
    >> >>
    >> >> Catch ex As Exception
    >> >>
    >> >> Response.Write(ex.Message)
    >> >>
    >> >> End Try
    >> >>
    >> >> End Sub
    >> >>
    >> >> Private Sub GJHAddToCartDataGrid1_SortCommand(ByVal source As Object,
    >> >> ByVal
    >> >> e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
    >> >> GJHAddToCartDataGrid1.SortCommand
    >> >>
    >> >> mySort = e.SortExpression & " ASC"
    >> >>
    >> >> BindList(mySort)
    >> >>
    >> >> End Sub
    >> >>
    >> >> Private Sub DgTest1_SelectedIndexChanged(ByVal sender As
    >> >> System.Object,
    >> >> ByVal e As System.EventArgs) Handles DgTest1.SelectedIndexChanged
    >> >>
    >> >> Response.Write("hi")
    >> >>
    >> >> End Sub
    >> >

    >
    GJH, Dec 4, 2006
    #5
  6. GJH

    Jeff Guest

    >From that code, it doesnt look like you are saving the data in the
    viewstate. to do so, you need to override the LoadViewState and
    SaveViewState subroutines.

    I was going to try to give you a relevant code snippet but I think that
    might be more confusing than anything else. If this is what youre
    dealing with, you might want to try googling around for a tutorial to
    overload these functions. I cant seem to find the one I learned from.


    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    If Not savedState Is Nothing Then
    MyBase.LoadViewState(savedState)
    End If
    End Sub



    Protected Overrides Function SaveViewState() As Object
    Dim baseState As Object = MyBase.SaveViewState()

    If baseState Is Nothing Then
    Return Nothing
    End If

    Return baseState
    End Function


    GJH wrote:
    > Thanks again Jeff,
    > Thats what I ended up doing was saving to the viewstate. my only thing is,
    > its not acting like a standard datagrid like Microsoft's does. usually I
    > just create a function that fills the dataset and binds the grid and I dont
    > have to keep calling the bindgrid() part ever page load.
    >
    > In my dgTest.vb file, am I missing code? Do I need to enable the ViewState
    > here to keep the grid visible?
    >
    > Imports System.ComponentModel
    > Imports System.Web.UI
    > <DefaultProperty("Text"), ToolboxData("<{0}:dgTest
    > runat=server></{0}:dgTest>")> Public Class dgTest
    > Inherits System.Web.UI.WebControls.DataGrid
    > Dim _text As String
    > <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]()
    > As String
    > Get
    > Return _text
    > End Get
    > Set(ByVal Value As String)
    > _text = Value
    > End Set
    > End Property
    >
    > Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
    >
    > Me.EnsureChildControls()
    >
    > MyBase.Render(output)
    >
    > End Sub
    >
    > Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    >
    > Me.EnsureChildControls()
    >
    > End Sub
    >
    >
    >
    >
    >
    >
    >
    > "Jeff" <> wrote in message
    > news:...
    > > You can save the data into the ViewState object. Basically, the
    > > viewstate is an object that holds all the information for each of the
    > > controls on the page across postbacks. Then on postback, instead of
    > > loading from the database, you load from the ViewState.
    > >
    > > Not being familiar with your application, I cant say exactly what would
    > > be the best way to accomplish this. Off the top of my head, you might
    > > want to try to load the database information into a DataTable, bind the
    > > DataGrid to the Table, then save the DataTable into the viewstate. On
    > > postback, load from the DataTable.
    > >
    > > GJH wrote:
    > >> Thanks Jeff,
    > >> if I remove the "IF" in the page load statement then yes it works, but
    > >> then
    > >> I am hitting the database on every postback.
    > >> With the MS datagrdd I can just fill the dataset/bind the grid on the
    > >> initial page load in the IF Not IsPostBack statement
    > >>
    > >> What is my datagrid missing thats different from MS standard datagrid?
    > >>
    > >> thanks,
    > >> Greg
    > >>
    > >>
    > >> "Jeff" <> wrote in message
    > >> news:...
    > >> > My guess would be that since youre populating your datagrid in the code
    > >> > behind, that the data is not being saved in the viewstate.
    > >> >
    > >> > What happens when you take the if statement out of the Page.Load
    > >> > handler and just call BindList every time the page loads?
    > >> >
    > >> > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > >> > System.EventArgs) Handles MyBase.Load
    > >> >
    > >> > BindList()
    > >> >
    > >> > End Sub
    > >> >
    > >> >
    > >> >
    > >> > GJH wrote:
    > >> >> I have a Web Custom Control datagrid written in VB.NET and when the
    > >> >> page
    > >> >> does a postback from a Selected Index or Sort command the datagrid
    > >> >> disappears??? anyone know why?
    > >> >> thanks
    > >> >>
    > >> >> here is the control's code:
    > >> >>
    > >> >> Imports System.ComponentModel
    > >> >>
    > >> >> Imports System.Web.UI
    > >> >>
    > >> >> <DefaultProperty("Text"), ToolboxData("<{0}:dgTest
    > >> >> runat=server></{0}:dgTest>")> Public Class dgTest
    > >> >>
    > >> >> Inherits System.Web.UI.WebControls.DataGrid
    > >> >>
    > >> >> Dim _text As String
    > >> >>
    > >> >> <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    > >> >> [Text]()
    > >> >> As String
    > >> >>
    > >> >> Get
    > >> >>
    > >> >> Return _text
    > >> >>
    > >> >> End Get
    > >> >>
    > >> >> Set(ByVal Value As String)
    > >> >>
    > >> >> _text = Value
    > >> >>
    > >> >> End Set
    > >> >>
    > >> >> End Property
    > >> >>
    > >> >> Protected Overrides Sub Render(ByVal output As
    > >> >> System.Web.UI.HtmlTextWriter)
    > >> >>
    > >> >> Me.EnsureChildControls()
    > >> >>
    > >> >> MyBase.Render(output)
    > >> >>
    > >> >> End Sub
    > >> >>
    > >> >> Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    > >> >>
    > >> >> Me.EnsureChildControls()
    > >> >>
    > >> >> End Sub
    > >> >>
    > >> >> End Class
    > >> >>
    > >> >>
    > >> >>
    > >> >>
    > >> >> Here is my webpage
    > >> >>
    > >> >> Dim mySort As String
    > >> >>
    > >> >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > >> >> System.EventArgs) Handles MyBase.Load
    > >> >>
    > >> >> If Not Page.IsPostBack Then
    > >> >>
    > >> >> BindList()
    > >> >>
    > >> >> End If
    > >> >>
    > >> >> End Sub
    > >> >>
    > >> >> Private Sub BindList(Optional ByVal sort As String = "")
    > >> >>
    > >> >> Dim query As String = "select * from employees"
    > >> >>
    > >> >> Dim da As New SqlDataAdapter(query, cnn)
    > >> >>
    > >> >> Dim ds As New DataSet
    > >> >>
    > >> >> Try
    > >> >>
    > >> >> da.Fill(ds, "employees")
    > >> >>
    > >> >> ds.Tables(0).DefaultView.Sort = sort
    > >> >>
    > >> >>
    > >> >>
    > >> >> DgTest1.DataSource = ds
    > >> >>
    > >> >> DgTest1.DataMember = "employees"
    > >> >>
    > >> >> DgTest1.DataBind()
    > >> >>
    > >> >> Catch ex As Exception
    > >> >>
    > >> >> Response.Write(ex.Message)
    > >> >>
    > >> >> End Try
    > >> >>
    > >> >> End Sub
    > >> >>
    > >> >> Private Sub GJHAddToCartDataGrid1_SortCommand(ByVal source As Object,
    > >> >> ByVal
    > >> >> e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
    > >> >> GJHAddToCartDataGrid1.SortCommand
    > >> >>
    > >> >> mySort = e.SortExpression & " ASC"
    > >> >>
    > >> >> BindList(mySort)
    > >> >>
    > >> >> End Sub
    > >> >>
    > >> >> Private Sub DgTest1_SelectedIndexChanged(ByVal sender As
    > >> >> System.Object,
    > >> >> ByVal e As System.EventArgs) Handles DgTest1.SelectedIndexChanged
    > >> >>
    > >> >> Response.Write("hi")
    > >> >>
    > >> >> End Sub
    > >> >

    > >
    Jeff, Dec 4, 2006
    #6
    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. =?Utf-8?B?QWxhbiBMYW1iZXJ0?=

    Values disappearing on PostBack

    =?Utf-8?B?QWxhbiBMYW1iZXJ0?=, Nov 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    585
    =?Utf-8?B?QWxhbiBMYW1iZXJ0?=
    Nov 4, 2004
  2. =?Utf-8?B?dmFuZGls?=
    Replies:
    2
    Views:
    1,186
    Eliyahu Goldin
    Jan 25, 2007
  3. Replies:
    0
    Views:
    208
  4. GJH

    Custom Datagrid disappearing on Postback

    GJH, Dec 4, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    101
  5. Dan
    Replies:
    2
    Views:
    236
Loading...

Share This Page