468,133 Members | 1,509 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Setting up a datalist control - Item_DataBound for a datalist in a datalist

I have a multi-level datalist setup:
DataList1
DataList2
MyGrid

I have columns explicitly defined for the grid in the html.
Sometimes, On the initial Load() of the Page, I want to add extra columns to
the datagrid - for all records.

I tried getting a reference to the datagrid like this:
foreach(Control ctrl in this.DataList1.Controls)
{
if (ctrl.ID == "DataList2")
{
DataList2 = (DataList) ctrl;
break;
}
}

DataGrid MyGrid;

foreach(Control ctrl in dlstClasses.Controls)
{
if (ctrl.ID == "MyGrid")
{
MyGrid= (DataGrid) ctrl;
break;
}
}

But the inner DataList2 isn't found in the controls set of DataList1, and so
MyGrid isn't found inside DataList2, of course.

So how do I make those changes to the datagrid? Help?


Nov 18 '05 #1
8 2629
The reason your code doesn't work is two-fold, first off, DataList2 is a
child control of a child of DataList1 (it, it's not directly its child).
Also, for each row of your datasource, you'll have a new DataGrid2, and for
each row of that you'll have a MyGrid...

You best best for this kind of thing is to hook into the ItemDataBound event
of your grid:

private void Page_Load(object sender, EventArgs e) {
list.ItemDataBound += new
System.Web.UI.WebControls.DataListItemEventHandler (list_ItemDataBound);
}

private void list_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
ListItemType.Item) {
DataList dl2 = e.Item.FindControl("DataList2") as DataList;
if (dl2 != null) {
dl2.ItemDataBound += new
DataListItemEventHandler(dl2_ItemDataBound);

}
}
}

private void dl2_ItemDataBound(object sender, DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
ListItemType.Item) {
DataGrid myGrid = e.Item.FindControl("myGrid") as DataGrid;
if (myGrid != null) {
//do something with myGrid
}
}
}

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...
I have a multi-level datalist setup:
DataList1
DataList2
MyGrid

I have columns explicitly defined for the grid in the html.
Sometimes, On the initial Load() of the Page, I want to add extra columns to the datagrid - for all records.

I tried getting a reference to the datagrid like this:
foreach(Control ctrl in this.DataList1.Controls)
{
if (ctrl.ID == "DataList2")
{
DataList2 = (DataList) ctrl;
break;
}
}

DataGrid MyGrid;

foreach(Control ctrl in dlstClasses.Controls)
{
if (ctrl.ID == "MyGrid")
{
MyGrid= (DataGrid) ctrl;
break;
}
}

But the inner DataList2 isn't found in the controls set of DataList1, and so MyGrid isn't found inside DataList2, of course.

So how do I make those changes to the datagrid? Help?

Nov 18 '05 #2
I implemented this, and the list_ItemDataBound(...) fires, but the
dll2_ItemDataBound() doesn't ever fire. Do I need to bind to PreRender() or
ItemCreated() instead?
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:Ob**************@tk2msftngp13.phx.gbl...
The reason your code doesn't work is two-fold, first off, DataList2 is a
child control of a child of DataList1 (it, it's not directly its child).
Also, for each row of your datasource, you'll have a new DataGrid2, and
for
each row of that you'll have a MyGrid...

You best best for this kind of thing is to hook into the ItemDataBound
event
of your grid:

private void Page_Load(object sender, EventArgs e) {
list.ItemDataBound += new
System.Web.UI.WebControls.DataListItemEventHandler (list_ItemDataBound);
}

private void list_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType
==
ListItemType.Item) {
DataList dl2 = e.Item.FindControl("DataList2") as DataList;
if (dl2 != null) {
dl2.ItemDataBound += new
DataListItemEventHandler(dl2_ItemDataBound);

}
}
}

private void dl2_ItemDataBound(object sender, DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType
==
ListItemType.Item) {
DataGrid myGrid = e.Item.FindControl("myGrid") as DataGrid;
if (myGrid != null) {
//do something with myGrid
}
}
}

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...
I have a multi-level datalist setup:
DataList1
DataList2
MyGrid

I have columns explicitly defined for the grid in the html.
Sometimes, On the initial Load() of the Page, I want to add extra columns

to
the datagrid - for all records.

