By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

Click event on an ImageButton column

P: n/a
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

Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
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)
{
.
.
ImageButtonColumn ibnCol = new ImageButtonColumn();
ibnCol.HeaderText = "Role";

ibnCol.Click += new EventHandler(this.OnImgBtnClick);
ResultsDataGrid.Columns.Add(ibnCol);
}

.
.
.
public class ImageButtonColumn : TemplateColumn
{
private ImageButtonItem imgItem;
public event ImageClickEventHandler Click
{
add
{
imgItem.Click += value;
}
remove
{
imgItem.Click -= value;
}
}

/// <summary>
/// If true then then each click on a CheckBox will cause an event to be fired on the server. /// </summary>
public bool AutoPostBack
{
set
{
imgItem.AutoPostBack = value;
}
get
{
return imgItem.AutoPostBack;
}
}

public ImageButtonColumn()
{
// set the view one as readonly
imgItem = new ImageButtonItem(false);
this.ItemTemplate = imgItem as ITemplate;

}
}
internal class ImageButtonItem : ITemplate
{
private bool readOnly = true;

/// <summary>
/// The internal storage for which DataField we are going to represent.
/// </summary>
private string dataField;

public event ImageClickEventHandler Click;

public ImageButtonItem(bool editable)
{
readOnly = (editable==true)?false:true;
}
void ITemplate.InstantiateIn(Control container)
{
ImageButton ib = new ImageButton();
ib.ImageUrl = "../images/greybutton.gif";

ib.Click += new ImageClickEventHandler(this.OnClick);
container.Controls.Add(ib);
}
private void OnClick(object sender, ImageClickEventArgs e)
{
//if (Click != null)
//{
Click(sender, e);
//}
}
/// <summary>
/// The internal storage for the AutoPostback flag.
/// </summary>
private bool autoPostBack=true;

/// <summary>
/// Set the AutoPostBack flag. If this is true then each time a ImageButton is clicked /// in the Column that contains this item then an event is raised on the server. /// </summary>
public bool AutoPostBack
{
set
{
autoPostBack = value;
}
get
{
return autoPostBack;
}
}
}

Nov 18 '05 #2

P: n/a
So can you tell me what is incorrect about my Click event handler here?
Nov 18 '05 #3

P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
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)
{
.
.
ImageButtonColumn ibnCol = new ImageButtonColumn();
ibnCol.HeaderText = "Role";

ibnCol.Click += new EventHandler(this.OnImgBtnClick);
ResultsDataGrid.Columns.Add(ibnCol);
}

.
.
.
public class ImageButtonColumn : TemplateColumn
{
private ImageButtonItem imgItem;
public event ImageClickEventHandler Click
{
add
{
imgItem.Click += value;
}
remove
{
imgItem.Click -= value;
}
}

/// <summary>
/// If true then then each click on a CheckBox will cause an event to be fired on the server. /// </summary>
public bool AutoPostBack
{
set
{
imgItem.AutoPostBack = value;
}
get
{
return imgItem.AutoPostBack;
}
}

public ImageButtonColumn()
{
// set the view one as readonly
imgItem = new ImageButtonItem(false);
this.ItemTemplate = imgItem as ITemplate;

}
}
internal class ImageButtonItem : ITemplate
{
private bool readOnly = true;

/// <summary>
/// The internal storage for which DataField we are going to represent.
/// </summary>
private string dataField;

public event ImageClickEventHandler Click;

public ImageButtonItem(bool editable)
{
readOnly = (editable==true)?false:true;
}
void ITemplate.InstantiateIn(Control container)
{
ImageButton ib = new ImageButton();
ib.ImageUrl = "../images/greybutton.gif";

ib.Click += new ImageClickEventHandler(this.OnClick);
container.Controls.Add(ib);
}
private void OnClick(object sender, ImageClickEventArgs e)
{
//if (Click != null)
//{
Click(sender, e);
//}
}
/// <summary>
/// The internal storage for the AutoPostback flag.
/// </summary>
private bool autoPostBack=true;

/// <summary>
/// Set the AutoPostBack flag. If this is true then each time a ImageButton is clicked /// in the Column that contains this item then an event is raised on the server. /// </summary>
public bool AutoPostBack
{
set
{
autoPostBack = value;
}
get
{
return autoPostBack;
}
}
}

Nov 18 '05 #4

P: n/a
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
"Martin Dechev" <de*******@hotmail.com> wrote in message
news:OP**************@TK2MSFTNGP10.phx.gbl...
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" <an*******@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
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)
{
.
.
ImageButtonColumn ibnCol = new ImageButtonColumn();
ibnCol.HeaderText = "Role";

ibnCol.Click += new EventHandler(this.OnImgBtnClick);
ResultsDataGrid.Columns.Add(ibnCol);
}

.
.
.
public class ImageButtonColumn : TemplateColumn
{
private ImageButtonItem imgItem;
public event ImageClickEventHandler Click
{
add
{
imgItem.Click += value;
}
remove
{
imgItem.Click -= value;
}
}

/// <summary>
/// If true then then each click on a CheckBox will cause an event to be

fired on the server.
/// </summary>
public bool AutoPostBack
{
set
{
imgItem.AutoPostBack = value;
}
get
{
return imgItem.AutoPostBack;
}
}

public ImageButtonColumn()
{
// set the view one as readonly
imgItem = new ImageButtonItem(false);
this.ItemTemplate = imgItem as ITemplate;

}
}
internal class ImageButtonItem : ITemplate
{
private bool readOnly = true;

/// <summary>
/// The internal storage for which DataField we are going to represent.
/// </summary>
private string dataField;

public event ImageClickEventHandler Click;

public ImageButtonItem(bool editable)
{
readOnly = (editable==true)?false:true;
}
void ITemplate.InstantiateIn(Control container)
{
ImageButton ib = new ImageButton();
ib.ImageUrl = "../images/greybutton.gif";

ib.Click += new ImageClickEventHandler(this.OnClick);
container.Controls.Add(ib);
}
private void OnClick(object sender, ImageClickEventArgs e)
{
//if (Click != null)
//{
Click(sender, e);
//}
}
/// <summary>
/// The internal storage for the AutoPostback flag.
/// </summary>
private bool autoPostBack=true;

/// <summary>
/// Set the AutoPostBack flag. If this is true then each time a

ImageButton is clicked
/// in the Column that contains this item then an event is raised on the

server.
/// </summary>
public bool AutoPostBack
{
set
{
autoPostBack = value;
}
get
{
return autoPostBack;
}
}
}


Nov 18 '05 #5

P: n/a
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.
Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.