CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

David Hayden [MVP C#]

         .NET Tutorials, Patterns, and Practices

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.

 



Comments

Jimmy Bogard said:

Just curious, does this change the .csproj file?

# December 27, 2007 5:14 PM

rascunho » Blog Archive » links for 2007-12-28 said:

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

# December 28, 2007 3:22 PM

DotNetKicks.com said:

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

# December 28, 2007 5:14 PM

» Daily Bits - December 28, 2007 Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats. said:

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.

# December 28, 2007 9:32 PM

Christopher Steen said:

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

# December 31, 2007 1:22 AM

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 said:

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

# December 31, 2007 5:41 AM

Chris Martin said:

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.... ;)

# January 3, 2008 3:32 AM

Troy Goode said:

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

# January 4, 2008 12:01 AM

幸福 said:

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

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

# January 4, 2008 8:16 PM

All about asp.net » Blog Archive » Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 said:

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

# January 6, 2008 10:02 PM

Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 « .NET Framework tips said:

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

# January 7, 2008 7:44 PM

Enlaces sobre: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS 7 « Thinking in .NET said:

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

# January 8, 2008 5:54 AM

Mutamblog said:

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

# January 17, 2008 12:13 AM

ASP.NET Chinese Blogs said:

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

# January 18, 2008 1:18 PM

Jan 4th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7 « Daily Weblog said:

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

# February 12, 2008 5:31 AM
Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home