MasterPage.FindControl in class

I am trying to find a control, more precisely, an image button, access to the main page from content pages. Below is the html main page code:

<body>
    <form id="form1" runat="server">
    <div class="navLeft">
        <br />           
        <asp:ImageButton ID="imgbtnMooring" runat="server" 
            Height="60px" ImageUrl="~/Item/RibbonIcon/Dashboard.png" />
        <br />
    </div>
    <div class="navTop">            
    </div>
    <div class="banner">
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    <div class="divider">
        <asp:ContentPlaceHolder id="ContentPlaceHolder2" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    <div class="content">
        <asp:ContentPlaceHolder id="ContentPlaceHolder3" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>

I managed to do this when I put the following code on the content page of the aspx.cs file

    string validMooring = "";

    comm = new SqlCommand("SELECT * FROM dbo.StructureCurrent", conn);
    conn.Open();
    reader = comm.ExecuteReader();
    while (reader.Read())
    {
       validMooring = reader["StructureMooring"].ToString();
    }

    switch (validMooring)
    {
        case "YES":
            (Page.Master.FindControl("imgbtnMooring") as ImageButton).Enabled = true;
            (Page.Master.FindControl("imgbtnMooring") as ImageButton).ImageUrl = "~/Item/RibbonIcon/Dashboard.png";
            break;
        case "NO":
            (Page.Master.FindControl("imgbtnMooring") as ImageButton).Enabled = false;
            (Page.Master.FindControl("imgbtnMooring") as ImageButton).ImageUrl = "~/Item/RibbonIcon - Grey/DashboardGrey.png";
            break;
        default:
            break;
    }

I'm currently trying to create a class file called GeneralClass so that the code can be accessed on any content page. The sql command is just to retrieve the YES / NO value from the server, so I think it might be ignored for my problem.

The following is the code in the GeneralClass class file:

    MasterPage masterPage = new MasterPage();
    masterPage.MasterPageFile = "~/GeneralLayout.master";

    string validMooring = "";

    comm = new SqlCommand("SELECT * FROM dbo.StructureCurrent", conn);
    conn.Open();
    reader = comm.ExecuteReader();
    while (reader.Read())
    {
        validMooring = reader["StructureMooring"].ToString();
    }

    switch (validMooring)
    {
        case "YES":
            (masterPage.FindControl("imgbtnMooring") as ImageButton).Enabled = true;
            (masterPage.FindControl("imgbtnMooring") as ImageButton).ImageUrl = "~/Item/RibbonIcon/Dashboard.png";
            break;
        case "NO":
            (masterPage.FindControl("imgbtnMooring") as ImageButton).Enabled = false;
            (masterPage.FindControl("imgbtnMooring") as ImageButton).ImageUrl = "~/Item/RibbonIcon - Grey/DashboardGrey.png";
            break;
        default:
            break;
    }

But anyway, the line (masterPage.FindControl ("imgbtnMooring") like ImageButton) returns a null value.

Can someone help me with this problem?

+4
2

, , , YES/NO / . , . , , 2- , . "GeneralClass"

public void validNavLeft()
{
    string validHull = "", validMooring = "";

    comm = new SqlCommand("SELECT * FROM dbo.StructureCurrent", conn);
    conn.Open();
    reader = comm.ExecuteReader();
    while (reader.Read())
    {
        validHull = reader["StructureHull"].ToString();
        validMooring = reader["StructureMooring"].ToString();
    }

    var pageHandler = HttpContext.Current.CurrentHandler;
    Control ctrlHull = ((Page)pageHandler).Master.FindControl("imgbtnHull");
    ImageButton imgBtnHull = (ImageButton)ctrlHull;
    Control ctrlMooring = ((Page)pageHandler).Master.FindControl("imgbtnMooring");
    ImageButton imgBtnMooring = (ImageButton)ctrlMooring;

    switch (validHull)
    {
        case "YES":
            imgBtnHull.Enabled = true;
            imgBtnHull.ImageUrl = "~/Item/RibbonIcon/Dashboard.png";
            break;
        case "NO":
            imgBtnHull.Enabled = false;
            imgBtnHull.ImageUrl = "~/Item/RibbonIcon - Grey/DashboardGrey.png";
            break;
        default:
            break;
    }
    switch (validMooring)
    {
        case "YES":
            imgBtnMooring.Enabled = true;
            imgBtnMooring.ImageUrl = "~/Item/RibbonIcon/Dashboard.png";
            break;
        case "NO":
            imgBtnMooring.Enabled = false;
            imgBtnMooring.ImageUrl = "~/Item/RibbonIcon - Grey/DashboardGrey.png";
            break;
        default:
            break;
    }
}
0

, , .

Master.cs:

private void Page_Load(object sender, System.EventArgs e)
{
  this.EnableControls(null);
}

NameSpaces GeneralClass:

using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public static void EnableControls(this Page page, ControlCollection ctrl)
{
    if (ctrl == null)
        ctrl = page.Controls;

    string validMooring = "";

    comm = new SqlCommand("SELECT * FROM dbo.StructureCurrent", conn);
    conn.Open();
    reader = comm.ExecuteReader();
    while (reader.Read())
    {
      validMooring = reader["StructureMooring"].ToString();
    }

    foreach (Control item in ctrl)
    {
        if (item.Controls.Count > 0)
            EnableControls(page, item.Controls, isEnable);

        if (item.GetType() == typeof(ImageButton))
        {
            switch (validMooring)
            {
              case "YES":
                ((ImageButton)item).Enabled = true;
                ((ImageButton)item).ImageUrl = "~/Item/RibbonIcon/Dashboard.png";
                 break;

              case "NO":
                ((ImageButton)item).Enabled = false;
                ((ImageButton)item).ImageUrl = "~/Item/RibbonIcon - Grey/DashboardGrey.png";
                break;
            default:
                break;
        }

    }
}
+2

All Articles