开发者

Webforms: Inline Literals in .ascx preventing compilation

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. 开发者_如何学JAVA 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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