|
-
Oct 23rd, 2004, 12:54 AM
#1
Thread Starter
Frenzied Member
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.
-
Oct 27th, 2004, 07:00 AM
#2
I wonder how many charact
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
-
Oct 28th, 2004, 10:28 PM
#3
Thread Starter
Frenzied Member
Ok, I think that will do it, but what about if the button is in the footertemplate? Is it the same way?
-
Oct 29th, 2004, 06:36 AM
#4
I wonder how many charact
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:
Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
Handles DataList1.ItemDataBound
'remember, anything you want to access here on the
'server-side (code-behind) must have included a runat=server tag
'on the front-side (aspx html) page.
'<FooterTemplate>
'<asp:LinkButton ID="lb123" Runat="server"></asp:LinkButton>
'<asp:LinkButton ID="lb124" Runat="server"></asp:LinkButton>
'</FooterTemplate>
[b]If e.Item.ItemType = ListItemType.Footer Then[/b]
Dim l As LinkButton
l = DirectCast(e.Item.Controls(1), LinkButton)
l.CommandArgument = 3
l.Text = "CANCEL"
'now get 2nd linkbutton defined in the footer template
l = DirectCast(e.Item.Controls(3), LinkButton)
l.Text = "SUBMIT"
l.CommandArgument = 5
End If
End Sub
-
Oct 31st, 2004, 06:13 PM
#5
Thread Starter
Frenzied Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|