开发者

ASP.NET GridView binding sequence

开发者 https://www.devze.com 2023-03-25 12:25 出处:网络
I\'m trying to bind my GridView at runtime, but I\'m also trying to avoid running all the binding events twice.

I'm trying to bind my GridView at runtime, but I'm also trying to avoid running all the binding events twice.

I have a GridView that gets populated from a function that returns a DataTable. I'm not using ViewState in the grid for a couple of reasons. I seem to have a Catch-22 situation here:

  • If I don't bind the grid by Page_Load at the latest, the RowCommand and other grid events won't fire.
  • If I DO bind the grid in Page_Load, but I'm on a PostBack from a pager link, sort link, or search button, those event handlers will change the data and need to rebind it, running all the binding code again.

The grid triggers DataBound, RowDataBound, and RowCreated events, which could be performing expensive operations. I really hate to call them all in Page_Load, and then wipe out the data and call them all again if the d开发者_开发技巧ata changes. But I can't seem to avoid this double duty, because in Page_Load I don't know if it was a grid event that will change the data, or a grid event that doesn't.

Any ideas?


Try the command arguments. If a button in the gridview was clicked, that event will be fired and you can handle it appropriately. Your question is not clear enough i'm afraid. Could you be more specific?


Check if request is a postback. Bind the datatable to the grid like so:

If(!ispostback)... That way you wont be binding the table to the grid on each request.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号