简体   繁体   中英

ASP.NET MVC - Dynamically set properties of UserControl not rendering correctly

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM