从页面后面的代码调用用户控件中的函数

发布于 2024-07-10 08:35:14 字数 1210 浏览 4 评论 0原文

现在这一切都被简化了,但这里是:

我有一个仅包含单个 *.ascx 文件的用户控件。 该控件没有隐藏代码:它只是一个带有一些函数的脚本,如下所示:

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

这就是整个文件。 我可以使用标记成功地将这个控件添加到 aspx 页面,如下所示:

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

现在我想做的是从页面的代码隐藏中调用 MyFunction,如下所示:

Dim someString As String = MyControl1.MyFunction()

不幸的是,我不能这样做。 相反,我收到一个编译错误,结果是“'MyFunction'不是'System.Web.UI.UserControl'的成员。

我也尝试过这个:

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

然后编译器告诉我,“类型'MyControlType'未定义。

我已经玩过很多次了,但我就是无法让它工作。 所有将 MyControl1 转换为更精确类型的努力都失败了,其他解决方法也是如此。 我怀疑问题在于没有代码隐藏的 ascx 文件无法编译为程序集,但代码隐藏想要编译为程序集,因此编译器对控件的类型感到困惑。

我需要做什么才能调用该函数?

[编辑]
所以我只需要为用户控件添加隐藏代码。 无论如何,这就是我想做的。 不过,我仍然想知道如何在不需要的情况下做到这一点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

北渚 2024-07-17 08:35:14

对我来说很奇怪。

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>

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>
甜中书 2024-07-17 08:35:14

确保代码隐藏中的 MyControl1 对象属于 MyControlType 类型,并且在调用该函数时进行了强制转换。 编译器指出它在 UserControl 的基类中找不到方法 MyFunction()。

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.

韵柒 2024-07-17 08:35:14

我不太用 VB(更多的是 C# 人员),但我想我已经成功了!

你在 runat 上有一个拼写错误:

<script ruant="server"> 

尝试修复它,它会起作用!

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!

甜心小果奶 2024-07-17 08:35:14

该问题与没有定义该类的代码隐藏文件有关。 一旦将公共函数放入代码隐藏文件中,它就会起作用。 我认为 ASP.NET 将该函数视为匿名函数,而不是类中的实际函数。

无论如何,这就是我的想法。

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.

离鸿 2024-07-17 08:35:14

这就是我所做的。

  1. 创建一个新网站。
  2. 添加新的 Web 用户控件 - 确保取消选中该对话框上的“将代码放入单独的文件中”复选框。
  3. 将函数放在生成的 .ascx
  4. 中 在 default.aspx 中注册控件并运行

WebUserControl.ascx 的内容

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

<脚本运行=“服务器”> 公共函数 MyFunction() 作为字符串 返回“CalledMyFunction!” 结束功能

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>

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>
滥情哥ㄟ 2024-07-17 08:35:14

我相信您需要从 WebUserControl 派生。

在您的 .ascx“Control”标记中放置以下内容:

Inherits="WebApplication1.WebUserControl1"

当然,您需要使用正确的名称。

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.

睡美人的小仙女 2024-07-17 08:35:14

将其放在声明后面的代码中,

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

此处 myControl1 是用户控件的 id。 现在您可以调用该控件的公共函数。

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.

绅士风度i 2024-07-17 08:35:14

您必须在 vb 文件中添加您的 vb 代码

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

You have to add your vb code in vb file

Public Function MyFunction() As String
        Return "CalledMyFunction!"
    End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文