GridView databinding

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a GridView which is bound to a table. The user has the ability to
change some defaults and then run a process from this.

This all worked fine until I added in an auto postback to one of the
controls to tie in some other functionality.

After the post back, and at the end of page load complete, everything is as
it should be, but then the GridView does an automatic databind (after load
complete) and wipes it all out. Is there anyway to keep this automatic
update from occuring, or cancel it or whatever?

Thanks.
 
You should be able to override the OnDataBinding event for the table. You
can check for Page.IsPostBack there and cancel the data binding if
necessary.
 
Dear Acsla,
Thanks for Brandon's response.

Actually, this is a known issue in Asp.net 2.0 Application.
asp.net 2.0 databound controls will automatically do the databinding when
the DataSourceID is configured... Also, this automatic databinding is
performed during the PreRender event

If you want to get rid of automatic databinding, 1) Brandon's idea is a
good solution.

2) For another choice, I'd like to suggest you use DBDataAdapter/DataSet
rather than DataSource Control. DataSource Control is a new component in
ASP.net 2.0. ASP.net 2.0 will retrieve data from DataSource control
automatically when we assign it to DataGrid.DataSourceID in design mode.

You can fill dataset by DataAdapter and set dataset to DataGrid.DataSource
property.
Call DataGrid.DataBind() method when necessary.
For example:
SqlConnection cn=new SqlConnection();
SqlCommand scd=new SqlCommand()
SqlDataAdatper sda=new SqlDataAdapter(scd,cn);
DataSet ds=new DataSet();
Sda.Fill(ds);
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();

3) Steven provided two solutions (set DataSourceID in runtime or get the
IEnumerable collection from DataSouce Contol.) for such issue.
a. Assign the DataSourceID at runtime (in a certain of your control's
postback event) and call databind method....

b. Call DataSourceControl.Select method to get the IEnumerable
collection (datasource object) and assign it to DataBoundControl.DataSource
property and call databind method.

http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/brow
se_thread/thread/b44e282bb0b0f95f/86036d48e0418053?lnk=st&q=ASP+Net+-+2.0+co
ntrolling+Databinding&rnum=3&hl=zh-CN#86036d48e0418053
[2.0 controlling Databinding ]

Hope this helps. Please feel free to let me know if you have anything
unclear. We are glad to assist you. Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Dear Acsla,

This is Wen Yuan again. I just want to check if the issue has been resolved
so far?
If there is anything unclear, please feel free to update here. We are glad
to assist you.

Have a great day,
Best regards,
Wen Yuan
Microsoft Online Community Support
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top