gpt4 book ai didi

c# - MasterPage .FindControl类

转载 作者:行者123 更新时间:2023-11-30 12:23:10 26 4
gpt4 key购买 nike

我试图在从内容页面访问母版页时找到一个控件,准确地说是一个图像按钮。以下是母版页 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") as ImageButton) 返回空值。

谁能帮我解决这个问题?

最佳答案

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

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

}
}

关于c# - MasterPage .FindControl类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37983174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com