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;
}
}
}