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?