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? 8 2706
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?
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?
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?
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.
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.
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.
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. >> >
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. >> >
This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by Roman |
last post: by
|
1 post
views
Thread by David W. Simmonds |
last post: by
|
10 posts
views
Thread by Bharat |
last post: by
|
3 posts
views
Thread by Stimp |
last post: by
|
6 posts
views
Thread by tshad |
last post: by
|
5 posts
views
Thread by Patrick.O.Ige |
last post: by
|
1 post
views
Thread by Rachel Devons |
last post: by
|
2 posts
views
Thread by Hans Merkl |
last post: by
|
1 post
views
Thread by berny.zamora |
last post: by
| | | | | | | | | | |