Datagrid postback problem?!?

Discussion in 'ASP .Net Datagrid Control' started by Bryan Fickle, Mar 16, 2005.

  1. Bryan Fickle

    Bryan Fickle Guest

    I have a datagrid that works perfectly well. I have default paging turned on and this is where I have a small problem.

    My datagrid is created via the On_Click even of my search button. When a user selects page 2 of the grid, for example, the grid disappears. If you press the search button again, page 2 of the grid will appear.....same for any page....click on page 3, grid disappears, click search, page 3 of grid appears. The data is always correct, but I just can't solve this issue.

    Any help would be appreciated.
     
    Bryan Fickle, Mar 16, 2005
    #1
    1. Advertisements

  2. Bryan Fickle

    Elton Wang Guest

    Hi BRYAN

    Could you show your code especially code in
    PageIndexChanged event?

    Elton Wang


    default paging turned on and this is where I have a small
    problem.
    button. When a user selects page 2 of the grid, for
    example, the grid disappears. If you press the search
    button again, page 2 of the grid will appear.....same for
    any page....click on page 3, grid disappears, click
    search, page 3 of grid appears. The data is always
    correct, but I just can't solve this issue.
     
    Elton Wang, Mar 16, 2005
    #2
    1. Advertisements

  3. Bryan Fickle

    Guest Guest

    bind the grid again in the PageIndexChanged event.
    default paging turned on and this is where I have a small
    problem.
    button. When a user selects page 2 of the grid, for
    example, the grid disappears. If you press the search
    button again, page 2 of the grid will appear.....same for
    any page....click on page 3, grid disappears, click
    search, page 3 of grid appears. The data is always
    correct, but I just can't solve this issue.
     
    Guest, Mar 16, 2005
    #3
  4. Bryan Fickle

    Bryan Fickle Guest

    Here is my code. I took out the section in the seach sub where I build the
    SQL string for brevity reasons.



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

    If Not Page.IsPostBack Then

    BindData()

    End If

    End Sub



    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnSearch.Click

    Dim temp As String

    'Create SQL Query string



    Dim strSQL As String = "SELECT * FROM uvwHeader" & temp

    Dim mySQL As New
    SqlConnection("server=ntsrvgvs37;database=AgreementProfile;uid=agreementprofileweb;pwd=roysmess")

    mySQL.Open()

    Dim myCommand As New SqlCommand(strSQL, mySQL)

    Dim myAdapter As New SqlDataAdapter(myCommand)

    Dim myDataSet As New DataSet

    myAdapter.Fill(myDataSet)

    'Bind DataGrid with search results

    SearchGrid.DataSource = myDataSet

    SearchGrid.DataBind()

    'Close connection

    mySQL.Close()

    End Sub



    Sub SearchGrid_PageChanger(ByVal Source As Object, _

    ByVal E As DataGridPageChangedEventArgs)

    'Set the CurrentPageIndex

    SearchGrid.CurrentPageIndex = E.NewPageIndex

    SearchGrid.DataBind()

    End Sub
     
    Bryan Fickle, Mar 16, 2005
    #4
  5. Bryan Fickle

    Elton Wang Guest

    You should rebind data source in SearchGrid_PageChanger
    like

    SearchGrid.CurrentPageIndex = E.NewPageIndex
    SearchGrid.DataSource =
    DataSet_You_Got_in_btnSearch_Click
    SearchGrid.DataBind()

    HTH

    Elton Wang
     
    Elton Wang, Mar 16, 2005
    #5
  6. Bryan Fickle

    Bryan Fickle Guest

    Hi Elton,

    So you are saying it should look like this?

    SearchGrid.DataSource =SearchGrid.DataBind()

    It does not like that. I'm guessing it should look like this then...

    SearchGrid.DataSource = myDataSet
    SearchGrid.DataBind()

    but my page change sub can't see "myDataSet".
     
    Bryan Fickle, Mar 17, 2005
    #6
  7. Bryan Fickle

    Elton Wang Guest

    What I said is

    SearchGrid.CurrentPageIndex = E.NewPageIndex
    SearchGrid.DataSource = myDataSet
    SearchGrid.DataBind()
     
    Elton Wang, Mar 17, 2005
    #7
  8. Bryan Fickle

    Bryan Fickle Guest

    I understand that much, but I guess my question is, Sub
    SearchGrid_PageChanger can't see myDataSet.

    It says it is undefined if I type it in exaclty how you explained. Is there
    a way to pass the myDataSet to the SearchGrid_PageChanger Sub?
     
    Bryan Fickle, Mar 17, 2005
    #8
  9. Bryan Fickle

    Elton Wang Guest

    There are two ways to do it.

    1. use Session to save the datasource.
    you can take look from following url
    http://www.asp.net/Forums/ShowPost.aspx?
    tabindex=1&PostID=857462

    2. each time re-quire the database to get the datasource
    myDataSet.

    HTH

    Elton Wang
     
    Elton Wang, Mar 17, 2005
    #9
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.