473,379 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Click event on an ImageButton column

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
5 6150
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
So can you tell me what is incorrect about my Click event handler here?
Nov 18 '05 #3
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JMcD | last post by:
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...
0
by: Mark | last post by:
Hi, I am trying to capture the click event of an ImageButton that exists in a composite server control that I am building. I create the imagebutton in the CreateChildControls routine '...
7
by: charliewest | last post by:
Using .Net Compact Framework, I have an ImageButton control (derived from the Button control) this is wired to a click event. The click event's receives the standard arguments (sender, e). How is...
3
by: Benjamin Gavin | last post by:
Hi all, I recently stumbled upon a bug in the ASP.NET framework handling of ImageButton based postbacks. The issues derives from the fact that IE and Mozilla handle the case of a missing image...
3
by: TCORDON | last post by:
Why does the click event of an image button that submitts a form does not get fired when ENTER is pressend on a textbox of that form? How can I make this happen? Thanks
2
by: Mark | last post by:
I am assigning a dynamic imagebutton on a page and then assiging it a Click event with the following code, to pass the string "test" to the DeleteDetail method testImgBtn.ID = "imgBtn" +...
2
by: Steve | last post by:
Hi, I have a dynamically created ImageButton in a custom control, and I can't get the control to handle the ImageButton Click event. I see the control in the Page.Request.Form.AllKeys collection,...
1
by: Brett Wesoloski | last post by:
I am having problems getting the index of an image click event in a datagrid. Every time I look at the index it is -1. What I am trying to do is get the index of the row in which the imagebutton...
0
by: mihai | last post by:
Hi, I am trying to create a list of record every with an ImageButton attached. The code is like this: ImageButton img = new ImageButton(); img.ImageUrl = "imgs/ok.bmp"; img.Visible = true;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.