1

我试图在课堂上找到一个中继器控件,但我不知道我在哪里做错了。

在我的 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 不起作用,但它找到了“中继器”控件。我应该怎么做才能找到方法中的控件?

附加信息。因此,简单地说,在我们的项目中,我们有许多具有不同角色的用户,基于我想要启用/禁用的角色或控件。我不能在我们项目的每个页面中都这样做,因为有很多控件和很多页面。

4

3 回答 3

1
  Repeater rpt = (Repeater)Page.FindControl("rptr");

        ImageButton imgBtn = (ImageButton)rpt.FindControl("lnkEdit");

        if (role.EDIT_ACCESS == false)
                imgBtn.Enabled = false;
于 2013-07-08T07:54:59.223 回答
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);
}
于 2013-07-08T08:34:23.790 回答
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);
        }
    }
于 2013-07-09T06:11:41.847 回答