Reputation: 255
I try to find a control, an image button to be exact, in master page access from content pages. Following is the master page html 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 success in doing so when i put the following code in the content page 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;
}
Right now I try to make a class file named GeneralClass so that above code can be access in any content pages. The sql command is just to extract the YES/NO value from server, so I think it something that can be ignore for my problem.
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 somehow the line (masterPage.FindControl("imgbtnMooring") as ImageButton) return null value.
Can anyone help me with this problem?
Upvotes: 4
Views: 1356
Reputation: 255
I found away to do the thing that i want which is to find the control on master page and by using the YES/NO value from server to enabled/disabled the button. Following is the code I use. Unlike in my question in which I post code for one image button, I will post code for 2 image button instead for better understanding. This code is for my 'GeneralClass' class file
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;
}
}
Upvotes: 0
Reputation: 912
You can use Page Extension method as I suspect your code is not able to find the controls.
Master.cs:
private void Page_Load(object sender, System.EventArgs e)
{
this.EnableControls(null);
}
Add following NameSpaces in GeneralClass class file:
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;
}
}
}
Upvotes: 2