GridView, dynamic template fields, FindControl problem

  • Thread starter Thread starter beton3000
  • Start date Start date
B

beton3000

Hello!

I'm building a GridView using code to add template fields, because
there is a random number of columns in result set from database. I'm
using a class with ITemplate interface for defining columns. I am
using a code which adds a checkbox for each column:

public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.DataRow:

CheckBox cb = new CheckBox();
cb.Enabled = true;
cb.ID = columnName;

cb.DataBinding += new
EventHandler(this.CB_DataBinding);

container.Controls.Add(cb);
break;

default:

break;
}
}

When I display the grid, everything looks fine, but when the page is
postback and GridView_RowUpdating event occurs, I'm unable to find any
checkbox using GridView.Rows[e.RowIndex].FindControl method. I am able
only to find empty TableCells.

I have no more ideas how to solve this problem, please help me.
 
Do you instantiate the template in every request? Best way to do it would be
in OnInit event of the Page.
 
Thank you for your answer.

I have a reload_controls() method which is run in following cases:

Page_Load / !IsPostBack
GridView_RowEditing
GridView_RowCancelingEdit

reload_controls() consists code which read data from controls on page,
builds gridview (using template fields) and finally calls
GridView.DataBind(). If I try to run reload_controls() from OnInit, I
see an obvious error saying that controls are not instantiated.
Unfortunatelly I need to read from other controls to build gridview...


Do you instantiate the template in every request? Best way to do it would be
in OnInit event of the Page.

--
Teemu Keiski
AspInsider, ASP.NET MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net




I'm building a GridView using code to add template fields, because
there is a random number of columns in result set from database. I'm
using a class with ITemplate interface for defining columns. I am
using a code which adds a checkbox for each column:
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.DataRow:
CheckBox cb = new CheckBox();
cb.Enabled = true;
cb.ID = columnName;
cb.DataBinding += new
EventHandler(this.CB_DataBinding);
container.Controls.Add(cb);
break;
break;
}
}

When I display the grid, everything looks fine, but when the page is
postback and GridView_RowUpdating event occurs, I'm unable to find any
checkbox using GridView.Rows[e.RowIndex].FindControl method. I am able
only to find empty TableCells.
I have no more ideas how to solve this problem, please help me.- Ukryj cytowany tekst -

- Poka cytowany tekst -
 
Hi,

see: http://discuss.joelonsoftware.com/default.asp?dotnet.12.350266.2

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

Thank you for your answer.

I have a reload_controls() method which is run in following cases:

Page_Load / !IsPostBack
GridView_RowEditing
GridView_RowCancelingEdit

reload_controls() consists code which read data from controls on page,
builds gridview (using template fields) and finally calls
GridView.DataBind(). If I try to run reload_controls() from OnInit, I
see an obvious error saying that controls are not instantiated.
Unfortunatelly I need to read from other controls to build gridview...


Do you instantiate the template in every request? Best way to do it would
be
in OnInit event of the Page.

--
Teemu Keiski
AspInsider, ASP.NET
MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net




I'm building a GridView using code to add template fields, because
there is a random number of columns in result set from database. I'm
using a class with ITemplate interface for defining columns. I am
using a code which adds a checkbox for each column:
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.DataRow:
CheckBox cb = new CheckBox();
cb.Enabled = true;
cb.ID = columnName;
cb.DataBinding += new
EventHandler(this.CB_DataBinding);
container.Controls.Add(cb);
break;
break;
}
}

When I display the grid, everything looks fine, but when the page is
postback and GridView_RowUpdating event occurs, I'm unable to find any
checkbox using GridView.Rows[e.RowIndex].FindControl method. I am able
only to find empty TableCells.
I have no more ideas how to solve this problem, please help me.- Ukryj
cytowany tekst -

- Poka cytowany tekst -
 
Thank you very much Teemu. This solution sounds very reasonable.
Unfortunately my time for this problem passed by and I decided not to
use GridView at all in this case... I will build custom table with
checkboxes.


Hi,

see:http://discuss.joelonsoftware.com/default.asp?dotnet.12.350266.2

--
Teemu Keiski
AspInsider, ASP.NET MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net




Thank you for your answer.
I have a reload_controls() method which is run in following cases:
Page_Load / !IsPostBack
GridView_RowEditing
GridView_RowCancelingEdit
reload_controls() consists code which read data from controls on page,
builds gridview (using template fields) and finally calls
GridView.DataBind(). If I try to run reload_controls() from OnInit, I
see an obvious error saying that controls are not instantiated.
Unfortunatelly I need to read from other controls to build gridview...
Do you instantiate the template in every request? Best way to do it would
be
in OnInit event of the Page.
--
Teemu Keiski
AspInsider, ASP.NET
MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net

Hello!
I'm building a GridView using code to add template fields, because
there is a random number of columns in result set from database. I'm
using a class with ITemplate interface for defining columns. I am
using a code which adds a checkbox for each column:
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.DataRow:
CheckBox cb = new CheckBox();
cb.Enabled = true;
cb.ID = columnName;
cb.DataBinding += new
EventHandler(this.CB_DataBinding);
container.Controls.Add(cb);
break;
default:
break;
}
}
When I display the grid, everything looks fine, but when the page is
postback and GridView_RowUpdating event occurs, I'm unable to find any
checkbox using GridView.Rows[e.RowIndex].FindControl method. I am able
only to find empty TableCells.
I have no more ideas how to solve this problem, please help me.- Ukryj
cytowany tekst -
- Poka cytowany tekst -- Ukryj cytowany tekst -

- Poka cytowany tekst -
 
Back
Top