Search code examples
asp.nethtml-tablecode-behindpager

Codebehind function not being called from aspx page


Probably a "doh" moment, but can't get this codebehind function to fire. In fact debugging shows it's not even being called, which may be indicative when finally displayed on the web page, one only sees:

<table id="dnn_ctr422_ViewPloads_PagerTable">.... 

Notice the missing "style". ANY help would be greatly appreciated.

//aspx code
<table id="PagerTable" style="<%# GetPagerStyle() %>" runat="server">
   <tr>
   <td> 
      <asp:DataPager ID="PloadPager" runat="server" PagedControlID="PloadListView" OnPreRender ="PloadPager_PreRender" PageSize="20" >

//code behind      
protected String GetPagerStyle( )
    {
        return "background-color:" + (String)Settings["TableBackgroundColor"];
    }

Solution

  • From what you show it is not clear why it isn't run. It is possible that you run against a version that is not equal to your source, but I assume you already tried a full rebuild.

    Know that you can set breakpoints in the ASPX page as well. But my hunch is that if this isnt't called, more isn't called. Have you tried breaking on Page_Load or Page_PreRender?

    Another possibility is that your PagerTable object is changed by other code prior to GetPagerStyle being called. In that case, if the style-attribute is altered before it is run may yield this problem. A workaround and to find out whether you actually can set the style is to do the following in Page_Load:

    // correction courtesy of Tim Schmelter ;)
    PagerTable.Style.Add(HtmlTextWriterStyle.BackgroundColor, 
        (String)Settings["TableBackgroundColor"]);
    

    EDIT: Note, as others have pointed out, the following should also work (tried it, as there was some controversy, see other answers):

    <table id="PagerTable" style="<%= GetPagerStyle() %>">
    

    However, note that I removed the runat="server". That means, you cannot use it from the code-behind anymore. I'm just assuming you're not doing anything else with this control in the code-behind, so whether this works for your situation I don't know.