我试图在课堂上找到一个转发器控件而且我不知道我在哪里做错了。
在我的ASPX页面中,我有包含两个图像按钮的转发器,并在页面加载时调用我在类中定义的方法。
这是我的课程页面,
public class Authorization
{
public Authorization()
{
}
public void ChangeControlStatus(ControlCollection PageControls, int Role_id)
{
using (EHSIMSDataContext db = new EHSIMSDataContext())
{
var Role_Assigned = (from auth in db.AUTHORISATIONs
where auth.ROLE_ID.Equals(Convert.ToInt32(Role_id)) && auth.PAGE_ACCESS.Equals(1)
select auth);
foreach (Control ctrl in PageControls)
{
string ControlName = (ctrl.GetType()).Name;
switch (ControlName)
{
case "ImageButton":
ImageButton imgbut = (ImageButton)ctrl;
{
foreach (var role in Role_Assigned)
{
if(role.ADD_ACCESS == false)
if(imgbut.ID.Equals("BtnAdd"))
imgbut.Enabled = false;
}
}
break;
case "Repeater":
Repeater rep = (Repeater)ctrl;
foreach (RepeaterItem item in rep.Items)
{
ImageButton img_but = item.FindControl("lnkEdit") as ImageButton;
if (role.EDIT_ACCESS == false)
if (img_but.ID.Equals("lnkEdit"))
img_but.Enabled = false;
}
break;
}
ChangeControlStatus(ctrl.Controls, Role_id);
}
}
}
} 这就是我在ASPX页面中所做的,
protected void Page_Load(object sender, EventArgs e)
{
Authorization assign_auth = new Authorization();
assign_auth.ChangeControlStatus(Page.Controls, Roleid);
if (!IsPostBack)
{
-----
现在,我将所有控件传递给方法“ChangeControlStatus” 因为在转发器内部有图像按钮,上述方法中的foreach不起作用,但它找到了“Repeater”控件。 我该怎么做才能在方法中找到控件?
补充讯息。 所以,在我们的项目中说简单,我们有许多用户具有不同的角色,基于我想要启用/禁用的角色或控件。 我不能在项目的每一页都这样做,因为有很多控件和很多页面。
答案 0 :(得分:1)
Repeater rpt = (Repeater)Page.FindControl("rptr");
ImageButton imgBtn = (ImageButton)rpt.FindControl("lnkEdit");
if (role.EDIT_ACCESS == false)
imgBtn.Enabled = false;
答案 1 :(得分:1)
您可以尝试递归搜索:
public static T FindControl<T>(string id, Control rootControl) where T : Control {
if (rootControl == null)
throw new ArgumentNullException("rootControl");
var controls = new Stack<Control>();
controls.Push(rootControl);
while (controls.Count > 0) {
var currentControl = controls.Pop();
var typedControl = currentControl as T;
if (typedControl != null && string.Compare(typedControl.ID, id, StringComparison.OrdinalIgnoreCase) == 0)
return typedControl;
foreach (Control childControl in currentControl.Controls) {
controls.Push(childControl);
}
}
return null;
}
调用(在页面上下文中):
var repeater = FindControl<Repeater>("rptr", Page);
foreach(RepeaterItem item in repeater.Items) {
var imgBtn = FindControl<ImageButton>("lnkEdit", item);
}
答案 2 :(得分:0)
明白了......!谢谢大家的回复...... 现在,我不是在页面加载事件中调用函数,而是在repeater_item数据绑定中调用它,并传递RepeaterItemEventArgs。 这就是我所做的,
protected void rptrdepartment_databound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem != null)
{
Authorization assign_auth = new Authorization();
int Roleid = Convert.ToInt32(Session["Roleid"]);
assign_auth.ChangeControlStatus(Page.Controls, Roleid, "Approval Path", e);
}
}
在方法中我做到了,
public void ChangeControlStatus(ControlCollection PageControls, int Role_id,string Submenu_Name,RepeaterItemEventArgs e)
{
foreach (Control ctrl in PageControls)
{
string ControlName = (ctrl.GetType()).Name;
switch (ControlName)
{
case "ImageButton":
ImageButton imgbut = (ImageButton)ctrl;
ImageButton img_but_delte = e.Item.FindControl("lnkDelete") as ImageButton;
ImageButton img_but_edit = e.Item.FindControl("lnkEdit") as ImageButton;
{
foreach (var role in Role_Assigned)
{
if(role.ADD_ACCESS == false)
if (imgbut.ID.Equals("btnAdd"))
imgbut.Enabled = false;
if (role.DELETE_ACCESS == false)
img_but_delte.Enabled = false;
if (imgbut.ID.Equals("btnlogout"))
img_but_edit.Enabled = false;
}
}
break;
}
ChangeControlStatus(ctrl.Controls, Role_id,Submenu_Name,e);
}
}