Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to inject an asp.net classic web control to my asp.net mvc application. 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?

share|improve this question
 
out of curiosity, what type of'classic' asp.net web control are you trying to use? –  RPM1984 Dec 10 '10 at 11:36
 
It it a third-party control for file uploading I was using in asp.net webforms before switching to mvc. It just renders an appropriate html markup for a silverlight app –  Lenin Dec 10 '10 at 11:48
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
 
Thank you for your response, but this could only work in classic asp.net app (asp.net web forms), not in asp.net mvc –  Lenin Dec 13 '10 at 8:02
 
The code above works just fine in an ASP.NET MVC app. To the best of my knowledge, there is no other way to call .DataBind(), unless you have access to the controls source code where you can manually add the call to .DataBind(). –  geoffrey.mcgill Dec 13 '10 at 20:11
 
Adding OnInit handler will work in asp.net webforms, but I beleive not in mvc. And I think yes, it would be the best solution to add the call to DataBind() somewhere in the control's source, but unfortunalty I don't have access to it –  Lenin Dec 14 '10 at 7:23
 
You can add/call the OnInit page handler if using the default ASP.MVC View engine. I haven't tested other View engines (like Razor), although I suspect it's no different, but I could be wrong about that one. –  geoffrey.mcgill Dec 14 '10 at 21:33
 
Can you please give an example of how to override OnInit function? I'm using the default asp.net mvc view engine –  Lenin Dec 15 '10 at 8:12
show 2 more comments

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.