470,815 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Reading columns in a datagrid that is nested in a repeater

Hi everyone,

I have a datagrid that I create inside a Repeater at runtime. I have
defined a checkbox beside each row so that users can select multiple rows.
<asp:datagrid id="dgProducts" HorizontalAlign="Center"
AutoGenerateColumns="false" CellPadding="2"
runat="server" >
......
<asp:TemplateColumn HeaderText="" HeaderStyle-Width="20">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="cbxProduct" Runat="server"
AutoPostBack="False"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

Just below my datagrid I have a button (Add button). I want to read the
values of all the checkboxes in my dynamically created datagrid. But it
keeps complaining that

System.NullReferenceException: Object reference not set to an instance of an
object.
for that Line Response.Write...

This is the code
private void btnAddProducts_Click(object sender, System.EventArgs e)
{
DataGrid dgProducts = this.Page.FindControl("dgProducts") as DataGrid;
Response.Write("Rows: " + dgProducts.Items.Count);
}

Can someone please help me with this?

Thank you
Maz
Nov 19 '05 #1
3 1429
wrong name FindControl("dgProducts") : name (and ids) must be reasonably
unique and are thus :
- prefixed by the name of the parent container.
- suffixed by a number

you will have to iterate through the object hierarchy looking for objects
whose id includes "cbxProduct"

"Maziar Aflatoun" <ma***@rogers.com> a écrit dans le message de news:
08********************@rogers.com...
Hi everyone,

I have a datagrid that I create inside a Repeater at runtime. I have
defined a checkbox beside each row so that users can select multiple rows.
<asp:datagrid id="dgProducts" HorizontalAlign="Center"
AutoGenerateColumns="false" CellPadding="2"
runat="server" >
.....
<asp:TemplateColumn HeaderText="" HeaderStyle-Width="20">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="cbxProduct" Runat="server"
AutoPostBack="False"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

Just below my datagrid I have a button (Add button). I want to read the
values of all the checkboxes in my dynamically created datagrid. But it
keeps complaining that

System.NullReferenceException: Object reference not set to an instance of
an object.
for that Line Response.Write...

This is the code
private void btnAddProducts_Click(object sender, System.EventArgs e)
{
DataGrid dgProducts = this.Page.FindControl("dgProducts") as DataGrid;
Response.Write("Rows: " + dgProducts.Items.Count);
}

Can someone please help me with this?

Thank you
Maz

Nov 19 '05 #2
Isn't dgProducts unique? Can you please show me an example? (Prefixed by the
name of the parent container and suffixed by a number)? Is there an easier
way to do this?

Thanks
Maz.

"Michel de Becdelièvre" <m_*****@msn.com> wrote in message
news:Oe**************@TK2MSFTNGP09.phx.gbl...
wrong name FindControl("dgProducts") : name (and ids) must be reasonably
unique and are thus :
- prefixed by the name of the parent container.
- suffixed by a number

you will have to iterate through the object hierarchy looking for objects
whose id includes "cbxProduct"

"Maziar Aflatoun" <ma***@rogers.com> a écrit dans le message de news:
08********************@rogers.com...
Hi everyone,

I have a datagrid that I create inside a Repeater at runtime. I have
defined a checkbox beside each row so that users can select multiple
rows.
<asp:datagrid id="dgProducts" HorizontalAlign="Center"
AutoGenerateColumns="false" CellPadding="2"
runat="server" >
.....
<asp:TemplateColumn HeaderText="" HeaderStyle-Width="20">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="cbxProduct" Runat="server"
AutoPostBack="False"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

Just below my datagrid I have a button (Add button). I want to read the
values of all the checkboxes in my dynamically created datagrid. But it
keeps complaining that

System.NullReferenceException: Object reference not set to an instance of
an object.
for that Line Response.Write...

This is the code
private void btnAddProducts_Click(object sender, System.EventArgs e)
{
DataGrid dgProducts = this.Page.FindControl("dgProducts") as DataGrid;
Response.Write("Rows: " + dgProducts.Items.Count);
}

Can someone please help me with this?

Thank you
Maz


Nov 19 '05 #3
Hi Maz,

No, it’s in the Repeater, so it’s not unique.
You should use repeater.Items[index].FindControl("dgProducts").

HTH

Elton Wang
el********@hotmail.com
"Maziar Aflatoun" wrote:
Hi everyone,

I have a datagrid that I create inside a Repeater at runtime. I have
defined a checkbox beside each row so that users can select multiple rows.
<asp:datagrid id="dgProducts" HorizontalAlign="Center"
AutoGenerateColumns="false" CellPadding="2"
runat="server" >
......
<asp:TemplateColumn HeaderText="" HeaderStyle-Width="20">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="cbxProduct" Runat="server"
AutoPostBack="False"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

Just below my datagrid I have a button (Add button). I want to read the
values of all the checkboxes in my dynamically created datagrid. But it
keeps complaining that

System.NullReferenceException: Object reference not set to an instance of an
object.
for that Line Response.Write...

This is the code
private void btnAddProducts_Click(object sender, System.EventArgs e)
{
DataGrid dgProducts = this.Page.FindControl("dgProducts") as DataGrid;
Response.Write("Rows: " + dgProducts.Items.Count);
}

Can someone please help me with this?

Thank you
Maz

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Lalit Bhatia | last post: by
reply views Thread by Lalit Bhatia | last post: by
reply views Thread by Maziar Aflatoun | last post: by
3 posts views Thread by Antonio Maciel | last post: by
6 posts views Thread by John Ruiz | last post: by
2 posts views Thread by Simon Harris | last post: by
2 posts views Thread by mark | last post: by
2 posts views Thread by Ralf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.