如何将用户控件从单独的程序集添加到Sharepoint应用程序页面?

时间:2022-02-02 02:59:16

There is probably a very simple solution to this problem but since I do not know Sharepoint at all and cannot seem to google my way to wisdom on this one maybe one of you guys can steer me in the right direction.

对于这个问题可能有一个非常简单的解决方案,但由于我根本不了解Sharepoint并且似乎无法通过谷歌来解决这个问题,也许你们其中一个人可以引导我朝着正确的方向前进。

Background: Currently I am working in a project where a part of our web solution is constituted by Sharepoint and I have absolutely no previous experience from Sharepoint. Lately I have been struggling with what looks like a very simple task, adding a (web) user control to one of the Sharepoint pages in our solution. There are two important requirements on this task;

背景:目前我正在开发一个项目,其中我们的Web解决方案的一部分由Sharepoint构成,我完全没有来自Sharepoint的经验。最近,我一直在努力解决看起来非常简单的任务,在我们的解决方案中为其中一个Sharepoint页面添加(web)用户控件。这项任务有两个重要的要求;

  • The user control and the code-behind must lie in and make up a separate assembly
  • 用户控件和代码隐藏必须位于并组成一个单独的程序集

  • That assembly must be signed
  • 那个集会必须签署

The user control assembly is really another ASP.NET Web application with a Default.aspx so I may debug the user control with mockups using Cassini. When I debug it through Default.aspx it works fine, the user control is displayed and I can use it as intended.

用户控件程序集实际上是另一个带有Default.aspx的ASP.NET Web应用程序,因此我可以使用Cassini使用模型调试用户控件。当我通过Default.aspx调试它工作正常,显示用户控件,我可以按预期使用它。

I have these lines in my web.config:

我在web.config中有这些行:

<SafeControls>
[..]
<SafeControl Assembly="MyMainNamespace.SubNamespace.SelectorPopup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7fa78676161515a2" Namespace="MyMainNamespace.SubNamespace.SelectorPopup" TypeName=" *" Safe=" True " />
[..]
</SafeControl>

And on the Sharepoint page I have put the following:

在Sharepoint页面上,我提出了以下内容:

<% @Register TagPrefix= "Selector" Namespace="MyMainNamespace.SubNamespace.SelectorPopup" Assembly="MyMainNamespace.SubNamespace.SelectorPopup" %>
[..]
<Selector:SelectorBoxControl runat="server" SelectorType="ContentArea" ></ Selector: SelectorBoxControl>

Our project is an ASP.NET web project on .NET 3.5 (2.0) deployed on IIS6 (Windows Server 2003) and we are using Sharepoint 2007, SQL Server 2005. Build, deploy and Visual Studio markup is fine.

我们的项目是部署在IIS6(Windows Server 2003)上的.NET 3.5(2.0)上的ASP.NET Web项目,我们使用的是Sharepoint 2007,SQL Server 2005.构建,部署和Visual Studio标记很好。

Problem: After deploy to our test environment the user control is not displayed at all on the Sharepoint page and I can´t figure out why.

问题:在部署到我们的测试环境后,Sharepoint页面上根本没有显示用户控件,我无法弄清楚原因。

Build and deploy works fine and the Sharepoint page is even displayed, just that my control is nowhere to be seen. Actually, the HTML response contains absolutely no trace of my user control. Even if I put pure HTML text and no functionality in the control. There are no error messages and the event handler provides no information about possible errors or warnings.

构建和部署工作正常,甚至可以显示Sharepoint页面,只是我的控件无处可见。实际上,HTML响应完全没有我的用户控件的痕迹。即使我在控件中放置纯HTML文本而没有功能。没有错误消息,事件处理程序不提供有关可能的错误或警告的信息。

Am I missing something? Is there anything I need to enable, an attribute I need to add or a reference somewhere? Is there anyway I can get this non-display behavior generate an understandable error message?

我错过了什么吗?我需要启用什么,我需要添加的属性或某处的引用?无论如何,我可以得到这种非显示行为生成一个可理解的错误消息?

2 个解决方案

#1


0  

I think you need to register your dll with the GAC for SharePoint to find it. I could be wrong there though and copying to the bin dir does the trick. But I'm quite sure you need to user the fully qualified name in the Assembly attribute when registering.

我认为您需要在GAC for SharePoint中注册您的dll才能找到它。我可能在那里错了,然后复制到bin dir就可以了。但我很确定您在注册时需要在Assembly属性中使用完全限定名称。

<% @Register TagPrefix= "Selector" Namespace="MyMainNamespace.SubNamespace.SelectorPopup" Assembly="MyMainNamespace.SubNamespace.SelectorPopup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7fa78676161515a2" %>

#2


0  

I'm posting this as an answer because, despite our exchange in comments, I'm not sure you and I have been talking about the same thing.

我发布这个作为答案,因为尽管我们在评论中交流,但我不确定你和我一直在谈论同样的事情。

As you know, there are two kinds of controls in ASP.NET, user controls and web custom controls. User controls have an .ascx file containing markup and - this is important - can't be shared between projects. It does no good to compile the project they're in and reference that assembly, because the .ascx file is not part of the assembly. Web custom controls don't have a markup file: they are entirely created in code, with their controls being created programmatically and added to the Controls collection of the custom control.

如您所知,ASP.NET中有两种控件,用户控件和Web自定义控件。用户控件有一个包含标记的.ascx文件 - 这很重要 - 无法在项目之间共享。编译它们所在的项目并引用该程序集是没有用的,因为.ascx文件不是程序集的一部分。 Web自定义控件没有标记文件:它们完全由代码创建,其控件以编程方式创建并添加到自定义控件的Controls集合中。

So either the third party control in its separate assembly that you're replacing is a web custom control, or - if it uses a separate markup file in its source - it must be using a trick, such as the Virtual Path Provider trick, to achieve this.

因此,要更换的单独程序集中的第三方控件是Web自定义控件,或者 - 如果它在源中使用单独的标记文件 - 它必须使用技巧,例如虚拟路径提供程序技巧,实现这一点。

You've talked about your user control and your code-behind as if they were separate things, which has led me to believe that you are indeed talking about a web user control. But these simply can't be shared between web projects. If your control needs to live in an external assembly, the simplest thing to do is re-write it as a custom control that builds its contents programmatically.

你已经谈到了你的用户控制和你的代码隐藏,好像它们是分开的东西,这让我相信你确实在谈论一个web用户控件。但这些根本无法在Web项目之间共享。如果您的控件需要存在于外部程序集中,最简单的方法是将其重新编写为以编程方式构建其内容的自定义控件。

There are other options, such as the Virtual Path Provider solution, or a famous one from Scott Guthrie (here). But the least tricky option is to write it as a custom control.

还有其他选项,例如Virtual Path Provider解决方案,或来自Scott Guthrie的着名选项(此处)。但最棘手的选择是将其编写为自定义控件。

#1


0  

I think you need to register your dll with the GAC for SharePoint to find it. I could be wrong there though and copying to the bin dir does the trick. But I'm quite sure you need to user the fully qualified name in the Assembly attribute when registering.

我认为您需要在GAC for SharePoint中注册您的dll才能找到它。我可能在那里错了,然后复制到bin dir就可以了。但我很确定您在注册时需要在Assembly属性中使用完全限定名称。

<% @Register TagPrefix= "Selector" Namespace="MyMainNamespace.SubNamespace.SelectorPopup" Assembly="MyMainNamespace.SubNamespace.SelectorPopup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7fa78676161515a2" %>

#2


0  

I'm posting this as an answer because, despite our exchange in comments, I'm not sure you and I have been talking about the same thing.

我发布这个作为答案,因为尽管我们在评论中交流,但我不确定你和我一直在谈论同样的事情。

As you know, there are two kinds of controls in ASP.NET, user controls and web custom controls. User controls have an .ascx file containing markup and - this is important - can't be shared between projects. It does no good to compile the project they're in and reference that assembly, because the .ascx file is not part of the assembly. Web custom controls don't have a markup file: they are entirely created in code, with their controls being created programmatically and added to the Controls collection of the custom control.

如您所知,ASP.NET中有两种控件,用户控件和Web自定义控件。用户控件有一个包含标记的.ascx文件 - 这很重要 - 无法在项目之间共享。编译它们所在的项目并引用该程序集是没有用的,因为.ascx文件不是程序集的一部分。 Web自定义控件没有标记文件:它们完全由代码创建,其控件以编程方式创建并添加到自定义控件的Controls集合中。

So either the third party control in its separate assembly that you're replacing is a web custom control, or - if it uses a separate markup file in its source - it must be using a trick, such as the Virtual Path Provider trick, to achieve this.

因此,要更换的单独程序集中的第三方控件是Web自定义控件,或者 - 如果它在源中使用单独的标记文件 - 它必须使用技巧,例如虚拟路径提供程序技巧,实现这一点。

You've talked about your user control and your code-behind as if they were separate things, which has led me to believe that you are indeed talking about a web user control. But these simply can't be shared between web projects. If your control needs to live in an external assembly, the simplest thing to do is re-write it as a custom control that builds its contents programmatically.

你已经谈到了你的用户控制和你的代码隐藏,好像它们是分开的东西,这让我相信你确实在谈论一个web用户控件。但这些根本无法在Web项目之间共享。如果您的控件需要存在于外部程序集中,最简单的方法是将其重新编写为以编程方式构建其内容的自定义控件。

There are other options, such as the Virtual Path Provider solution, or a famous one from Scott Guthrie (here). But the least tricky option is to write it as a custom control.

还有其他选项,例如Virtual Path Provider解决方案,或来自Scott Guthrie的着名选项(此处)。但最棘手的选择是将其编写为自定义控件。