Can't address asp-Controls on MVC View Pages in CodeBehind file

Last post 12-27-2007 12:40 PM by hannesBrazil. 5 replies.

Sort Posts:

  • Can't address asp-Controls on MVC View Pages in CodeBehind file

    12-27-2007, 11:24 AM
    • Member
      6 point Member
    • hannesBrazil
    • Member since 12-27-2007, 11:04 AM
    • Posts 18

    Hello,

    I have a MVC ViewPage with a ListView control on it:


     <asp:ListView ID="CarsListView" runat="server">
                <LayoutTemplate>
                    <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
                </LayoutTemplate>
                <ItemTemplate>
                    ...
                </ItemTemplate>
            </asp:ListView>

     

    Then I want to fill the ListView in the page_load event handler of the codebehind-page:


       public partial class Thumbs : ViewPage<CarsViewData>
        {

            protected void Page_Load(object sender, EventArgs e)
            {
      CarsListView.DataSource = CarsViewData.MyCarsList;
      CarsListView.DataBind();
     }
        }


    I get a compilation error on the first and second line of "Page_Load", because the compiler doesn't find the "CarListView" Object:
    "The name 'CarsListView' does not exist in the current context". The same problem exists with every asp Control, e.g. an <asp:button>. None of them can be addressed in the code behind file.

    What am I doing wrong? I think my example should work when I look at the "Rendering Approach 2: Using Server Side Controls" part of ScottGu's Blog Post: http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx

     

    Second problem: 

    What is also strange is that when I want to use the HtmlHelper-Objcet "Hmtl" it is null. So for example this code

       public partial class Thumbs : ViewPage<CarsViewData>
        {

            protected void Page_Load(object sender, EventArgs e)
            {
      Html.ActionLink(....);
     }
        }

    throws a runtime exception because Html is null.

    Mybe I have a corrupt installation?

    I have installed the VS 2008 Team suite trial. In fact I did a lot of installing these days, so I don't remember if I have installed the ASP.NET 3.5 Extensions CTP manually or if it was shipped with the trial version.
    I am using a reference to the MVCToolkit.

    Thanks for your hints,
    Hannes

    Filed under:
  • Re: Can't address asp-Controls on MVC View Pages in CodeBehind file

    12-27-2007, 11:28 AM
    Answer
    • Member
      59 point Member
    • brianjlowry
    • Member since 11-07-2006, 2:13 PM
    • Richmond, VA
    • Posts 46

    "CarListView" not appearing in the designer file is a known issue. Try right clicking on the Thumbs page/control and hit "Convert to Web Application". That should take care of it.

  • Re: Can't address asp-Controls on MVC View Pages in CodeBehind file

    12-27-2007, 12:01 PM
    • Member
      6 point Member
    • hannesBrazil
    • Member since 12-27-2007, 11:04 AM
    • Posts 18

    Hi Brian,

    thanks a lot, that helped me out. After applying "Convert to Web Application" I can perfectly address my asp controls.

     

    Concering my second problem:

    I found out that the "Html" object is only null when I try to use it on my "MVC View User Control". I tried to convert this also to a Web Application, what succeeded, but still, the Html-ojbcet ist null and I get the runtime exception (object is null ...).

    Using the Html object on a normal MVC View Page is no problem, there it is not null.

    Hannes

  • Re: Can't address asp-Controls on MVC View Pages in CodeBehind file

    12-27-2007, 12:18 PM
    • Member
      59 point Member
    • brianjlowry
    • Member since 11-07-2006, 2:13 PM
    • Richmond, VA
    • Posts 46

    Which method are you calling on the HtmlHelpers?

    Not sure if this helps:

    http://forums.asp.net/t/1193081.aspx


     

     

  • Re: Can't address asp-Controls on MVC View Pages in CodeBehind file

    12-27-2007, 12:20 PM
    • Member
      59 point Member
    • brianjlowry
    • Member since 11-07-2006, 2:13 PM
    • Richmond, VA
    • Posts 46
  • Re: Can't address asp-Controls on MVC View Pages in CodeBehind file

    12-27-2007, 12:40 PM
    • Member
      6 point Member
    • hannesBrazil
    • Member since 12-27-2007, 11:04 AM
    • Posts 18

    I am using Hmtl.ActionLink<MyController>(...)

    I also tried the non-generic Html.ActionLink( ... ); 

    Both same result.

    I think you are right, this issue is treated in the Thread  http://forums.asp.net/t/1192730.aspx  that you have posted.

Page 1 of 1 (6 items)