Click event on an ImageButton column

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

Guest

Hi

I have a DataGrid with an ImageButton column. When I click on an imagebutton I get a postback but it doesn't run the OnImgBtnClick method. I can actually comment out the line where I add this Click event to the ImageButton Column and it makes no difference, I still get a postback. This is driving me crazy...something seems to be causing an OnClick postback and it isn't my Click event
I've included the code below....any help will be much appreciated as I've been looking at this for a long time and I'm havnig a real problem understanding what's going on

Thanks in advance, John

private void OnImgBtnClick(object sender, EventArgs e

label1.Text="Button Clicked"


public void BindDataGrid(string querytype
 
an image button will always postback (its a feature of the browser). to
catch the postback you have to setup the onclick handler correcly.

-- bruce (sqlwork.com)

J McD said:
Hi,

I have a DataGrid with an ImageButton column. When I click on an
imagebutton I get a postback but it doesn't run the OnImgBtnClick method. I
can actually comment out the line where I add this Click event to the
ImageButton Column and it makes no difference, I still get a postback. This
is driving me crazy...something seems to be causing an OnClick postback and
it isn't my Click event.
I've included the code below....any help will be much appreciated as I've
been looking at this for a long time and I'm havnig a real problem
understanding what's going on.
 
Hi, John,

First thing that I'm missing is the class ImageButtonColumn. I can only
guess what this class implements and from which clas it is inherited.

If this class is inherited from the ButtonColumn class, note that in a
DataGrid when you have a ButtonColumn you handle the ItemCommand event.

Greetings
Martin
J McD said:
Hi,

I have a DataGrid with an ImageButton column. When I click on an
imagebutton I get a postback but it doesn't run the OnImgBtnClick method. I
can actually comment out the line where I add this Click event to the
ImageButton Column and it makes no difference, I still get a postback. This
is driving me crazy...something seems to be causing an OnClick postback and
it isn't my Click event.
I've included the code below....any help will be much appreciated as I've
been looking at this for a long time and I'm havnig a real problem
understanding what's going on.
 
I should have read your entire post. Sorry.

It seems like the proxying of the event is broken. In the BindDataGrid
method you attach OnImgBtnClick to the event Click of the ImageButtonColumn,
but nothing is attached to the Click event of the ImageButtonItem class. Try
adding to the constructor of the ImageButtonColumn class the following:

imgItem.Click += new ImageClickEventHandler(OnImgBtnClick);

And add a method to handle it:

void OnImgBtnClick(object s, ImageClickEventArgs e)
{
if(Click != null)
Click(s, e);
}

Also, the Click event of the ImageButtonColumn is ImageClickEventHandler, so
you should change both the signature of the handler in the page class as
well as the statement in which that handler is attached:

ibnCol.Click += new ImageClickEventHandler(this.OnImgBtnClick);

protected void OnImgBtnClick(object s, ImageClickEventArgs e)
{
}

Anyway, it is easier to use ButtonColumn because it is easier to find out
which button/hyperlink was clicked in a single handler.

Hope this helps
Martin
 
Thanks a lot for your advice. You've shown me what I should be looking at

Is it easy to Create an ImageButtonClass that inherits from ButtonColumn
Something that behaves just like the ButtonColumn class but has an image to click on

Thanks again, John.
 
Back
Top