I tried getting a reference to the datagrid like this:
foreach(Control ctrl in this.DataList1.Controls)
{
if (ctrl.ID == "DataList2")
{
DataList2 = (DataList) ctrl;
break;
}
}

DataGrid MyGrid;

foreach(Control ctrl in dlstClasses.Controls)
{
if (ctrl.ID == "MyGrid")
{
MyGrid= (DataGrid) ctrl;
break;
}
}

But the inner DataList2 isn't found in the controls set of DataList1, and

so
MyGrid isn't found inside DataList2, of course.

So how do I make those changes to the datagrid? Help?


Nov 18 '05 #3
I can think of two things:
1 - When you debug, are you sure dl2.ItemDataBound += new
DataListItemEventHandler(dl2_ItemDataBound); is reached?

2 - (I bet this is the on), you need to hook the dl2.ItemDataBound event
before yo call dl2.DataBind()

Going with #2, from your original post it wasn't clear how you were binding
DataList2.

If I haven't given you enough to go on, you'll need to show me the relevant
aspx code (I'd like to see how all those controls are nested within each
other) and source code..

Cheers,
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:ul**************@TK2MSFTNGP14.phx.gbl...
I implemented this, and the list_ItemDataBound(...) fires, but the
dll2_ItemDataBound() doesn't ever fire. Do I need to bind to PreRender() or ItemCreated() instead?
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:Ob**************@tk2msftngp13.phx.gbl...
The reason your code doesn't work is two-fold, first off, DataList2 is a
child control of a child of DataList1 (it, it's not directly its child).
Also, for each row of your datasource, you'll have a new DataGrid2, and
for
each row of that you'll have a MyGrid...

You best best for this kind of thing is to hook into the ItemDataBound
event
of your grid:

private void Page_Load(object sender, EventArgs e) {
list.ItemDataBound += new
System.Web.UI.WebControls.DataListItemEventHandler (list_ItemDataBound);
}

private void list_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType
==
ListItemType.Item) {
DataList dl2 = e.Item.FindControl("DataList2") as DataList;
if (dl2 != null) {
dl2.ItemDataBound += new
DataListItemEventHandler(dl2_ItemDataBound);

}
}
}

private void dl2_ItemDataBound(object sender, DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType
==
ListItemType.Item) {
DataGrid myGrid = e.Item.FindControl("myGrid") as DataGrid;
if (myGrid != null) {
//do something with myGrid
}
}
}

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...
I have a multi-level datalist setup:
DataList1
DataList2
MyGrid

I have columns explicitly defined for the grid in the html.
Sometimes, On the initial Load() of the Page, I want to add extra
columns to
the datagrid - for all records.

I tried getting a reference to the datagrid like this:
foreach(Control ctrl in this.DataList1.Controls)
{
if (ctrl.ID == "DataList2")
{
DataList2 = (DataList) ctrl;
break;
}
}

DataGrid MyGrid;

foreach(Control ctrl in dlstClasses.Controls)
{
if (ctrl.ID == "MyGrid")
{
MyGrid= (DataGrid) ctrl;
break;
}
}

But the inner DataList2 isn't found in the controls set of DataList1,
and so
MyGrid isn't found inside DataList2, of course.

So how do I make those changes to the datagrid? Help?



Nov 18 '05 #4
Yes, I stepped thru, and it is doing (1), adding the event handler.

I don't call dll2's databind() explicitly. In the .aspx page, I set the
DataSource to a method in the .cs class:

<asp:DataGrid id="dgStudents" DataSource='<%#
GetStudentList((int)DataBinder.Eval(Container.Data Item, "ClassID")) %>'
runat="server" autogeneratecolumns="False" BorderWidth="0" GridLines="None"
CellPadding="0" CellSpacing="5">

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:uE**************@tk2msftngp13.phx.gbl...
I can think of two things:
1 - When you debug, are you sure dl2.ItemDataBound += new
DataListItemEventHandler(dl2_ItemDataBound); is reached?

2 - (I bet this is the on), you need to hook the dl2.ItemDataBound event
before yo call dl2.DataBind()

Going with #2, from your original post it wasn't clear how you were
binding
DataList2.


Nov 18 '05 #5
Oh, and dll2 is bound the same way:

<asp:datalist id="dlstClasses" runat="server" DataSource='<%#
GetClassList((int)DataBinder.Eval(Container.DataIt em, "ID")) %>'
Width="100%">

