简体   繁体   English

ASP.NET MVC中的ASP.NET Web表单控件

[英]Asp.net web forms control in asp.net mvc

I'm trying to inject an asp.net classic web control to my asp.net mvc application. 我正在尝试将asp.net经典Web控件注入到我的asp.net mvc应用程序中。 This control doesn't use view state or post back so it works fine. 此控件不使用视图状态或回发,因此可以正常工作。

But I can't plug a real-time provided value in its attribute. 但是我无法在其属性中插入实时提供的值。 This code 这段代码

<my:Control runat="server" Tag="<%: Model.ID %>" />

ends up with the tag value just set explicitly to "<%: Model.ID %>". 最终将标记值设置为明确设置为“ <%:Model.ID%>”。 Is there a way to make it work? 有办法使它起作用吗?

I believe the syntax is as follows: 我相信语法如下:

<my:Control runat="server" Tag="<%# Model.ID %>" />

The other gotcha is you must call .DataBind() on the Control at some point after the Control has been initialized. 另一个陷阱是必须在控件初始化后的某个时候在控件上调用.DataBind()。 This probably means taping into the Page_Load or OnInit events of the Page. 这可能意味着利用Page的Page_Load或OnInit事件。

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<script runat="server">
    protected override void OnInit(EventArgs e)
    {
        this.Label1.DataBind();
        base.OnInit(e);
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%= Html.Encode(ViewData["Message"]) %></h2>

    <asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>" />
</asp:Content>

Or, if you have access to the source, you could add a call to .DataBind() somewhere before the final Render. 或者,如果您有权访问源,则可以在最终Render之前的某个位置添加对.DataBind()的调用。 You would have to experiment with that. 您将不得不尝试一下。

Hope this helps. 希望这可以帮助。

Well, seems like the only way to do this is injecting code the control renders to the page directly. 好吧,看来这样做的唯一方法是将控件直接呈现给页面的代码注入。 In my case the control renders a silverlight object with some javascript so I put it at the page as is. 在我的情况下,该控件使用一些JavaScript渲染了silverlight对象,因此我将其照原样放在页面上。

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

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