Hi.
I have my project 90% working with one Major hitch - ANY help is appreciated!
In a nutshell, it's a Server control which derives directly from an ASP DataGrid.
I then add a button column, some other meaningless stuff, and autogenerate the columns from a DataReader.
The problem is, sometimes the dynamically created column shows up on the grid - sometimes not.
I have a feeling that I'm battling ViewState - but I don't know how or why - or when for that matter.
Even the slightest change in the order in whaich I call things makes a world of difference.
Can anyone see a grotesque error here?
By the way - I have the whole thing working perfectly using a UserControl - I'm just trying to move each part one by one from the UserControl into a ServerControl so I can compile the dern thing and reuse it.
Also, I use the DataGrid's native DataSource and DataBind commands with a simple DataReader - the binding works fine.
Code below:
[ToolboxData("<{0}:GridSingleLinkButton runat=serverX></{0}:GridSingleLinkButton>")]
public class GridSingleLinkButton : System.Web.UI.WebControls.DataGrid
<snip>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ID = "dgMain";
base.AutoGenerateColumns = true;
DataKeyField = "MyDatabaseIDField";
}
protected override void CreateChildControls()
{
ButtonColumn BC = new ButtonColumn();
Columns.Add(BC);
BC.CommandName = "SelectItem";
BC.ButtonType = ButtonColumnType.LinkButton;
BC.DataTextField = "PrimaryColumn";
base.CreateChildControls();
// NOTE!! If I call base.CreateChildControls first - other wacky things happen.
}
<snip>
Other code not shown down here to hook up the button column click events. When the button column actually does show itself in the grid, this event code works every time - flawlessly. I can get the ID of the item clicked by using:
protected override void OnItemCommand(DataGridCommandEventArgs e)
{
if (e.CommandName == "SelectItem")
DoSomething(DataKeys[e.Item.ItemIndex].ToString());
base.OnItemCommand(e);
}