簡體   English   中英

無法從后面的代碼訪問用戶控件的子控件

[英]Unable to access User Control's child controls from code behind

我正在嘗試從后面的代碼訪問某些用戶控件的子控件。 其中一些正在工作,而另一些則沒有。 錯誤的用戶控件沒有設計器文件。 我想知道這是為什么嗎? 如果是這樣,那么我就不能從頭開始創建某些控件,因為它們太復雜了,我的設計師在沒有設計師文件的情況下就發送了這些控件,而且當他出城時我也無法將代碼退還給他。

此外,在設計視圖中沒有設計器文件的情況下,它們看起來很棒。 但是當我寫這樣的東西:

Button1.Visible=false;

這表明Button1在當前上下文中不存在。 請告訴我該如何解決?

以下是錯誤的用戶控件之一的標記:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="commentUI.ascx.cs" Inherits="commentUI" %>
<asp:Button ID="Button1" runat="server" Text="Button" />

代碼隱藏文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class commentUI : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Visible = false;
    }
}

嘗試從頭開始創建一個ascx,它也可以顯示設計器。

借助@raja的提示,我找到了解決方案。 我將Inherits屬性更新為Inherits="MyProjectNamespace.User_Controls.commentUI"並成功運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM