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
    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
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.
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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