Webforms:.ascx 中的内联文字阻止编译

发布于 2024-10-15 12:11:10 字数 2227 浏览 1 评论 0原文

我的 .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 技术交流群。

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

发布评论

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

评论(1

对你的占有欲 2024-10-22 12:11:10

我没有使用过 DNN,但对于我认为你想要做的事情,内联表达式应该可以工作。

在你的代码隐藏中有一些属性(或方法):

protected string ModuleId {get {return "1"; }}
protected string ModuleId2 {get {return "2"; }}
protected string ControlPath {get { return "path1/"; }}
protected string ControlPath2 {get {return "path2/"; }}
protected string Content {get {return "somecontent"; }}
protected string ControlPath3 {get {return "path3/"; }}

然后你可以按如下方式使用它们:

<div id="ViewDiscovery_<%= ModuleId %>" ></div>
<script type="text/javascript" src="<%= ControlPath %>carousel-jquery.js"></script>
<script type="text/javascript" src="<%= ControlPath2 %>discovery-widget.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var myData = <%= Content %>;
        var myDiscovery = discovery('<%= ControlPath3 %>');
        myDiscovery.json(myData);
        myDiscovery.init("ViewDiscovery_<%= ModuleId2 %>");
    });
</script>

应该生成以下 HTML:

<div id="ViewDiscovery_1" ></div>
<script type="text/javascript" src="path1/carousel-jquery.js"></script>
<script type="text/javascript" src="path2/discovery-widget.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var myData = somecontent;
        var myDiscovery = discovery('path3/');
        myDiscovery.json(myData);
        myDiscovery.init("ViewDiscovery_2");
    });
</script>

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):

protected string ModuleId {get {return "1"; }}
protected string ModuleId2 {get {return "2"; }}
protected string ControlPath {get { return "path1/"; }}
protected string ControlPath2 {get {return "path2/"; }}
protected string Content {get {return "somecontent"; }}
protected string ControlPath3 {get {return "path3/"; }}

then you can use them as follows:

<div id="ViewDiscovery_<%= ModuleId %>" ></div>
<script type="text/javascript" src="<%= ControlPath %>carousel-jquery.js"></script>
<script type="text/javascript" src="<%= ControlPath2 %>discovery-widget.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var myData = <%= Content %>;
        var myDiscovery = discovery('<%= ControlPath3 %>');
        myDiscovery.json(myData);
        myDiscovery.init("ViewDiscovery_<%= ModuleId2 %>");
    });
</script>

Should generate the following HTML:

<div id="ViewDiscovery_1" ></div>
<script type="text/javascript" src="path1/carousel-jquery.js"></script>
<script type="text/javascript" src="path2/discovery-widget.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var myData = somecontent;
        var myDiscovery = discovery('path3/');
        myDiscovery.json(myData);
        myDiscovery.init("ViewDiscovery_2");
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文