SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a User Control (myUserControl.ascx) with the code:

<div id="content"> 

that is called from a webpart:

protected override void OnLoad(EventArgs e) {
 Control c = Page.LoadControl(_ascxPath);
   if (c != null)

since here it's all ok.

Now I want to add a code-behind to this User Control

1) I created myUserControl.ascx.cs

public partial class SP4ITeditXML : UserControl

 public string select1 = "test text";

2) I changed the myUserControl.ascx:

<%@ Control Language="C#" CodeBehind="myUserControl.ascx.cs"%>
<div id="content"> 
 <% Response.Write(this.select1);  %>

but then I get the error CS0117 (myUserControl.ascx not contains a definition for select1).

What I'm doing wrong?

share|improve this question
When you add the USer Control from the "Add" menu, selecting User Control from the Visual Studio templates, it should create both the ASCX and the ASCX.CS files for you... – James Love Aug 24 '11 at 10:00
@James Love, wow, that's true. The problem was that I create the class manually, but creating from the Add menu, Visual Studio add the code properly. If you want to write it like a answer instead a comment, I'll accept it – user674887 Aug 25 '11 at 7:31
up vote 4 down vote accepted

Commented migrated to answer:

Add the User Control via the templates, do Project -> Add -> New Item -> User Control.

This creates the ASCX and the associated CS file.

share|improve this answer
  1. Compile the myUserControl.ascx.cs to a signed assembly.
  2. Reference the myUserControl.ascx.cs with the full qualified name in the myUserControl.ascx
  3. Add the assembly to the GAC.
share|improve this answer
Again, if you use the VS2010 templates properly, you shouldn't need to do any manual steps. – James Love Aug 24 '11 at 10:05

Your Answer


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.