2

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?

2
  • 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
    Commented Aug 24, 2011 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
    Commented Aug 25, 2011 at 7:31

2 Answers 2

4

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.

1
  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.
1
  • 2
    Again, if you use the VS2010 templates properly, you shouldn't need to do any manual steps.
    – James Love
    Commented Aug 24, 2011 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.