views:

7375

answers:

7

Now this is all way simplified, but here goes:

I have a User Control that consists only of a single *.ascx file. The control has no code-behind: it's just a script with a few functions, like this:

<%@ Control Language="VB" EnableViewState="False" ClassName="MyControlType" %>
<script runat="server">
    Public Function MyFunction() As String
       return "CalledMyFunction!"
    End Function
</script>

That's the entire file. I can successfully add this control to an aspx page using markup like so:

<%@ Register Src="~/path/to/Control.ascx" TagPrefix="aaa" TagName="MyControl" %>
...
<aaa:MyControl runat="server" id="MyControl1" />

Now what I want to do is call MyFunction from the page's code-behind, like this:

Dim someString As String = MyControl1.MyFunction()

Unfortunately, I can't do that. Instead, I get a compile error to the effect of "'MyFunction' is not a member of 'System.Web.UI.UserControl'."

I've also tried this:

Dim someString As String = DirectCast(MyControl1, MyControlType).MyFunction()

and then the compiler tells me, "Type 'MyControlType' is not defined."

I've played with this a lot, and I just can't make it work. All efforts to cast MyControl1 to a more exact type have failed, as have other work-arounds. I suspect the problem is that the ascx file without a code-behind is unable to be compiled to an assembly but the code-behind wants to be compiled to an assembly and therefore the compiler gets confused about what type the control is.

What do I need to do to be able to call that function?

[edit]
So I'm just gonna have to add code-behind for the user control. It's what I wanted to do anyway. I'd still like to know how to do this without needing one, though.

A: 

Make sure that the MyControl1 object in your code-behind is of type MyControlType and is casted as such when calling that function. The compiler is stating that it cannot find the method MyFunction() in the base class of UserControl.

NYSystemsAnalyst
I know that much. Try the example I posted in a simple page and you'll find it's not that simple- the code behind has no knowledge of any "MyControlType" type.
Joel Coehoorn
A: 

I don't do much VB (more of a C# guy) but I think I've got this working!

You have a typo on runat:

<script ruant="server">

Try fixing that and it works!

Macka
Fixed the typo: I checked and it's spelled correctly in the actual code.
Joel Coehoorn
Works for me - pasted code in answer below.
Macka
Does your MyControlType inherit from UserControl?
Macka
I've tried it both ways: remember, though: there's no code behind for the control at all, and I can't add one at the moment due to outside forces.
Joel Coehoorn
A: 

I believe you need to derive from WebUserControl.

In your .ascx "Control" tag place the following:

Inherits="WebApplication1.WebUserControl1"

Of course you'll need to use the proper names.

+2  A: 

Weird works for me.

Imports Microsoft.VisualBasic

Public Class MyControlType
    Inherits UserControl
End Class

.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Src="~/WebUserControl.ascx" TagPrefix="aaa" TagName="MyControl"  %>
...
<aaa:MyControl runat="server" id="MyControl1"  />

.

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        Dim someString As String = MyControl1.MyFunction()
    End Sub

End Class

.

<%@ Control Language="VB" EnableViewState="False"  %>
<script runat="server">
    Public Function MyFunction() As String
       return "CalledMyFunction!"
    End Function
</script>
Macka
I don't have a code-behind for the user control
Joel Coehoorn
Okay, last go! The code above still works for me if I delete the MyControlType.vb class!
Macka
Did you also delete the .designer.vb file?
Joel Coehoorn
And are you building this as a web site or web application?
Joel Coehoorn
Yup - all I have now is default.aspx, default.aspx.vb and WebUserControl.ascx
Macka
Ah as a website, so that may explain it!
Macka
A: 

The issue has to do with there being no code-behind file that defines the class. As soon as you put the public function in a code-behind file, it works. I think ASP.NET is treating that function more like an anonymous function rather than an actual function in a class.

Thats my thought anyway.

Redbeard 0x0A
A: 
A: 

Place this in your code behind declarations

protected <solutionName>.<controlName> myControl1; /*C#*/

here myControl1 is the id of your user control. Now you may call public functions of this control.

Mani