.NETGURU
Setting up a datalist control - Item_DataBound for a datalist in a datalist
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.

Post a new message to this list...

Nevyn Twyll
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?

Reply to this message...
 
    
Karl
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" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Nevyn Twyll
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:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Karl
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" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Nevyn Twyll
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.DataItem, "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:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Nevyn Twyll
Here's the .aspx page I use...

"Nevyn Twyll" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Karl
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" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Nevyn Twyll
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:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Karl
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" <Click here to reveal e-mail address> wrote in message
news:%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Nevyn Twyll
Oh, and dll2 is bound the same way:

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

"Nevyn Twyll" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.ComponentModel.Container
System.EventArgs
System.Web.UI.DataBinder
System.Web.UI.WebControls.BoundColumn
System.Web.UI.WebControls.DataGrid
System.Web.UI.WebControls.DataList
System.Web.UI.WebControls.DataListItemEventArgs
System.Web.UI.WebControls.DataListItemEventHandler
System.Web.UI.WebControls.GridLines
System.Web.UI.WebControls.ListItemType
System.Windows.Forms.DataGrid




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification