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 am creating a custom form template for a list and this form template uses a lot of controls such as panels and dropdowns. I am using a rendering template in the controltemplates folder and have this working. I want to now add code-behind to the control template and I can not seem to find an example of how to do this correctly. I am using VSeWSS 1.3 and VS 2008 and have my solution all setup. I just want to know how to setup a code-behind page for this template.

share|improve this question
add comment

5 Answers

up vote 2 down vote accepted

Just build (and sign) your code beforehand, drop it in the bin_app_bin and drop the class declaration in the <@Control tag.

<%@ Control Language="C#" Inherits="YourNamespace.Class,YourAssembly.AssemblyName,Version=1.0.0.0,Culture=neutral,PublicKeyToken=YourPublicKeyToken" %>

share|improve this answer
    
I tried to do this but was not successful. If I remove this the page seems to load properly but is useless. I feel that I am not correctly building the code-behind page correctly. I am not sure if I am creating a class and what it is supposed to inherit from and what functions to call on the page. –  spevilgenius Apr 20 '10 at 9:50
    
How is your project setup? Is it a web application project? The way I do it, I have two projects in my solution - one for ascx\aspx and one for .cs. The .cs compiles down, which I drop into the GAC (or _app_bin, or bin, depending on your setup\trust levels). The control declaration includes the name of the class and the assembly built in the .cs project. It's hard to fully explain in this little box - I can post a code sample if you'd like. –  John Dandison Apr 20 '10 at 21:48
    
I normally do not have an issue doing what I want but this is a VSeWSS 1.3 wsp solution package for a new site definition. I have a list and I am using the schema.xml file to override the listform template by telling it to use my custom template. This is supposed to be "How Microsoft Does It" I copied the ListControl template from the defaulttemplates.ascx file and replaced the list field iterator with my own form controls. I put this in my Templates/Controltemplates folder in my solution and it works fine to this point. I need to add code to this ascx page! –  spevilgenius Apr 21 '10 at 0:01
    
I'll post a code sample hopefully this evening, if not tomorrow. Going out of town for a couple days so lots to do. Look for it soon! –  John Dandison Apr 27 '10 at 18:06
1  
Ok, I've gotten up a quick little sample. I'm not sure if it's exactly what you're looking for, so let me know if it's not. Essentially, I'm just creating a standard UserControl, dropping the assembly in the GAC\BIN, registering the tag in DefaultTemplates.ascx in the RenderingTemplate desired. johndandison.com/blog/post/2010/04/27/… –  John Dandison Apr 27 '10 at 18:49
add comment

Theres alot of examples of this out there on how to do this.

This was the first example google gave me

http://jamestsai.net/Blog/post/Using-ASPNET-Web-User-Control-with-Code-Behind-in-SharePoint.aspx

share|improve this answer
    
That is not exactly what I am doing as I already found that example. I am overriding the form template for the form using an ascx rendering template. The newform.aspx page and editform.aspx page will have new rendering templates. I want to add code-behind to those .ascx pages. I do not know how to setup the code-behind to attach and what class type I should use. Thanks for the example! –  spevilgenius Apr 20 '10 at 0:25
add comment

As I was not successful in getting this to work right, I tried to understand if I really had to do it the way I was doing it. As it turns out, I was able to instead build a custom webpart and put my controls in there and then just used a different page for the new and edit forms. Not sure if this will really help anyone else it is just what I did to get the job done.

share|improve this answer
add comment

I'm having the same problem as the OP also. I would like to here more from John Dandison if you're still listening!

I've added the inherits attribute with the 5 part name to the CONTROL directive. My code behind class looks like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace MyNamespace
{
    public class CustomRenderingTemplate : UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
    }
}

The rendering template is loading ok. I've added a break point to the 2 overriden methods and neither is being hit so I'm assuming it's not wired up ok? Can you help me out? Am I inheriting from the correct class - UserControl?

share|improve this answer
    
I never could get this wired up correctly. What I did do was move my code to inherit from Page and changed the inherits of the NewForm.aspx page to this and it seems to work. I had to move the controls over to the NewForm.aspx page but that was not really an issue. I also had to hide the actual listform webpart so the standard controls would not be visiable. Not what I wanted to do, but it works... –  spevilgenius Apr 26 '10 at 15:33
add comment

http://www.andrewconnell.com/blog/articles/UsingCodeBehindFilesInSharePointSites.aspx

I don't use the VSeWSS, but the process is the same. You need to get the 5-part name of the code behind and wire it up in the ASPX's Inherits attribute in the Page directive.

share|improve this answer
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.