Refresh data in ObjectDataSource in button event handler

I have a user control with GridView and  ObjectDataSource. I also have a button, that should update database and I wanted to refresh GridView in the same click callback.

And it's easy to do- call ObjectDataSource.Select and then GridView.DataBind:

        Protected Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

 â€˜method to update database is omitted for simplicity

            Me.ObjectDataSource1.Select()

            Me.GridView1.DataBind()

        End Sub

Update: As I was adviced by Bob Riley, call to GridView1.DataBind()is enough.  

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

ImportRow to DataTable with reference to a new row

The MS .Net framework DataTable.ImportRow doesn't return reference to a new row.

Also it is not documented how ImportRow will behave if record with primary keys already exist.  

So I've created a static "overload" of DataTable.LoadDataRow  method.

 

 

The common mistake that I had was that DataTable didn't have schema filled, and primary keys were empty. It caused that even existing records were considered as new, and duplicates were imported. So I've added a CheckPrimaryKey parameted, which is recommended set to true unless you expected tables without primary keys.

     

        /// <summary>

        /// Static "overload" of <see cref="DataTable.LoadDataRow"/> method.

        ///Finds and updates a specific row. If no matching row is found, a new row is created using the given values.

        /// </summary>

        /// <param name="tbl"></param>

        /// <param name="row"></param>

        /// <returns></returns>

        /// <remarks >The MS .Net framework DataTable.ImportRow doesn't return reference to a new row.

        ///Also it is not documented how ImportRow will behave if record with primarykeys already exist.</remarks>

        public static DataRow LoadDataRow(DataTable tbl,    DataRow row, bool CheckPrimaryKey)

        {

            if (CheckPrimaryKey == true)

            {

                if ((tbl.PrimaryKey == null) || (tbl.PrimaryKey.Length == 0))

                {

                    Debug.Assert(false);

                    return null;

                }

            }

           DataRow newRow = tbl.LoadDataRow(row.ItemArray,false);

            return newRow;

        }

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Event sequence for ASP.NET user control with ObjectDataSource

I've started to work with ObjectDataSource and had a question about event sequence for it. I didn't find specific documentation and added event handlers with DebugHelper.TracedLine calls (you can use just simple Debug.WriteLiine).

The result was quite logical:
Page_Load (IsPostBack =False)
ObjectDataSource1_Load:
ObjectDataSource1_ObjectCreating:
ObjectDataSource1_ObjectCreated:
ObjectDataSource1_Selecting:
ObjectDataSource1_Selected
:
GridView1_DataBound

When user clicked on Select button
Page_Load (IsPostBack =False)
ObjectDataSource1_Load:
GridView1_SelectedIndexChanged
As documented in MSDN DatItem article, GridView1.SelectedRow.DataItem and GridView1.DataSource are Null at that time.

Also (not directly related)   event sequence for DataGrid.BindData

DataBinding:

ItemCreated:Header

For each row:

ItemCreated:

ItemDataBound:

ItemCreated:Footer

 

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
«August»
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789