无法在占位符内访问控制或点击事件

时间:2016-05-25 13:32:39

标签: asp.net vb.net

我正在尝试创建自定义Panel-Expander-Module。扩展器有效,但如果我尝试将旧代码剪切并复制到占位符中,VS2015会告诉我存在一些错误。

  1. 错误:" lstXYZ"由于其保护级别而未声明可能无法访问
  2. 错误:Handles子句需要在包含类型或其基类型之一中定义的WithEvents变量
  3. 这是一个代码示例:

    包含面板占位符的Usercontrol(panelExpander.ascx):

    <%-- Begin Panel Wrapper --%>
    <div  id="panelContainer" class="Panel collapseable slim" runat="server">
    
        <asp:HiddenField ID="hidPanelClosed" runat="server" Value=""></asp:HiddenField>
    
        <%-- Begin Panel Header --%>
        <div class="panel-header PanelHeader">
            <span><asp:PlaceHolder runat="server" ID="phPanelHeader" /></span>
            <div class="PanelToggleIcon">
                <img  id="panelToggleIcon" src="" runat="server">
            </div>
        </div>
        <%-- End Panel Header --%>
    
        <%-- Begin Panel Content --%>
        <div id="panelContentContainer" class="panel-content" runat="server">
            <asp:PlaceHolder runat="server" ID="phPanelContent" />
        </div>
        <%-- End Panel Content --%>
    </div>
    <%-- End Panel Wrapper --%>
    

    在de panelExpander.vb中:

            ''' <summary>
            ''' ***
            ''' </summary>
            ''' <returns></returns>
            <PersistenceMode(PersistenceMode.InnerProperty)>
            Public Property PanelHeader As ITemplate
    
            ''' <summary>
            ''' ***
            ''' </summary>
            ''' <returns></returns>
            <PersistenceMode(PersistenceMode.InnerProperty)>
            Public Property PanelContent As ITemplate
    
            ''' <summary>
            ''' ***
            ''' </summary>
            ''' <param name="sender">sender</param>
            ''' <param name="e">event Arguments</param>
            Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                Me.Initialize() 'setting some classes etc nothing special
    
                If (IsPostBack) Then
                    'do stuff
                End If
    
            End Sub
    

    上面的代码工作正常。如果我喂它没有什么特别的东西,如:

    <%-- Loaded Usercontrol with placeholders --%>
    <uc:PanelExpander runat="server">
        <%--Panel header--%>
        <PanelHeader>
            <p>Header Text</p>
        </PanelHeader>
    
        <%--Panel content--%>
        <PanelContent>
            <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr...</p>
        </PanelContent>
    </uc:PanelExpander>
    

    问题是由MyEditMask.ascx和MyEditMask.vb引起的:

    <%@ Register Src="...panelExpander.ascx" TagName="PanelExpander" TagPrefix="uc" %>
    <uc:PanelExpander runat="server">
        <panelheader>
            <%-- Nothign happens here, works fine. --%>
            <asp:Label runat="server" ID="lblHeader" Text="Header"></asp:Label>
        </panelheader>
    
        <panelcontent>
            <%-- seems ok in ascx but in codebehin it throws errors --%>
             <asp:ListBox ID="lstAvailable" runat="server" SelectionMode="Multiple"
             DataTextField="CAL_NAME" DataValueField="CAL_ID" Rows="10" CssClass="some classes"></asp:ListBox>
    
            <%-- seems ok in ascx but in codebehin it throws errors --%>
            <asp:Button runat="server" ID="btnAdd" Text="Add >> " class="DefaultButton qb-default" />
    
        </panelcontent>
    </uc:PanelExpander>
    

    在MyEditMask.vb中(错误在哪里):

    ''' <summary>
    ''' ****
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub FillListBox()
    
        'Do stuff
    
        ' Can't access lstAvailable 
        '-> "lstAvailable" not declared it may be inaccessible due to its protection level
        lstAvailable.Items.Clear()
    
        'Do Stuff
    
    End Sub
    
    
    ''' <summary>
    ''' ***
    ''' </summary>
    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    
        'btnAdd throws error 
        '-> Handles clause requires a WithEvents variable defined in the 
        'containing type or one of its base types
    
    
        'Do Stuff
    
    End Sub
    

    感觉有点奇怪,因为控件在designer.vb中也因某种原因消失了。在我使用任何占位符之前,这一切都有效。我感觉我复制到占位符的控件已经从我的&#34; MyEditMask.vb和MyEditMask.ascx&#34;的视图中消失了。我仍然不明白为什么。

    希望有人可以帮助我,我疯了似乎很容易但仍然......

    先谢谢!

2 个答案:

答案 0 :(得分:1)

执行以下操作之一......

将ClientIDMode添加到页面声明

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="YourPage.aspx.vb"
    ClientIDMode="Static" %>

或者将ClientIDMode添加到控件

<panelcontent>
    <%-- seems ok in ascx but in codebehin it throws errors --%>
     <asp:ListBox ID="lstAvailable" runat="server" ClientIDMode="Static" SelectionMode="Multiple"
     DataTextField="CAL_NAME" DataValueField="CAL_ID" Rows="10" CssClass="some classes"></asp:ListBox>

    <%-- seems ok in ascx but in codebehin it throws errors --%>
    <asp:Button runat="server" ID="btnAdd" ClientIDMode="Static" Text="Add >> " class="DefaultButton qb-default" />

</panelcontent>

这应该允许您的代码通过其ID识别控件。

答案 1 :(得分:1)

好的!我找到了解决方案。特别感谢@Mych!

我改变了这一点:

<PersistenceMode(PersistenceMode.InnerProperty)>
Public Property PanelContent As ITemplate

对此:

<PersistenceMode(PersistenceMode.InnerProperty)>
<TemplateInstance(TemplateInstance.Multiple)>
Public Property PanelContent As ITemplate

然后我能够访问MyEditMask中的控件。只有一件事丢失了。我能够访问控件,但他们没有设置。 Mych建议我使用“FindControl”方法。随之而来,它终于完成了。

希望这可以帮助有同样问题的人。