This control works, but I have a feeling it could be implemented better. I'm still learning all the details of web controls. So any suggestions - please fire away.
- We have a grid where we want a button, not text, for a column in the grid.
- This is a button that will execute a data command (like Delete).
- We want it to be a data bound control to keep things simpler in the aspx file.
- We have 4 bitmaps - one each for normal, OnMouseOver, OnClick, and a bitmap we use if the link is disabled.
The attached code is the web control. One interesting item I found implementing this is that there seems to be a single instance of the ButtonImageField class implemented for the column, reused for each row. So you cannot have per-row variables in this class. Instead you must operate on the ImageButton object it creates and puts in the DataControlFieldCell.Controls[0].
To disable the control, you need to do:
protected void GlobalDatasourceView_RowDataBound(object sender, GridViewRowEventArgs e) {
if (some logic) {
DataControlFieldCell dcfc = e.Row.Cells[1] as DataControlFieldCell;
ButtonLinkField blf = dcfc.ContainingField as ButtonLinkField;
blf.Enable(false, dcfc.Controls[0] as HyperLink, "cannot edit this {0}");
}
}
Webcontrol here: Download ButtonImageField.cs

Comments