Datagrid, databind, and ItemCommand

Results 1 to 2 of 2

Thread: Datagrid, databind, and ItemCommand

  1. #1
    Join Date
    Dec 1969

    Default Datagrid, databind, and ItemCommand

    Seasoned in ASP, yet new to ASP.NET so this question is more general.<BR><BR>I have a page with a datagrid, with a couple button columns. In the page_load event, I populate a dataview and set the datagrid source to that dataview. Works like a champ. Now if I declare an OnItemCommand function for the button columns, the function only fires correctly if on the page_load, I do a if !Postback then binddata() and in the event function also do a binddata() for the datagrid.<BR><BR>I am curious as to why I must check the PostBack value in the page_load to get the buttons to work. Due to the nature of the data, it is going to queried and bound on every page request anyway.<BR><BR>I am assuming I am just missing some concept in when events fire.<BR>O&#039;Cary

  2. #2
    Join Date
    Dec 1969

    Default RE: Datagrid, databind, and ItemCommand

    The reason you need to wrap the DataBind in an If !IsPostBack block is that the Page_Load event fires every time the page is requested from the server. Even if you are doing a postback.<BR><BR>If you don&#039;t put your DataBind() in the If !IsPostBack block it will execute twice on a button column click, once in the ItemCommand event handler and subsequently in the Page_Load event handler.<BR><BR>If you let the Page_Load event handle the grid too, you lose all of the changes stored in the viewstate you just posted back and your grid remains unchanged by the event.<BR><BR>Is this too confusing?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts