ClassPage中的MasterPage .FindControl

时间:2016-06-23 05:39:43

标签: c# asp.net

我尝试在内容页面的母版页访问中找到一个控件,一个准确的图像按钮。以下是母版页html代码:

<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>

当我将以下代码放在内容页面aspx.cs文件中时,我成功地这样做了

    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;
    }

现在我尝试创建一个名为GeneralClass的类文件,以便可以在任何内容页面中访问上面的代码。 sql命令只是从服务器中提取YES / NO值,所以我认为可以忽略我的问题。

以下是GeneralClass类文件中的代码:

    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;
    }

但不知何故,该行(masterPage.FindControl(“imgbtnMooring”)作为ImageButton)返回空值。

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

您可以使用页面扩展方法,因为我怀疑您的代码无法找到控件。

<强> Master.cs:

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

在GeneralClass类文件中添加以下NameSpaces:

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;
        }

    }
}

答案 1 :(得分:0)

我发现要做我想要的东西,即在母版页上找到控件,并使用服务器的YES / NO值启用/禁用按钮。以下是我使用的代码。与我在一个图像按钮上发布代码的问题不同,我会发布2个图像按钮的代码,以便更好地理解。此代码适用于我的&#39; GeneralClass&#39;类文件

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;
    }
}