0

As a follow on from one of my previous posts (ASP.NET MVC2 Master Page - Server side script not rendering, first bracket being escaped) I now having a similar problem.

This time it is with trying to dynamically set the properties of a UserControl.

**For anyone familiar with N2CMS, it is the AdvancedPager control.*

Here is what I am doing:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.master" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewPage<Products>" %>
<%@ Register TagPrefix="AddOn" TagName="Pager" Src="~/Views/SharedParts/PagerControl.ascx" %>
<asp:Content ContentPlaceHolderID="Head" runat="server">
   // content for head section
</asp:Content>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <AddOn:Pager runat="server" ID="AdvPager" PerPage="<%= Model.ItemsPerPage %>" Total="<% Model.Count %>" />
</asp:Content>

The issue appears to be the value being set in the PerPage or Total property. I can't debug this because I don't have the source, however, I believe it is the same problem I was getting in my previous post because if I replace the dynamic ASP code with hard-coded values it works fine.

Any idea's of a work-around? I have tried putting the entire thing into a String.Format but that simply outputs <AddOn:Pager runat="server"... to the page, it doesn't actually render the control correctly.

Community
  • 1
  • 1
James
  • 80,725
  • 18
  • 167
  • 237

1 Answers1

3

UserControls with runat="server" are a no-no in ASP.NET MVC. They are dependent on the classic WebForms life-cycle which no longer exists in MVC, not even talking about ViewState and PostBack models.

Looking at the name of this control it looks like a pager. There's a nice collection of HTML helpers for rendering paginated grids in ASP.NET MVC you might take a look at.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have tried removing the `runat="server"` tag but then the control doesn't render at all. I am using this in conjunction with the N2CMS MVC version so for whatever reason I do need the runat attribute. – James Oct 25 '10 at 13:18
  • 1
    @James, yes you need the runat="server" attribute but this doesn't work with ASP.NET MVC. Use a classic web forms application if you want to use server controls, not MVC. – Darin Dimitrov Oct 25 '10 at 13:21
  • I have already implemented most of the site using the MVC framework so switching to WebForms is out of the question. So it looks like I will need to roll my own control for this? – James Oct 25 '10 at 13:27
  • Yes, you will need to either roll your own HTML helper (not control, the notion of control doesn't exist in MVC) or use one of the existing. – Darin Dimitrov Oct 25 '10 at 13:28