ItemCommand handler and DataBind on Postback

Discussion in 'ASP .Net Datagrid Control' started by Joel Finkel, Aug 16, 2004.

  1. Joel Finkel

    Joel Finkel Guest


    I have a web application that displays rows from a database and, on each row, displays three ImageButtons, each of which needs to execute a different function. In order to improve performance, I have a Panel with a 26 buttons (labeled 'A' thru 'Z'), which is used to limit the rows to only those where the Name field begins with a specific letter (rather than returning the entire database each time).

    The first time through, all those with the Name field beginning with 'A' are returned. When the user presses a different button, let's say 'F,' the application adjusts a parameter to the Stored Procedure, the sqlDataAdapter is filled, and, in order to display the data, the DataGrid does a DataBind.

    So far so good. I can display only the subset of rows that I want. Neat. By the way, each of these 26 buttons runs client-side code that places the correct letter into an invisible text field and then submits the form, resulting in a postback.

    Now for the problem with the ImageButtons: I placed these in a TemplateColumn and gave each of them a specific CommandName so that the event handler can figure out which function to call.

    I created the function (ItemsGrid_Command), which swithes on CommandName to call the proper function. In the Page_Load function, I bind this handler to the DataGrid's ItemCommand delegate like this:

    DataGrid1.ItemCommand += new DataGridCommandEventHandler(this.ItemsGrid_Command);

    The problem is that this function never gets called; the page simply redisplays with the same data when I click on any of the three ImageButtons.

    Having read that calling DataBind blows away the ItemCommand event handler, I confirmed that the event handler is called correctly if I do not do a DataBind (that is, if I test for IsPostBack). But I MUST perform the DataBind even if IsPostBack in order to display the new data subset. But then the ImageButtons do not function.

    Interestingly, doing a DataBind does not destroy the ItemBound event handler. I attached code to this event to set the color of the rows depending on values in the row. So I know that it works. But it seems that the ItemCommand event is not re-created even though I always set it (see code, above).

    I wonder if anyone has some suggestions for me as to how to achieve both of the required features: 1) limit the rows by first letter and 2) have three ImageButtons on each row that respond to user clicks.

    Joel Finkel
    Joel Finkel, Aug 16, 2004
    1. Advertisements

  2. Joel Finkel

    anon Guest

    Hi Joel,
    I hope I understood you problem correctly, but since u have not got any
    replies yet, I am going to attempt to provide a useful solution..

    Now, the initial Bind shud only be done if it is not a
    bind again after the itemcommand has performed some operations on the
    page, u shud call bind again in the end of might help
    to have a Bind() method, so that you can call it from !PostBack and also
    at the end of ur page wud have a skeleton like this...



    SWITCH(for the 3 imagebuttons)

    Hope that helps,
    anon, Aug 18, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.