Webforms:.ascx 中的内联文字阻止编译
我的 .ascx 文件中有以下代码片段:
<%@ Control Language="C#" Inherits="MyCompany.Modules.Discovery.ViewDiscovery"
AutoEventWireup="true" CodeBehind="ViewDiscovery.ascx.cs" %>
<div id="ViewDiscovery_<asp:Literal ID="litModuleId" runat="server" />"></div>
<script type="text/javascript" src="<asp:Literal ID="litControlPath" runat="server" />carousel-jquery.js"></script>
<script type="text/javascript" src="<asp:Literal ID="litControlPath2" runat="server" />discovery-widget.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var myData = <asp:Literal ID="litContent" runat="server" />;
var myDiscovery = discovery('<asp:Literal ID="litControlPath3" runat="server" />');
myDiscovery.json(myData);
myDiscovery.init("ViewDiscovery_<asp:Literal ID="litModuleId2" runat="server" />");
});
</script>
正如您可能能够从语法突出显示(或者更确切地说,它有多么糟糕)推断出的那样,如果我尝试使用此 .ascx 文件编译我的 CodeBehind 文件,它会严重失败,因为它无法识别我散布在文件中的各种文字,因此我的代码隐藏文件无法编译。我必须将它们从其位置中删除,将它们放在文件末尾,不受页面中上下文的阻碍,进行编译,然后恢复到原始版本。
当然,有更好的方法可以将文字放入页面中。
编辑:当我打包我的模块并将其安装在 DotNetNuke 的另一个实例上时(只要我在编译时执行该 rigamarole,它就可以在本地正常工作),它会抛出一个错误:
DotNetNuke.Services.Exceptions.ModuleLoadException: The tag contains duplicate 'ID' attributes
相关,或者是这里还有其他问题吗?
编辑:我尝试使用<%=variable %>
但没有成功。这是我的代码隐藏的片段:
namespace MyCompany.Modules.Discovery
{
partial class ViewDiscovery : PortalModuleBase, IActionable
{
public string strContent = "Insert Content Here!";
protected void Page_Load(object sender, System.EventArgs e)
{
// Do stuff here!
this.strContent = "My content!";
}
}
}
如果我在我的 ascx 文件中执行此操作:
<%@ Control Language="C#" Inherits="MyCompany.Modules.Discovery.ViewDiscovery"
AutoEventWireup="true" CodeBehind="ViewDiscovery.ascx.cs" %>
<%=this.strContent %>
...我的页面上什么也没有显示。查看 .ascx 文件中“this”的上下文会发现它的类型是“ASP.viewdiscovery_ascx”,而不是我预期的“ViewDiscovery”类型。
I have the following snippit in my .ascx file:
<%@ Control Language="C#" Inherits="MyCompany.Modules.Discovery.ViewDiscovery"
AutoEventWireup="true" CodeBehind="ViewDiscovery.ascx.cs" %>
<div id="ViewDiscovery_<asp:Literal ID="litModuleId" runat="server" />"></div>
<script type="text/javascript" src="<asp:Literal ID="litControlPath" runat="server" />carousel-jquery.js"></script>
<script type="text/javascript" src="<asp:Literal ID="litControlPath2" runat="server" />discovery-widget.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var myData = <asp:Literal ID="litContent" runat="server" />;
var myDiscovery = discovery('<asp:Literal ID="litControlPath3" runat="server" />');
myDiscovery.json(myData);
myDiscovery.init("ViewDiscovery_<asp:Literal ID="litModuleId2" runat="server" />");
});
</script>
As you might be able to infer from the syntax highlighting (or rather, how broken it is), if I try to compile my CodeBehind file with this .ascx file, it fails horribly because it doesn't recognize the various literals that I have sprinkled around in the file, and thus my codebehind file fails to compile. I have to remove them from their positions, put them at the end of the file unencumbered by their context in the page, compile, then revert to the original version.
Surely there is nicer way of going about putting literals in the page.
EDIT: When I package my module up and install it on another instance of DotNetNuke (it works fine locally as long as I do that rigamarole when I compile it), it throws an error:
DotNetNuke.Services.Exceptions.ModuleLoadException: The tag contains duplicate 'ID' attributes
Related, or is there some other issue going on here?
EDIT: I have tried to use <%=variable %>
but haven't gotten anywhere. This is a snippit from my codebehind:
namespace MyCompany.Modules.Discovery
{
partial class ViewDiscovery : PortalModuleBase, IActionable
{
public string strContent = "Insert Content Here!";
protected void Page_Load(object sender, System.EventArgs e)
{
// Do stuff here!
this.strContent = "My content!";
}
}
}
If I then do this in my ascx file:
<%@ Control Language="C#" Inherits="MyCompany.Modules.Discovery.ViewDiscovery"
AutoEventWireup="true" CodeBehind="ViewDiscovery.ascx.cs" %>
<%=this.strContent %>
...nothing shows up on my page. A peek at the context of 'this' in the .ascx file reveals it to be of type "ASP.viewdiscovery_ascx", not "ViewDiscovery" as I expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有使用过 DNN,但对于我认为你想要做的事情,内联表达式应该可以工作。
在你的代码隐藏中有一些属性(或方法):
然后你可以按如下方式使用它们:
应该生成以下 HTML:
I've not used DNN, but for what I think you're trying to do, inline expressions should work.
In your code-behind have some properties (or methods):
then you can use them as follows:
Should generate the following HTML: