Wednesday, January 20, 2010

Asp.net id not declared in repeater control?

In asp.net i have a repeater control which shows some news items. With every item i enclosed an edit and delete link so admins can change the news. But if the userlevel is not adeqate for changing i want the buttons to dissapear. So what i tried to do is simply refer to the button like btnEdit.visible = false. The id of the buttons is btnEdit but it keeps on saying it's not declared. Is it possible that it has something to do with the repater control and id's inside it?Asp.net id not declared in repeater control?
Yes, you have to remember that the buttons are created at run-time since they are inside the repeaters.





Before I continue, I need to tell you that what you are doing is a poor security practice. Merely hiding the button is not enough. But that's another issue..





Now back to your question. The buttons can be accessed at the repeater_ItemDataBound event. Your code should be something similar to this:


%26lt;asp:Repeater ID=';MyRepeater'; runat=';server'; OnItemDataBound=';MyRepeater_DataBound';%26gt;


%26lt;ItemTemplate%26gt;


%26lt;asp:button id=';btnTest'; runat=';server'; Text=';Test'; /%26gt;


%26lt;/ItemTemplate%26gt;


%26lt;/asp:Repeater%26gt;





protected void MyRepeater_DataBound(object sender, RepeaterItemEventArgs e)


{


if ((e.Item.ItemType == ListItemType. Item) || (e.Item. ItemType == ListItemType. AlternatingItem) )


{


((Button) e.Item. FindControl (';btnTest';) ).Visible = false;


}


}





Hope this helps.Asp.net id not declared in repeater control?
In the .aspx file, make sure you have id=';btnEdit'; runat=';server';. Sometimes omitting the 'runat' will throw that error

No comments:

Post a Comment