473,748 Members | 11,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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...somethi ng 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(o bject sender, EventArgs e

label1.Text="Bu tton Clicked"
public void BindDataGrid(st ring querytype

Nov 18 '05 #1
5 6173
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*******@disc ussions.microso ft.com> wrote in message
news:D3******** *************** ***********@mic rosoft.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...somethi ng 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(o bject sender, EventArgs e)
{
label1.Text="Bu tton Clicked";
}

public void BindDataGrid(st ring querytype)
{
.
.
ImageButtonColu mn ibnCol = new ImageButtonColu mn();
ibnCol.HeaderTe xt = "Role";

ibnCol.Click += new EventHandler(th is.OnImgBtnClic k);
ResultsDataGrid .Columns.Add(ib nCol);
}

.
.
.
public class ImageButtonColu mn : TemplateColumn
{
private ImageButtonItem imgItem;
public event ImageClickEvent Handler 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.AutoPos tBack = value;
}
get
{
return imgItem.AutoPos tBack;
}
}

public ImageButtonColu mn()
{
// set the view one as readonly
imgItem = new ImageButtonItem (false);
this.ItemTempla te = 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 ImageClickEvent Handler Click;

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

ib.Click += new ImageClickEvent Handler(this.On Click);
container.Contr ols.Add(ib);
}
private void OnClick(object sender, ImageClickEvent Args e)
{
//if (Click != null)
//{
Click(sender, e);
//}
}
/// <summary>
/// The internal storage for the AutoPostback flag.
/// </summary>
private bool autoPostBack=tr ue;

/// <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 ImageButtonColu mn. 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*******@disc ussions.microso ft.com> wrote in message
news:D3******** *************** ***********@mic rosoft.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...somethi ng 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(o bject sender, EventArgs e)
{
label1.Text="Bu tton Clicked";
}

public void BindDataGrid(st ring querytype)
{
.
.
ImageButtonColu mn ibnCol = new ImageButtonColu mn();
ibnCol.HeaderTe xt = "Role";

ibnCol.Click += new EventHandler(th is.OnImgBtnClic k);
ResultsDataGrid .Columns.Add(ib nCol);
}

.
.
.
public class ImageButtonColu mn : TemplateColumn
{
private ImageButtonItem imgItem;
public event ImageClickEvent Handler 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.AutoPos tBack = value;
}
get
{
return imgItem.AutoPos tBack;
}
}

public ImageButtonColu mn()
{
// set the view one as readonly
imgItem = new ImageButtonItem (false);
this.ItemTempla te = 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 ImageClickEvent Handler Click;

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

ib.Click += new ImageClickEvent Handler(this.On Click);
container.Contr ols.Add(ib);
}
private void OnClick(object sender, ImageClickEvent Args e)
{
//if (Click != null)
//{
Click(sender, e);
//}
}
/// <summary>
/// The internal storage for the AutoPostback flag.
/// </summary>
private bool autoPostBack=tr ue;

/// <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 ImageButtonColu mn,
but nothing is attached to the Click event of the ImageButtonItem class. Try
adding to the constructor of the ImageButtonColu mn class the following:

imgItem.Click += new ImageClickEvent Handler(OnImgBt nClick);

And add a method to handle it:

void OnImgBtnClick(o bject s, ImageClickEvent Args e)
{
if(Click != null)
Click(s, e);
}

Also, the Click event of the ImageButtonColu mn is ImageClickEvent Handler, 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 ImageClickEvent Handler(this.On ImgBtnClick);

protected void OnImgBtnClick(o bject s, ImageClickEvent Args 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*******@hotm ail.com> wrote in message
news:OP******** ******@TK2MSFTN GP10.phx.gbl...
Hi, John,

First thing that I'm missing is the class ImageButtonColu mn. 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*******@disc ussions.microso ft.com> wrote in message
news:D3******** *************** ***********@mic rosoft.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...somethi ng 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(o bject sender, EventArgs e)
{
label1.Text="Bu tton Clicked";
}

public void BindDataGrid(st ring querytype)
{
.
.
ImageButtonColu mn ibnCol = new ImageButtonColu mn();
ibnCol.HeaderTe xt = "Role";

ibnCol.Click += new EventHandler(th is.OnImgBtnClic k);
ResultsDataGrid .Columns.Add(ib nCol);
}

.
.
.
public class ImageButtonColu mn : TemplateColumn
{
private ImageButtonItem imgItem;
public event ImageClickEvent Handler 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.AutoPos tBack = value;
}
get
{
return imgItem.AutoPos tBack;
}
}

public ImageButtonColu mn()
{
// set the view one as readonly
imgItem = new ImageButtonItem (false);
this.ItemTempla te = 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 ImageClickEvent Handler Click;

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

ib.Click += new ImageClickEvent Handler(this.On Click);
container.Contr ols.Add(ib);
}
private void OnClick(object sender, ImageClickEvent Args e)
{
//if (Click != null)
//{
Click(sender, e);
//}
}
/// <summary>
/// The internal storage for the AutoPostback flag.
/// </summary>
private bool autoPostBack=tr ue;

/// <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 ImageButtonClas s 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
1323
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 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...
0
1819
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 ' Create the button Dim imgButton as New ImageButton ' Add click event handler AddHandler imgButton.Click, AddressOf OnSend
7
14471
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 it possible from within the click event function to get the "name" property of the control? For example: ImageButton ImageButton1 = new ImageButton(); ImageButton1.Click += new System.EventHandler(ImageButton1_Click); .... code private void...
3
2814
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 file differently. In both cases, the setup for the problem is: 1. Create a form with an ImageButton whose ImageURL is invalid, specify an AlternateText value 2. Submit the form through that ImageButton
3
7075
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
4297
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" + loopnumber; testImgBtn.ImageUrl = "imgs/delete_16.gif"; testImgBtn.AlternateText = "Delete this detail"; testImgBtn.Style = "#B4B4B4"; testImgBtn.Click += new ImageClickEventHandler(DeleteDetail("test"));
2
2239
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, but it's represented as "imgbtn.X" (and .Y), and I'm guessing that .NET can't connect the name to the event since the ID isn't an exact match. I read an article stating that I need to use IPostBackDataHandler methods, but I didn't have a lot of...
1
2497
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 was clicked. So that I can then go and look into a hidden field as to what the value will be that I need to use. I am using a template column to create the imagebutton. <asp:TemplateColumn HeaderText="">
0
937
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
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.