Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 588153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:17:19+00:00 2026-05-13T15:17:19+00:00

I have a GridView with dynamically created image buttons that should fire command events

  • 0

I have a GridView with dynamically created image buttons that should fire command events when clicked. The event handling basically works, except for the very first time a button is clicked. Then, the postback is processed, but the event is not fired.

I have tried to debug this, and it seems to me, that the code executed before and after the first click is exactly the same as for any other clicks. (With the exception that in the first click, the event handler is not called.)

There is some peculiarity in that: The buttons which fire the event are created dynamically through databinding, i.e. databinding must be carried out twice in the page lifecycle: Once on load, in order to make the buttons exist (otherwise, events could not be handled at all), and once before rendering in order to display the new data after the events have been processed.

I have read these posts but they wouldn’t match my situation:
ASP.NET LinkButton OnClick Event Is Not Working On Home Page,
LinkButton not firing on production server,
ASP.NET Click() event doesn't fire on second postback

To the details:
The GridView contains image buttons in each row. The images of the buttons are databound. The rows are generated by GridView.DataBind(). To achieve this, I have used the TemplateField with a custom ItemTemplate implementation. The ItemTemplate’s InstantiateIn method creates the ImageButton and assigns it the according event handler. Further, the image’s DataBinding event is assigned a handler that retrieves the appropriate image based on the respective row’s data.

The GridView is placed on a UserControl. The UserControl defines the event handlers for the GridView’s events. The code roughly looks as follows:

private DataTable dataTable = new DataTable();
protected SPGridView grid;

protected override void OnLoad(EventArgs e)
{
    DoDataBind(); // Creates the grid. This is essential in order for postback events to work.
}

protected override void Render(HtmlTextWriter writer)
{
    DoDataBind();
    base.Render(writer); // Renews the grid according to the latest changes
}

void ReadButton_Command(object sender, CommandEventArgs e)
{
    ImageButton button = (ImageButton)sender;
    GridViewRow viewRow = (GridViewRow)button.NamingContainer;
    int rowIndex = viewRow.RowIndex;

    // rowIndex is used to identify the row in which the button was clicked,
    // since the control.ID is equal for all rows.
    // [... some code to process the event ...]
}

private void DoDataBind()
{
    // [... Some code to fill the dataTable ...]

    grid.AutoGenerateColumns = false;

    grid.Columns.Clear();

    TemplateField templateField = new TemplateField();
    templateField.HeaderText = "";
    templateField.ItemTemplate = new MyItemTemplate(new CommandEventHandler(ReadButton_Command));
    grid.Columns.Add(templateField);

    grid.DataSource = this.dataTable.DefaultView;
    grid.DataBind();
}

private class MyItemTemplate : ITemplate
{
    private CommandEventHandler commandEventHandler;

    public MyItemTemplate(CommandEventHandler commandEventHandler)
    {
        this.commandEventHandler = commandEventHandler;
    }

    public void InstantiateIn(Control container)
    {
        ImageButton imageButton = new ImageButton();
        imageButton.ID = "btnRead";
        imageButton.Command += commandEventHandler;
        imageButton.DataBinding += new EventHandler(imageButton_DataBinding);
        container.Controls.Add(imageButton);
    }

    void imageButton_DataBinding(object sender, EventArgs e)
    {
        // Code to get image URL
    }
}

Just to repeat: At each lifecycle, first the OnLoad is executed, which generates the Grid with the ImageButtons. Then, the events are processed. Since the buttons are there, the events usually work. Afterwards, Render is called, which generates the Grid from scratch based upon the new data. This always works, except for the very first time the user clicks on an image button, although I have asserted that the grid and image buttons are also generated when the page is sent to the user for the first time.

Hope that someone can help me understand this or tell me a better solution for my situation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:17:19+00:00Added an answer on May 13, 2026 at 3:17 pm

    Since in my opinion this is a partial answer, I re-post it this way:

    • If I use normal Buttons instead of ImageButtons (in the exact same place, i.e. still using MyItemTemplate but instantiating Button instead of ImageButton in “InstantiateIn”, it works fine.

    • If I assert that DoDataBind() is always executed twice before sending the content to the client, it works fine with ImageButtons.

    Still puzzled, but whatever…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 278k
  • Answers 278k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Do yourself a favor, and use a JS library that… May 13, 2026 at 3:17 pm
  • Editorial Team
    Editorial Team added an answer WP-eCommerce is the only plugin for WordPress worth looking at… May 13, 2026 at 3:17 pm
  • Editorial Team
    Editorial Team added an answer Call me a man of brevity... I would prefer seeing… May 13, 2026 at 3:17 pm

Related Questions

I have a web page with a GridView. The GridView contains a list of
I'm sure that almost everyone programming in .net has ran into similar issues with
I have an ASP.NET page that has dynamically created LinkButton elements. Please note that
In my .NET application I need to add a checkbox to each row in
I have a Gridview that I create a dynamic footer wired up to the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.