Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

<div id="content"> 
    TEST USER CONTROL
    <div>

that is called from a webpart:

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

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"> 
        TEST USER CONTROL
 <% Response.Write(this.select1);  %>
        <div>

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

What I'm doing wrong?

share|improve this question
2  
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
add comment

2 Answers 2

up vote 3 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
add comment
  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
2  
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
add comment

Your Answer

 
discard

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.