469,964 Members | 1,754 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

GridView, dynamic template fields, FindControl problem

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.

Sep 1 '07 #1
4 8855
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 MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
<be*******@gmail.comwrote in message
news:11********************@g4g2000hsf.googlegroup s.com...
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.

Sep 1 '07 #2
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...
On 1 Wrz, 15:37, "Teemu Keiski" <jot...@aspalliance.comwrote:
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

<beton3...@gmail.comwrote in message

news:11********************@g4g2000hsf.googlegroup s.com...
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 -

Sep 1 '07 #3
Hi,

see: http://discuss.joelonsoftware.com/de...et.12.350266.2

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

<be*******@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
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...
On 1 Wrz, 15:37, "Teemu Keiski" <jot...@aspalliance.comwrote:
>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

<beton3...@gmail.comwrote in message

news:11********************@g4g2000hsf.googlegrou ps.com...
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 -


Sep 2 '07 #4
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.
On 2 Wrz, 09:04, "Teemu Keiski" <jot...@aspalliance.comwrote:
Hi,

see:http://discuss.joelonsoftware.com/de...et.12.350266.2

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

<beton3...@gmail.comwrote in message

news:11**********************@22g2000hsm.googlegro ups.com...
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...
On 1 Wrz, 15:37, "Teemu Keiski" <jot...@aspalliance.comwrote:
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
<beton3...@gmail.comwrote in message
>news:11********************@g4g2000hsf.googlegrou ps.com...
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 -

Sep 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by NateDawg | last post: by
3 posts views Thread by Rob Roberts | last post: by
reply views Thread by JayD | last post: by
1 post views Thread by Chicagoboy27 | last post: by
3 posts views Thread by Jeff | last post: by
5 posts views Thread by Andrew Robinson | last post: by
4 posts views Thread by Craig Buchanan | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.