0

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?

2
  • out of curiosity, what type of'classic' asp.net web control are you trying to use? Commented Dec 10, 2010 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 Commented Dec 10, 2010 at 11:48

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your response, but this could only work in classic asp.net app (asp.net web forms), not in asp.net mvc
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().
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
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.
Can you please give an example of how to override OnInit function? I'm using the default asp.net mvc view engine
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.