c# – 类中的MasterPage .FindControl

我试图在内容页面的母版页访问中找到一个控件,一个准确的图像按钮.以下是母版页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)返回null值.

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

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

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

    }
}
点赞