LinkButton in a DataList
I have a datalist and in its header t开发者_StackOverflow社区emplate I have a linkbutton.In my codebehind file I wrote as I've always written:
((LinkButton)(DataList1.FindControl("LinkButton1"))).Enabled = false;
but this gives me the error:
Object reference not set to an instance of an object.
How can I access this linkbutton?
You should use FindControl()
in the template that you use (e.g ItemTemplate
)
Your call to FindControl
isn't finding anything - you need to ensure that something is found before you cast it and try to use it.
This approach is safer:
LinkButton linkButton
= DataList1.FindControl("LinkButton1") as LinkButton;
if (linkButton != null)
linkButton.Enabled = false;
If the LinkButton is embedded in a container like a Panel or other control you will have to reach inside of it. FindControl does not recurse through the child controls of the collection.
For example, you might have to do something like this with whatever nested control structure you have:
FindControl("Panel1").FindControl("LinkButton1").Enabled ...
Try with :
**LinkButton lnk = (LinkButton)e.Item.FindControl("LnkPager");**
Comment :: LnkPager is a linkbutton in My Datalist
精彩评论