【问题标题】:Why is my UserControl Visible property always returning false?为什么我的 UserControl Visible 属性总是返回 false?
【发布时间】:2019-12-31 11:39:48
【问题描述】:

我有一个问题,即我的用户控件上的 Visible 属性总是返回 False,不管它是否应该在页面上可见。我有以下用户控制:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="VB_WebApp_Test.WebUserControl1" %>
<h1>My User Control</h1>
Public Class WebUserControl1
    Inherits UserControl

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.PreRender
        If Visible Then
            Console.WriteLine("VISIBLE")
        End If
    End Sub

End Class

我加载到以下页面:

<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="VB_WebApp_Test.Default" %>
<%@ Register src="WebUserControl1.ascx" tagName="WebUserControl" tagPrefix="Mine"%>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <table>
                <tr>
                    <td>
                        <asp:MultiView runat="server" ID="multiView">
                            <asp:View ID="vInit" runat="server">
                                <h1>Initial View</h1>
                                <asp:Button runat="server" ID="toControlButton" Text="To Control"/>
                            </asp:View>
                            <asp:View ID="vControl" runat="server">
                                <Mine:WebUserControl ID="wuc1" runat="server"/>
                                <asp:Button runat="server" ID="toNonControlButton" Text="To Non Control"/>
                            </asp:View>
                        </asp:MultiView>
                    </td>
                </tr>
            </table>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
Public Class [Default]
    Inherits Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        multiView.ActiveViewIndex = 0
    End Sub

    Public Sub ToControl(sender As Object, e As EventArgs) Handles toControlButton.Click
        multiView.ActiveViewIndex = 1
    End Sub

    Public Sub ToNonControl(sender As Object, e As EventArgs) Handles toNonControlButton.Click
        multiView.ActiveViewIndex = 0
    End Sub

End Class

我想做的是让它在显示用户控件时执行一些初始化逻辑,但Visible 属性始终设置为False。我已经尝试连接到LoadPreRenderInit 事件,并且所有这些事件的Visible 属性都是False

【问题讨论】:

    标签: asp.net vb.net user-controls


    【解决方案1】:

    当你的 ActiveViewIndex 在按钮的 Click 事件中更新时,事件的顺序是:

    1) 页面加载,ActiveIndex 为 0,UC 不可见。

    2) 加载 UserControl,由于 View 不可见,UC 不可见

    3) 事件处理,ActiveIndex更新为1,所有相关控件的Visibility属性更新

    4) Page.LoadComplete 事件(Visible 已经为 True,但你没有该事件)

    5) Page.PreRender (Visible 已经是 True,但是你还没有这个事件)

    6) UC.PreRender - 在这里您应该看到 Visible=True。

    我刚刚在空项目上对此进行了测试,它可以正常工作。检查 PreRender 事件时,您一定错过了什么。

    基于可见性的初始化不是很好的做法。我建议你添加一些这样的初始化:

    Public Sub Init()
        'Do init here
    End Sub
    

    【讨论】:

    • 感谢您的回答,这使我只需要挂钩 UC.PreRender 事件,该事件只会在我期望的时候触发。另外,是的,我知道我并不特别希望在变得可见时进行初始化,但是我正在对旧网站进行新修复,并且我需要的数据不会在每个初始化中都存在,但会是当控件被渲染时。我完全同意它又脏又可怕。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    相关资源
    最近更新 更多