Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep state in Custom Datagrid

Status
Not open for further replies.

mir23

Programmer
Oct 28, 2003
11
BE
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
 
Sorry this is an ASP.NET topic, I'll post the thread on the ASP.NET forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top