母版.FindControl课堂 [英] MasterPage .FindControl in Class

查看:196
本文介绍了母版.FindControl课堂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种控制,图像按钮准确的说,从内容页的母版页的访问。以下是母版页的html代码:

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>

在这样做时,我把下面的代码在内容页aspx.cs文件

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



现在我尽量让名为GeneralClass一个类文件,这样上面的代码可以在任何内容页面的访问。 SQL命令只是提取YES /从服务器没有价值,所以我觉得它的东西,可以忽略我的问题。

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.

以下是在GeneralClass代码类文件:

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



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

But somehow the line (masterPage.FindControl("imgbtnMooring") as ImageButton) return null value.

谁能帮我解决这个问题?

Can anyone help me with this problem?

推荐答案

我觉得客场做我想要的东西这是找到母版页上,并通过YES控制/为启用从服务器没有价值/禁用按钮。以下是我使用的代码。不像我的问题在我张贴代码为一个图像按钮,我将发布2图像按钮代码,而不是为了更好的理解。这个代码是我的'GeneralClass'类文件

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

这篇关于母版.FindControl课堂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