Click event on an ImageButton column

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
 
B

bruce barker

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.
 
M

Martin Dechev

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.
 
M

Martin Dechev

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top