Search code examples
vb.nettagsconstantsascx

How to use a constant definde in a .vb file as the id for an element in an .ascx page?


Let's say I have a Constants class defined in a .vb file as such:

Public Const myConst As String = "myID"

And in my page.ascx file I wish to do something like this:

...
<someControl ID="<%$ Constants.myConst %>" runat="server" />
...

This shields compilation errors all over the place.

I've attempted several combinations, including = or # instead of $, but none seem to work.

Note that I've read and do not believe this to be a duplicate of this answer, nor other similars.


Solution

  • I don't think this is even possible since Visual Studio use the ID to create an instance of that object in the .vb file. I would question the purpose of this but if you really need to I would suggest to generate a dictionary.

    <someControl ID="ctrlName" runat="server" />
    

    And in the init

    Dim dic As New Dictionary(Of String, Control)
    
    dic.Add(Constants.myConst, ctrlName)
    

    Usually it's the other way around where the web page tells the dll which id/instance to use.