How to set visibility of a control in DataList template?

G

Guest

I want to set the visible of pnlEditButton according to current login.
However, the control is not a member of this(Page) and FindControl() returns
null.

protected void Page_Load(object sender, System.EventArgs e)
{
bool isAdmin = User.IsInRole("Administrators");
this.FindControl("pnlEditButton").Visible = isAdmin;
}

<asp:DataList ID="DataList1" runat="server" DataSourceID="ObjectDataSource1"
....>
<ItemTemplate>
<div class="membercard">
<asp:panel id="pnlEditButton" runat="server">

</asp:panel>
 
R

ReyN

hello nick

for templated controls, such as the DataList, you'd normally use
FindControl either during the ItemCreated or ItemDataBound events of
the the control, since you'd have to find a control which is rendered
for each row in the list, that is, you'd have to look for a
pnlEditButton for each row before you can set it.

for an example of coding the handler for the ItemCreated event

void setButton ( Object src, DataListItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ) {
Panel pnlEditButton = ( Panel ) e.Item.FindControl (
"pnlEditButton " );
pnlEditButton.Visible = User.IsInRole("Administrators") ;
}
}

or something like that
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top