David Hayden [MVP C#]

Sponsors

The Lounge

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
ASP.NET MVC and Accessing Server Controls in CodeBehind

If you are new to MVC in ASP.NET, it is only natural that your first attempt at using it will involve the use of server controls. If you create a blank ASP.NET MVC Website and add a Repeater Control to the Index View of the Home Controller:

 

ASP.NET MVC Tutorial 

 

and try to access it from the codebehind, you might freak out a bit that you won't be able to access the Repeater Control.

The problem is that we have all been spoiled by the designer file that is normally associated with Web Application Projects. As you drop server controls on the form, controls are automatically declared in a partial class in the designer file. If you check out your ASP.NET MVC solution, you will notice the absence of these designer files. Adding them is as simple as right-clicking on the view file and choosing "Convert to Web Application".

 

ASP.NET MVC Conver to Web Application 

 

 

This will add the designer file associated with the view.

 

 

 

 

The Repeater Control will now be declared in the designer file:

 

public partial class Index {
    
    /// <summary>
    /// Repeater1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.Repeater Repeater1;
}

 

and available in the code-behind file.

 

 

 

Hopefully this helps.

 


Posted 12-27-2007 2:28 PM by David Hayden

[Advertisement]

Comments

Jimmy Bogard wrote re: ASP.NET MVC and Accessing Server Controls in CodeBehind
on 12-27-2007 5:14 PM

Just curious, does this change the .csproj file?

rascunho » Blog Archive » links for 2007-12-28 wrote rascunho &raquo; Blog Archive &raquo; links for 2007-12-28
on 12-28-2007 3:22 PM

Pingback from  rascunho  &raquo; Blog Archive   &raquo; links for 2007-12-28

DotNetKicks.com wrote ASP.NET MVC and Accessing Server Controls in CodeBehind
on 12-28-2007 5:14 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

» Daily Bits - December 28, 2007 Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats. wrote &raquo; Daily Bits - December 28, 2007 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.
on 12-28-2007 9:32 PM

Pingback from  &raquo; Daily Bits - December 28, 2007 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.

Christopher Steen wrote Link Listing - December 30, 2007
on 12-31-2007 1:22 AM

AJAX ASP.NET AJAX Control Toolkit - Basic Sample For DynamicPopulate Control [Via: alikl ] ASP.NET ...

Wöchentliche Rundablage: ASP.NET MVC, ASP.NET 3.5, ADO.NET Data Services, C# 3.0, ASP.NET AJAX, LINQ, WPF, Surface, Javascript | Code-Inside Blog wrote W&ouml;chentliche Rundablage: ASP.NET MVC, ASP.NET 3.5, ADO.NET Data Services, C# 3.0, ASP.NET AJAX, LINQ, WPF, Surface, Javascript | Code-Inside Blog
on 12-31-2007 5:41 AM

Pingback from  W&ouml;chentliche Rundablage: ASP.NET MVC, ASP.NET 3.5, ADO.NET Data Services, C# 3.0, ASP.NET AJAX, LINQ, WPF, Surface, Javascript | Code-Inside Blog

Chris Martin wrote re: ASP.NET MVC and Accessing Server Controls in CodeBehind
on 01-03-2008 3:32 AM

Besides not have postback and viewstate, isn't this just opening one up to the problems of webforms? Don't we want our controllers independent of our views?

This guy's confused.... ;)

Troy Goode wrote re: ASP.NET MVC and Accessing Server Controls in CodeBehind
on 01-04-2008 12:01 AM

ScottGu has confirmed that it is a template bug that will be fixed in the next CTP. You may want to take a look at my recent post containing a template fix that automatically creates the designer files for you. No more right-clicking and selecting "Convert to Web Application"!

www.squaredroot.com/.../MVC-Template-Fix.aspx

幸福 wrote ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7
on 01-04-2008 8:16 PM

原文地址】Jan4thLinks:ASP.NET,ASP.NETAJAX,ASP.NETMVC,VisualStudio,IIS7

【原文发表日期】Friday,Janua...

All about asp.net » Blog Archive » Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 wrote All about asp.net &raquo; Blog Archive &raquo; Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7
on 01-06-2008 10:02 PM

Pingback from  All about asp.net  &raquo; Blog Archive   &raquo; Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7

Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 « .NET Framework tips wrote Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 &laquo; .NET Framework tips
on 01-07-2008 7:44 PM

Pingback from  Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 &laquo; .NET Framework tips

Enlaces sobre: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS 7 « Thinking in .NET wrote Enlaces sobre: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS 7 &laquo; Thinking in .NET
on 01-08-2008 5:54 AM

Pingback from  Enlaces sobre: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS 7 &laquo; Thinking in .NET

Mutamblog wrote Links 4/Jan: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7
on 01-17-2008 12:13 AM

Links 4/Jan: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7

ASP.NET Chinese Blogs wrote 1月4日链接篇:ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7
on 01-18-2008 1:18 PM

【原文地址】 Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 【原文发表日期】 Friday, January

Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 « Daily Weblog wrote Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 &laquo; Daily Weblog
on 02-12-2008 5:31 AM

Pingback from  Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 &laquo; Daily Weblog

http://rails.codebetter.com/blogs/david.hayden/archive/2007/12/27/asp-net-mvc-and-accessing-server-controls-in-codebehind.aspx wrote http://rails.codebetter.com/blogs/david.hayden/archive/2007/12/27/asp-net-mvc-and-accessing-server-controls-in-codebehind.aspx
on 03-20-2008 1:53 AM
http://httphandler.codebetter.com/blogs/david.hayden/archive/2007/12/27/asp-net-mvc-and-accessing-server-controls-in-codebehind.aspx wrote http://httphandler.codebetter.com/blogs/david.hayden/archive/2007/12/27/asp-net-mvc-and-accessing-server-controls-in-codebehind.aspx
on 03-28-2008 5:54 AM