开发者

Asp.net web forms control in asp.net mvc

开发者 https://www.devze.com 2023-01-30 02:37 出处:网络
I\'m trying to inject an asp.net classic web control to my asp.net mvc applica开发者_JAVA百科tion. This control doesn\'t use view state or post back so it works fine.

I'm trying to inject an asp.net classic web control to my asp.net mvc applica开发者_JAVA百科tion. 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 %>". 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. This probably means taping into the Page_Load or OnInit events of the Page.

<%@ 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号