简体   繁体   English

ASP.NET MVC-动态设置的UserControl属性无法正确呈现

[英]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. 作为我以前的一篇文章的后续内容( ASP.NET MVC2母版页-服务器端脚本未呈现,第一个括号被转义了 ),我现在遇到类似的问题。

This time it is with trying to dynamically set the properties of a UserControl. 这次是尝试动态设置UserControl的属性。

**For anyone familiar with N2CMS, it is the AdvancedPager control.* **对于熟悉N2CMS的任何人,它都是AdvancedPager控件。*

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. 问题似乎是在PerPage或Total属性中设置的值。 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. 我无法调试此代码,因为我没有源代码,但是,我认为这与我以前的帖子中遇到的问题相同,因为如果我用硬编码值替换动态ASP代码,则可以正常工作。

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. 我已经尝试将整个内容放入String.Format但是仅将<AddOn:Pager runat="server"...到页面,它实际上无法正确呈现控件。

UserControls with runat="server" are a no-no in ASP.NET MVC. 在ASP.NET MVC中,带有runat="server" UserControl是禁止的。 They are dependent on the classic WebForms life-cycle which no longer exists in MVC, not even talking about ViewState and PostBack models. 它们依赖于经典的WebForms生命周期,该生命周期在MVC中不再存在,甚至不讨论ViewState和PostBack模型。

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. 您可以查看一下用于在ASP.NET MVC中渲染分页网格的大量HTML帮助器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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