Code added to CodeBehind not executing

2019-02-28 15:04发布

问题:

I'm very new to Umbraco, I am still picking my way through how it works so it is entirely possible that I have missed something extremely obvious.

I have been asked to amend how a slider on a MasterPage functions, I've found the markup for the slider is in the .cs file for the MasterPage.

void CreateSlider()
    {
        if (!String.IsNullOrEmpty(CurrentContent.Slider1Image))
        {
            slider.InnerHtml += "<li class='foobar'>";
            if (!String.IsNullOrEmpty(CurrentContent.Slider1Title))
            {
                slider.InnerHtml += "<img src='" + GetMedia(CurrentContent.Slider1Image) + "' alt='' />";
                slider.InnerHtml += "<div class='slider_content bx-pager-item'>";
                slider.InnerHtml += "<h1>" + CurrentContent.Slider1Title + "</h1>";
                if (!String.IsNullOrEmpty(CurrentContent.Slider1VideoButtonTitle) && !String.IsNullOrEmpty(CurrentContent.Slider1VideoLink))
                    slider.InnerHtml += "<span>" + CurrentContent.Slider1VideoButtonTitle + "</span>";
                slider.InnerHtml += "</div>";
                if (!String.IsNullOrEmpty(CurrentContent.Slider1VideoButtonTitle) && !String.IsNullOrEmpty(CurrentContent.Slider1VideoLink))
                {
                    slider.InnerHtml += "<div class='video_wrapper'>";
                    slider.InnerHtml += "<div class='youtube_container'>";
                    slider.InnerHtml += "<div><iframe src='https://player.vimeo.com/video/" + CurrentContent.Slider1VideoLink + "' width='100%' height='542' frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>";
                    slider.InnerHtml += "</div>";
                    slider.InnerHtml += "</div>";
                }
            }
            slider.InnerHtml += "</li>";
        }
}

I have tried adding a class to the <li> but it doesn't not show up in the HTML markup at all. I have tried building the project but with no joy.

Here is the markup that is output:

<%@ Master Language="C#" MasterPageFile="~/masterpages/Base.master" AutoEventWireup="true" Inherits="HomePageType1" Codebehind="HomePageType1.master.cs" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<div class="slider_container">
    <ul id="ContentPlaceHolderDefault_ContentPlaceHolder1_slider" class="bxslider">
        <li>
            <img src='/media/img001.jpg' alt='' />
            <div class='slider_content'>
                <!--  SLIDE CONTENT -->
            </div>
            <div class='video_wrapper'>
                <div class='youtube_container'>
                    <div>
                        <!-- VIDEO URL -->
                    </div>
                </div>
            </div>
        </li>
    </ul>
</div>
</asp:Content>

Any suggestions?

回答1:

Any code added to a .cs file outside of /App_Code/ must be compiled before it "counts" - that part should be dealt with when you build the project.

Also, the master page must reference its code behind for it to pick it up, like

<%@ Master Language="C#"
    AutoEventWireup="true" CodeBehind="MyMasterpage.master.cs" Inherits="MyNamespace.MyMasterpage" %>

Can you perhaps also share the master page content?



回答2:

The reason this was not functioning correctly is because the previous developers had compiled their code but had also uploaded the C# files to the server. So the files we received were compiled.

After much political wrangling we got them to send us the uncompiled files which allowed me to make the code changes as expected.



标签: c# umbraco6