Hello,
I have passed quite enought time with this and I cannot sort this out:
I have a custom control which inherit from the datagrid class. In the Page class, I add TemplateColumn instance (with custom ItemTemplate and EditItemTemplate class) to the Columns collection.
When I Create the custom Datagrid programmatically, it works fine but when I use the designer the control does not keep his state between postback, here is a snipset:
//In VB.NET in the Page class , sorry :~)...
...
Protected WithEvents MyDG1 As MyComponents.Forms.MyDG
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
AddHandler MyDG1.GetData, AddressOf GetDataSource
InitializeComponent()
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tempColumn As TemplateColumn
Dim columnName As String
With MyDG1
.AutoGenerateColumns = False
.DataKeyField = "HistoryID"
tempColumn = New TemplateColumn
tempColumn.ItemStyle.CssClass = "dgTxtArea"
tempColumn.HeaderText = "Description"
tempColumn.ItemTemplate = New GenericItem("Description")
tempColumn.EditItemTemplate = New EditTextArea("Description", "dgTxtArea")
.SetTemplateColumn = tempColumn
...
End With
End Sub
//The DataGrid Control
[ParseChildren(true)]
public class MyDG: DataGrid
{
...
protected override void CreateChildControls()
{
Controls.Clear();
base.CreateChildControls ();
//Add templateCol Columns Collection
for(int i=0; i<= templColumns.Length -1; i++){
if(templColumns!=null)
this.Columns.Add(templColumns);
else break;
}
}
public override void DataBind()
{
this.EnsureChildControls();
base.DataBind ();
}
// Get the datasource from the page class
private ICollection GetDataSource()
{
DataTable dt;
dt= this.GetData();
return dt.DefaultView;
}
...
}
//The item template class
public class GenericItem: ITemplate
{
private string column;
public GenericItem3(string column){
this.column = column;
}
public void InstantiateIn(Control container){
Literal l = new Literal();
l.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(l);
}
public void BindData(object sender, EventArgs e){
Literal l = (Literal) sender;
DataGridItem container = (DataGridItem) l.NamingContainer;
l.Text = ((DataRowView) container.DataItem)[column].ToString();
}
}
I will greatly appreciate your suggestion,
Mir23
I have passed quite enought time with this and I cannot sort this out:
I have a custom control which inherit from the datagrid class. In the Page class, I add TemplateColumn instance (with custom ItemTemplate and EditItemTemplate class) to the Columns collection.
When I Create the custom Datagrid programmatically, it works fine but when I use the designer the control does not keep his state between postback, here is a snipset:
//In VB.NET in the Page class , sorry :~)...
...
Protected WithEvents MyDG1 As MyComponents.Forms.MyDG
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
AddHandler MyDG1.GetData, AddressOf GetDataSource
InitializeComponent()
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tempColumn As TemplateColumn
Dim columnName As String
With MyDG1
.AutoGenerateColumns = False
.DataKeyField = "HistoryID"
tempColumn = New TemplateColumn
tempColumn.ItemStyle.CssClass = "dgTxtArea"
tempColumn.HeaderText = "Description"
tempColumn.ItemTemplate = New GenericItem("Description")
tempColumn.EditItemTemplate = New EditTextArea("Description", "dgTxtArea")
.SetTemplateColumn = tempColumn
...
End With
End Sub
//The DataGrid Control
[ParseChildren(true)]
public class MyDG: DataGrid
{
...
protected override void CreateChildControls()
{
Controls.Clear();
base.CreateChildControls ();
//Add templateCol Columns Collection
for(int i=0; i<= templColumns.Length -1; i++){
if(templColumns!=null)
this.Columns.Add(templColumns);
else break;
}
}
public override void DataBind()
{
this.EnsureChildControls();
base.DataBind ();
}
// Get the datasource from the page class
private ICollection GetDataSource()
{
DataTable dt;
dt= this.GetData();
return dt.DefaultView;
}
...
}
//The item template class
public class GenericItem: ITemplate
{
private string column;
public GenericItem3(string column){
this.column = column;
}
public void InstantiateIn(Control container){
Literal l = new Literal();
l.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(l);
}
public void BindData(object sender, EventArgs e){
Literal l = (Literal) sender;
DataGridItem container = (DataGridItem) l.NamingContainer;
l.Text = ((DataRowView) container.DataItem)[column].ToString();
}
}
I will greatly appreciate your suggestion,
Mir23