Joel Coehoorn
Joel Coehoorn

Reputation: 416039

Call function in User Control from code behind of the page

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.

Upvotes: 5

Views: 25415

Answers (8)

Mukund Thakkar
Mukund Thakkar

Reputation: 1305

You have to add your vb code in vb file

Public Function MyFunction() As String
        Return "CalledMyFunction!"
    End Function

Upvotes: -1

Mani
Mani

Reputation: 67

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.

Upvotes: -1

user21826
user21826

Reputation: 3644

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.

Upvotes: 0

user21826
user21826

Reputation: 3644

Here's what I did.

  1. Create a new web site.
  2. Add a new Web User Control - Ensure that you uncheck the "Place code in separate file" checkbox on that dialog.
  3. Place the function in the generated .ascx
  4. Register the control in default.aspx and run

Contents of WebUserControl.ascx

<%@ Control Language="VB" ClassName="WebUserControl" %>

<script runat="server"> Public Function MyFunction() As String Return "CalledMyFunction!" End Function </script>

Contents of Default.aspx


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Src="WebUserControl.ascx" TagPrefix="aaa" TagName="MyControl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <aaa:MyControl runat="server" ID="mycontrolid" />
    <%
        Dim i As String = mycontrolid.MyFunction()
        Response.Write(i)
     %>
    </div>
    </form>
</body>
</html>

Upvotes: 0

Astra
Astra

Reputation: 11231

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.

Upvotes: 0

BigJump
BigJump

Reputation: 16419

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>

Upvotes: 3

BigJump
BigJump

Reputation: 16419

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!

Upvotes: 0

DCNYAM
DCNYAM

Reputation: 12126

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.

Upvotes: 0

Related Questions