could anyone see this please:
I've been building a server control that consist of a grid hierarchy
cBCDataGrid - cBCGridHighlighted - cBCGridHighlightedOnCheck
First one has standard colors, fonts,..
Second one renders java script code to highlight rows on MouseOver
Third one must render a column with a checkbox, for each row to be selected or unselected
The problem is:
Just the last row in the grid is getting such checkbox, no other previous rows get it.
The first class makes public a PreRender method in order to be able to pages using this server control to populate the grid each time with different data sources.. I'll expose main parts of the code:
**********************************************************************
cBCDataGridHighlightedOnCheck.cs
**********************************************************************
namespace WebUI.BaseControls
{
public class cBCDataGridHighlightedOnCheck: cBCDataGridHighlighted
{
protected override string generateJavaScriptCode()
{
String scriptCode = base.generateJavaScriptCode() + ....
return scriptCode;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
TemplateColumn column = new TemplateColumn();
CheckBox headerCheckBox = new CheckBox();
headerCheckBox.ID = "chkAll";
headerCheckBox.AutoPostBack = false;
headerCheckBox.Attributes.Add("OnClick","javascript:SelectAllCheckboxes(this);");
column.HeaderTemplate = new DataGridTemplate(headerCheckBox);
CheckBox itemCheckBox = new CheckBox();
itemCheckBox.ID = "chkSelect";
itemCheckBox.AutoPostBack = false;
itemCheckBox.Attributes.Add("OnClick","javascript:HighlightRowOnCheck(this);");
column.ItemTemplate = new DataGridTemplate(itemCheckBox);
grid.Columns.Add(column);
}
}
public class DataGridTemplate: ITemplate
{
System.Web.UI.WebControls.WebControl fControl;
public DataGridTemplate(System.Web.UI.WebControls.WebControl acontrol)
{
fControl = acontrol;
}
public void InstantiateIn(Control container)
{
container.Controls.Add(fControl);
}
}
}
**********************************************************************
cBCDataGridHighlighted.cs
**********************************************************************
namespace WebUI.BaseControls
{
public class cBCDataGridHighlighted: cBCDataGrid
{
protected virtual string generateJavaScriptCode()
{
String scriptCode = ...
return scriptCode;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
this.Controls.Add(
new LiteralControl(
"<script language='javascript'> "+
generateJavaScriptCode() +
"</script> "));
grid.ItemDataBound += new DataGridItemEventHandler(grid_ItemDataBound);
}
private void grid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if ((e.Item.ItemType==ListItemType.Item) ||
(e.Item.ItemType==ListItemType.AlternatingItem))
{
e.Item.Attributes.Add("OnMouseOver","javascript:HighlightRowOnMouseOver(this)");
e.Item.Attributes.Add("OnMouseOut","javascript:HighlightRowOnMouseOut(this)");
}
}
}
}
**********************************************************************
cBCDataGrid.cs
**********************************************************************
namespace WebUI.BaseControls
{
public class cBCDataGrid : Control, INamingContainer
{
protected DataGrid grid = new DataGrid();
public event EventHandler PreRender;
protected void OnPreRender(EventArgs e)
{
PreRender(this, e);
}
private void grid_PreRender(object sender, System.EventArgs e)
{
OnPreRender(EventArgs.Empty);
}
public object DataSource
{
get
{
this.EnsureChildControls();
return ((DataGrid)Controls[0]).DataSource;
}
set
{
this.EnsureChildControls();
((DataGrid)Controls[0]).DataSource = value;
}
}
public override void DataBind()
{
((DataGrid)Controls[0]).DataBind();
}
protected override void CreateChildControls()
{
grid.ID = "fDataGrid";
grid.BackColor = Color.White;
grid.BorderColor = Color.FromName("#336666");
grid.BorderStyle = BorderStyle.Double;
grid.BorderWidth = Unit.Pixel(3);
grid.CellPadding = 4;
grid.Font.Size = FontUnit.Smaller;
grid.Font.Name = "Verdana";
grid.GridLines = GridLines.Horizontal;
grid.HorizontalAlign = HorizontalAlign.Left;
grid.Width = Unit.Percentage(100);
grid.AllowPaging = true;
grid.AllowCustomPaging = false;
grid.PagerStyle.Visible = false;
grid.PageSize = 2;
grid.SelectedItemStyle.Font.Bold = true;
grid.SelectedItemStyle.ForeColor = Color.White;
grid.SelectedItemStyle.BackColor = Color.FromName("#339966");
grid.AlternatingItemStyle.BorderColor = Color.White;
grid.ItemStyle.ForeColor = Color.FromName("#333333");
grid.HeaderStyle.Font.Bold = true;
grid.HeaderStyle.ForeColor = Color.White;
grid.HeaderStyle.BackColor = Color.FromName("#336666");
grid.FooterStyle.ForeColor = Color.FromName("#333333");
grid.FooterStyle.BackColor = Color.White;
grid.PagerStyle.Visible = false;
grid.PagerStyle.NextPageText = "next";
grid.PagerStyle.PrevPageText = "previous";
grid.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
grid.PagerStyle.ForeColor = Color.White;
grid.PagerStyle.BackColor = Color.FromName("#336666");
grid.PreRender += new EventHandler(this.grid_PreRender);
this.Controls.Add(grid);
}
}
}
**********************************************************************
pruebas.aspx
**********************************************************************
namespace WebUI.Pages
{
public class pruebas : System.Web.UI.Page
{
protected WebUI.BaseControls.cBCDataGrid fDataGridHighlighted;
protected DataSet fAuxDataSet;
protected string fdbConnectionString;
protected SqlConnection fAuxConnection;
protected SqlDataAdapter fAuxDataAdapter;
protected int nRecordCount;
protected void fDataGridHighlighted_PreRender(Object sender, EventArgs e)
{
fAuxConnection = new SqlConnection(
Application["dbMelonesConnectionString"].ToString());
fAuxDataAdapter = new SqlDataAdapter();
fAuxDataAdapter.SelectCommand = new SqlCommand();
fAuxDataAdapter.SelectCommand.Connection = fAuxConnection;
fAuxDataAdapter.SelectCommand.CommandText = "sp_getlistofcharacteristics";
fAuxDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter ocurlanguageprefigparam = new SqlParameter ...
fAuxDataSet = new DataSet();
fAuxDataAdapter.Fill(fAuxDataSet,"tblCharacteristics");
nRecordCount = fAuxDataSet.Tables["tblCharacteristics"].Rows.Count;
fDataGridHighlighted.DataSource = fAuxDataSet;
fDataGridHighlighted.DataBind();
}
......
}
THANKS A LOT IN ADVANCE,
Leriel.