473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2808
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roman | last post by:
I have built a simple user control that contains 2 buttons, a text box and a dropdownlist. When a button is clicked it sets the visible property of the textbox/dropdownlist and the button. ie the...
1
by: David W. Simmonds | last post by:
I have a Button in a DataList Footer. I add the click event like this: void Item_DataBound(Object sender, DataListItemEventArgs e) { if(e.Item.ItemType == ListItemType.Footer) { Button ctlSave...
10
by: Bharat | last post by:
Hi Folks, Suppose I have two link button on a page (say lnkBtn1 and lnkBtn2). On the click event of the lnkbtn1 I have to add a dynamically created control. And On the click event of the lnkBtn2 I...
3
by: Stimp | last post by:
I have a datalist that outputs a link to a picture in each ItemTemplate: e.g. <asp:DataList id="DataList1" ...etc> <ItemTemplate> <a href='Pic.aspx?pic=somenumber'>picture</a> </ItemTemplate>...
6
by: tshad | last post by:
I need to get to a status label I have on my footer section of my datalist. There is no event happening that would go to the footer. I am just doing some processing and want to update the label...
5
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able...
1
by: Rachel Devons | last post by:
All, I have a table with three rows, and within each row I have a DataList that is set to use 3 columns. The problem I'm having is that each DataList sets it's columns to be different widths so...
2
by: Hans Merkl | last post by:
Hi, I am trying to use a user control as EditItemTemplate in a DataList. It loads fine but I can't figure out how to bind to the data of the DataList. Here is what I have got so far: ...
1
by: berny.zamora | last post by:
Hello everyone, I have a composite control (lets call it the parent) that contains a datalist. The datalist has an ItemTemplate that contains another composite control (lets call it the child)....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.