从页面后面的代码调用用户控件中的函数
现在这一切都被简化了,但这里是:
我有一个仅包含单个 *.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对我来说很奇怪。
。
。
。
Weird works for me.
.
.
.
确保代码隐藏中的 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.
我不太用 VB(更多的是 C# 人员),但我想我已经成功了!
你在 runat 上有一个拼写错误:
尝试修复它,它会起作用!
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:
Try fixing that and it works!
该问题与没有定义该类的代码隐藏文件有关。 一旦将公共函数放入代码隐藏文件中,它就会起作用。 我认为 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.
这就是我所做的。
WebUserControl.ascx 的内容
Default.aspx 的内容
Here's what I did.
Contents of WebUserControl.ascx
Contents of Default.aspx
我相信您需要从 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.
将其放在声明后面的代码中,
此处 myControl1 是用户控件的 id。 现在您可以调用该控件的公共函数。
Place this in your code behind declarations
here myControl1 is the id of your user control. Now you may call public functions of this control.
您必须在 vb 文件中添加您的 vb 代码
You have to add your vb code in vb file