"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...
Yes, I stepped thru, and it is doing (1), adding the event handler.

I don't call dll2's databind() explicitly. In the .aspx page, I set the
DataSource to a method in the .cs class:

<asp:DataGrid id="dgStudents" DataSource='<%#
GetStudentList((int)DataBinder.Eval(Container.Data Item, "ClassID")) %>'
runat="server" autogeneratecolumns="False" BorderWidth="0"
GridLines="None" CellPadding="0" CellSpacing="5">

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:uE**************@tk2msftngp13.phx.gbl...
I can think of two things:
1 - When you debug, are you sure dl2.ItemDataBound += new
DataListItemEventHandler(dl2_ItemDataBound); is reached?

2 - (I bet this is the on), you need to hook the dl2.ItemDataBound event
before yo call dl2.DataBind()

Going with #2, from your original post it wasn't clear how you were
binding
DataList2.

Nov 18 '05 #6
Nevyn,
I could have saved us some trouble had I known that :)

Anyways, to get it working is pretty simple...we can delete the entire
list_ItemDataBound method that we added, keep dl2_ItemDataBound but make it
protected instead of private.

in your aspx page where you have your 2nd datalist do:

<asp:datalist id="dlstClasses" onItemDataBound="dl2_ItemDataBound" ...>

Since you are setting/binding in aspx, you need to hook up the events there
as well (the ItemDataBound of the parent is too late to hook up events)...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
Here's the .aspx page I use...

"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...
Yes, I stepped thru, and it is doing (1), adding the event handler.

I don't call dll2's databind() explicitly. In the .aspx page, I set the
DataSource to a method in the .cs class:

<asp:DataGrid id="dgStudents" DataSource='<%#
GetStudentList((int)DataBinder.Eval(Container.Data Item, "ClassID")) %>'
runat="server" autogeneratecolumns="False" BorderWidth="0"
GridLines="None"
CellPadding="0" CellSpacing="5">

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:uE**************@tk2msftngp13.phx.gbl...
I can think of two things:
1 - When you debug, are you sure dl2.ItemDataBound += new
DataListItemEventHandler(dl2_ItemDataBound); is reached?

2 - (I bet this is the on), you need to hook the dl2.ItemDataBound event before yo call dl2.DataBind()

Going with #2, from your original post it wasn't clear how you were
binding
DataList2.


Nov 18 '05 #7
Okay, it does call the method now. But, interestingly, I'm adding columns
to the datagrid, but not even the headers show up in the resulting output.
Isn't this the way I'd add new columns?
It runs through just fine, it just doesn't...work...

protected void dlstClass_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
ListItemType.Item )

{

DataGrid dgStudent = e.Item.FindControl("dgStudents") as DataGrid;

foreach(string sColumn in m_asFields)

{

int iNewColumn = dgStudent.Columns.Count;

BoundColumn dgc = new BoundColumn();
dgc.HeaderText = sColumn;
dgc.DataField = sColumn;

dgStudent.Columns.Add(dgc);

dgStudent.Columns[iNewColumn].ItemStyle.CssClass = "ItemStyle";

dgStudent.Columns[iNewColumn].HeaderStyle.CssClass = "ProductHeader";

}

}

}

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:Oo**************@TK2MSFTNGP15.phx.gbl...
Nevyn,
I could have saved us some trouble had I known that :)

Anyways, to get it working is pretty simple...we can delete the entire
list_ItemDataBound method that we added, keep dl2_ItemDataBound but make
it
protected instead of private.

in your aspx page where you have your 2nd datalist do:

<asp:datalist id="dlstClasses" onItemDataBound="dl2_ItemDataBound" ...>

Since you are setting/binding in aspx, you need to hook up the events
there
as well (the ItemDataBound of the parent is too late to hook up events)...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
Here's the .aspx page I use...

