Вот, пожалуйста. Я пробовал много поисков Google, и никто не мог ясно объяснить это. Вы должны выполнить это на PreRender Event. Обратите внимание, что этот фрагмент кода использует поставщика членства в .net, чтобы проверить, есть ли пользователь в роли. Если у вас есть свои собственные пользовательские таблицы, вам придется написать пользовательскую функцию, которая проверяет, входит ли пользователь в одну из ваших пользовательских ролей. Также обратите внимание, что в этом решении используется ItemTemplates
, а не BoundFields.
protected void detailsView_OnPreRender(object sender, EventArgs e)
{
if (dvPackage.CurrentMode == DetailsViewMode.Edit)
{
//disables/enables a the dropdown for Process Status if the user has the RLIST role
TextBox txtCustomID = (TextBox)Utilities.FindControlRecursive(dvPackage, "txtCustomID ");
txtCustomID.Visible = false;
if (User.IsInRole("Admin"))
{
txtCustomID.Visible = true;
}
}
Вот рекурсивная функция find find. Бесплатно.
public static Control FindControlRecursive(Control ctlRoot, string sControlId)
{
// if this control is the one we are looking for, break from the recursion
// and return the control.
if (ctlRoot.ID == sControlId)
{
return ctlRoot;
}
// loop the child controls of this parent control and call recursively.
foreach (Control ctl in ctlRoot.Controls)
{
Control ctlFound = FindControlRecursive(ctl, sControlId);
// if we found the control, return it.
if (ctlFound != null)
{
return ctlFound;
}
}// we never found the control so just return null.
return null;
}