Megajin Megajin - 6 months ago 40
Vb.net Question

Can't access control or click event within placeholder

I am trying to create a custom Panel-Expander-Module. The Expander works, but if I try to cut and copy my old code into the placeholders, VS2015 tells me that there are some errors.


  1. Error: "lstXYZ" not declared it may be inaccessible due to its protection level

  2. Error: Handles clause requires a WithEvents variable defined in the containing type or one of its base types



Here is a code example:

The Usercontrol which holds the Placeholders for the panels (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 --%>


In 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


The Code above is working fine. if I feed it with nothing special like:

<%-- 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>


The Problem is caused here in MyEditMask.ascx nad 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>


In MyEditMask.vb (where the errors are):

''' <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


It feels a little bit strange because the controls disappear in the designer.vb too for some reason. This all worked before I used any placeholder. I've got the feeling that the controls which I copy into the placeholders are gone from the view of my "MyEditMask.vb and MyEditMask.ascx". Still I can't understand why.

Hope someone can help me, I'm going crazy it seems so easy but still...

Thanks in advanced!

Answer

Alright! I've found a solution. Special thanks to @Mych!

I've changed this:

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

To this:

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

Then I was able to access the controls in MyEditMask. There was just one thing missing. I was able to access the controls but they were set to nothing. Mych was suggesting that I use "FindControl" Method. With that it was finally done.

Hope this helps someone with the same problem.