"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...
> Yes, I stepped thru, and it is doing (1), adding the event handler.
>
> I don't call dll2's databind() explicitly. In the .aspx page, I set
> the
> DataSource to a method in the .cs class:
>
> <asp:DataGrid id="dgStudents" DataSource='<%#
> GetStudentList((int)DataBinder.Eval(Container.Data Item, "ClassID")) %>'
> runat="server" autogeneratecolumns="False" BorderWidth="0"
> GridLines="None"
> CellPadding="0" CellSpacing="5">
>
> "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in > message news:uE**************@tk2msftngp13.phx.gbl...
>>I can think of two things:
>> 1 - When you debug, are you sure dl2.ItemDataBound += new
>> DataListItemEventHandler(dl2_ItemDataBound); is reached?
>>
>> 2 - (I bet this is the on), you need to hook the dl2.ItemDataBound event >> before yo call dl2.DataBind()
>>
>> Going with #2, from your original post it wasn't clear how you were
>> binding
>> DataList2.
>>
>



Nov 18 '05 #8
I think you are running into more of the timing issue. It looks like by the
time ItemDataBound is called on dlstClass, your dgStudent has already been
bound - this makes sense if you think about it. Therefore you are adding
columns but the DataBind() operation has already occured.

We have gotten off track of the initial question and I don't think I have
the right scope to help you with this new problem (as you know, it took a
while to get to the initial solution, I'd like to avoid that if possible).

However, I can give you a quick and possible solution, not sure if it'll fit
your needs.

As you know from the last fix, we got things working by putting the
OnItemDataBound inside the aspx control. We did this because you were
binding in there and the dlstClass_ItemDataBound was too late to hook the
event. I think if you want to do what you are doing (without know enough to
make possible alternate suggestions), you should reverse what we did.
Instead of putting the OnItemDataBound in the gdStudents tag because that's
where the binding was, you should consider doing the binding and the
onITemDataBound in the dlstClass_ItemDataBound function:

if (dgStudent != null){
//add your columns
dgStudent.DataSource = GetSource();
dgStudent.onItemDataBound += ....
dgStudent.DataBind();
}

I think something like that may work.

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Okay, it does call the method now. But, interestingly, I'm adding columns
to the datagrid, but not even the headers show up in the resulting output.
Isn't this the way I'd add new columns?
It runs through just fine, it just doesn't...work...

protected void dlstClass_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
ListItemType.Item )

{

DataGrid dgStudent = e.Item.FindControl("dgStudents") as DataGrid;

foreach(string sColumn in m_asFields)

{

int iNewColumn = dgStudent.Columns.Count;

BoundColumn dgc = new BoundColumn();
dgc.HeaderText = sColumn;
dgc.DataField = sColumn;

dgStudent.Columns.Add(dgc);

dgStudent.Columns[iNewColumn].ItemStyle.CssClass = "ItemStyle";

dgStudent.Columns[iNewColumn].HeaderStyle.CssClass = "ProductHeader";

}

}

}

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:Oo**************@TK2MSFTNGP15.phx.gbl...
Nevyn,
I could have saved us some trouble had I known that :)

Anyways, to get it working is pretty simple...we can delete the entire
list_ItemDataBound method that we added, keep dl2_ItemDataBound but make
it
protected instead of private.

in your aspx page where you have your 2nd datalist do:

<asp:datalist id="dlstClasses" onItemDataBound="dl2_ItemDataBound" ...>

Since you are setting/binding in aspx, you need to hook up the events
there
as well (the ItemDataBound of the parent is too late to hook up events)...
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
Here's the .aspx page I use...

"Nevyn Twyll" <as****@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...
> Yes, I stepped thru, and it is doing (1), adding the event handler.
>
> I don't call dll2's databind() explicitly. In the .aspx page, I set
> the
> DataSource to a method in the .cs class:
>
> <asp:DataGrid id="dgStudents" DataSource='<%#
> GetStudentList((int)DataBinder.Eval(Container.Data Item, "ClassID")) %>' > runat="server" autogeneratecolumns="False" BorderWidth="0"
> GridLines="None"
> CellPadding="0" CellSpacing="5">
>
> "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in
> message news:uE**************@tk2msftngp13.phx.gbl...
>>I can think of two things:
>> 1 - When you debug, are you sure dl2.ItemDataBound += new
>> DataListItemEventHandler(dl2_ItemDataBound); is reached?
>>
>> 2 - (I bet this is the on), you need to hook the dl2.ItemDataBound

event
>> before yo call dl2.DataBind()
>>
>> Going with #2, from your original post it wasn't clear how you were
>> binding
>> DataList2.
>>
>



Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David W. Simmonds | last post: by
10 posts views Thread by Bharat | last post: by
6 posts views Thread by tshad | last post: by
1 post views Thread by Rachel Devons | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.