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

Discussion in 'ASP .Net' started by Nevyn Twyll, Sep 7, 2004.

  1. Nevyn Twyll

    Nevyn Twyll Guest

    I have a multi-level datalist setup:

    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;

    DataGrid MyGrid;

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

    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?
    Nevyn Twyll, Sep 7, 2004
    1. Advertisements

  2. Nevyn Twyll

    Karl Guest

    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

    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


    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, Sep 7, 2004
    1. Advertisements

  3. Nevyn Twyll

    Nevyn Twyll Guest

    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?
    Nevyn Twyll, Sep 9, 2004
  4. Nevyn Twyll

    Karl Guest

    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

    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..

    Karl, Sep 9, 2004
  5. Nevyn Twyll

    Nevyn Twyll Guest

    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">
    Nevyn Twyll, Sep 9, 2004
  6. Nevyn Twyll

    Nevyn Twyll Guest

    Oh, and dll2 is bound the same way:

    <asp:datalist id="dlstClasses" runat="server" DataSource='<%#
    GetClassList((int)DataBinder.Eval(Container.DataItem, "ID")) %>'
    Nevyn Twyll, Sep 9, 2004
  7. Nevyn Twyll

    Karl Guest

    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, Sep 9, 2004
  8. Nevyn Twyll

    Nevyn Twyll Guest

    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'

    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[iNewColumn].ItemStyle.CssClass = "ItemStyle";

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



    Nevyn Twyll, Sep 9, 2004
  9. Nevyn Twyll

    Karl Guest

    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 += ....

    I think something like that may work.

    MY ASP.Net tutorials

    Karl, Sep 9, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.