Results 1 to 5 of 5

Thread: DataList child controls [resolved]

  1. #1

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    DataList child controls [resolved]

    I have a DataGrid that I populate from a DataTable. In the FooterTemplate, I have 2 LinkButtons. How do I control the LinkButtons? When I try to access them directly like:

    lnkbtnPrev.Visible = False

    I get an error saying 'Object reference not set to an instance of an object' Now, I think I need to use the DataList's Controls collection, but I am unsure of how. I thought it would be"

    dlstNews.Controls.Item(1).Visible = False

    but that didn't do anything.
    Last edited by blindlizard; Oct 31st, 2004 at 06:13 PM.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Datalist or datagrid? In reality I suppose it doesn't matter, they are both databound template controls.

    There are a few ways you can accomplish this. You can change the visibility of the control inside the DataList_ItemDataBound routine...

    Or you can access it directly in the Page_PreRender.

    But considering once you bind the data, it doesn't change, there is no reason to have the framework parse code to change its visibility in the Page_PreRender event since the datalist is bound and knows what it is supposed to do thanks to viewstate.

    see this thread for an example:

    http://www.vbforums.com/showthread.p...hreadid=310220

  3. #3

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    Ok, I think that will do it, but what about if the button is in the footertemplate? Is it the same way?
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    The footer does not receive a DataItem, because its not bound to data. However, the datalist control still fires the _ITemDataBound event once for the header, and once for the footer. It just doesn't provide a datarecord to bind to (obviously).

    So yes, you can use the DataBound event. You just check for
    the footer itemtype (see code below)
    VB Code:
    1. Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
    2. Handles DataList1.ItemDataBound
    3.        
    4.         'remember, anything you want to access here on the
    5.         'server-side (code-behind) must have included a runat=server tag
    6.         'on the front-side (aspx html) page.
    7.         '<FooterTemplate>
    8.         '<asp:LinkButton ID="lb123" Runat="server"></asp:LinkButton>
    9.         '<asp:LinkButton ID="lb124" Runat="server"></asp:LinkButton>
    10.         '</FooterTemplate>
    11.  
    12.  
    13.         [b]If e.Item.ItemType = ListItemType.Footer Then[/b]
    14.  
    15.             Dim l As LinkButton
    16.             l = DirectCast(e.Item.Controls(1), LinkButton)
    17.             l.CommandArgument = 3
    18.             l.Text = "CANCEL"
    19.  
    20.             'now get 2nd linkbutton defined in the footer template
    21.             l = DirectCast(e.Item.Controls(3), LinkButton)
    22.             l.Text = "SUBMIT"
    23.             l.CommandArgument = 5
    24.  
    25.         End If
    26.     End Sub

  5. #5

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    Oh perfect, thanks!
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width