Datagrid / Postback question (ASP.NET)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig S

    #1

    Datagrid / Postback question (ASP.NET)

    I believe the problem is definately a lack of understanding on my part about
    how postbacks work, but here goes...

    In my Page_Load method I bind my data from a SQL database to a datagrid -
    works great... I've now created a button and in the OnClick method I do a
    "DELETE FROM tablename WHERE keyfield=" +variablename (deletes several
    records based on which checkboxes are checked in the datagrid).

    The problem is that when the page posts back, the DELETE happens in the
    database, but it obviously happens after the SELECT statement that populates
    the datagrid. I check the DB contents and the records were indeed deleted,
    but the datagrid is always one postback behind (If I post the page back
    AGAIN, it will get the update)

    What code can I add to make sure the datagrid gets bound AFTER the delete
    command happens?

    Thanks in advance!
    Craig


  • Arran Pearce

    #2
    Re: Datagrid / Postback question (ASP.NET)

    Try....

    create a method which binds the data to the grid.

    in page load add
    if(!Page.IsPost Back)
    {
    <run databinding method>
    }

    then in your event run the code which deletes the data and then run the
    databind method again here.

    Dont know if this would be the correct way to do it, but it should work
    fine.



    "Craig S" <craig@removeth is_birch.net> wrote in message
    news:e1FdlwdXDH A.1204@TK2MSFTN GP12.phx.gbl...[color=blue]
    > I believe the problem is definately a lack of understanding on my part[/color]
    about[color=blue]
    > how postbacks work, but here goes...
    >
    > In my Page_Load method I bind my data from a SQL database to a datagrid -
    > works great... I've now created a button and in the OnClick method I do a
    > "DELETE FROM tablename WHERE keyfield=" +variablename (deletes several
    > records based on which checkboxes are checked in the datagrid).
    >
    > The problem is that when the page posts back, the DELETE happens in the
    > database, but it obviously happens after the SELECT statement that[/color]
    populates[color=blue]
    > the datagrid. I check the DB contents and the records were indeed[/color]
    deleted,[color=blue]
    > but the datagrid is always one postback behind (If I post the page back
    > AGAIN, it will get the update)
    >
    > What code can I add to make sure the datagrid gets bound AFTER the delete
    > command happens?
    >
    > Thanks in advance!
    > Craig
    >
    >[/color]


    Comment

    • Fred

      #3
      Re: Datagrid / Postback question (ASP.NET)

      the problem is that the page_load event is fired before your Delete
      event


      the workaround is to put the code to populate your page overriding the
      pre-render event which happens after the page events are handled


      So if you have a DataList dl and DataTable dt , move your code from
      the page_load event to the following event


      protected override void OnPreRender(Eve ntArgs e)
      {
      --- add code to populate the datatable
      dl.DataSource = dt.DefaultView;
      dl.DataBind();

      }
      }


      hope this helps
      Fred

      Comment

      Working...