在后面的代码中访问页面布局的控件

时间:2020-12-21 15:51:51

I am developing a publishing portal in SharePoint. The page layouts, master pages are designed using Visual Studio and I deploy the page layouts into the content database using wspbuilder.

我正在SharePoint中开发一个发布门户。页面布局,母版页使用Visual Studio设计,我使用wspbuilder将页面布局部署到内容数据库中。

I have a requirement wherein I have to access the controls of the page layout in code behind and assign or get values to/from the controls. But, the VS intellisense never shows the controls used in my page layout. What should I do in order to access the controls using code behind?

我有一个要求,我必须在后面的代码中访问页面布局的控件,并从控件中分配或获取值。但是,VS intellisense从不显示我的页面布局中使用的控件。我应该怎么做才能使用后面的代码访问控件?

Is there any workaround for this?

这有什么解决方法吗?

Regards, Raghuraman.V

此致,Raghuraman.V

2 个解决方案

#1


0  

I guess that you have the page layouts and the code-behind in two different projects or at least in two different locations. You can also use 'real' code-behind pages in SharePoint that are side-by-side with the ASPX file so that you do not have to re-declare the controls at all.

我猜你在两个不同的项目中或至少在两个不同的位置有页面布局和代码隐藏。您还可以在SharePoint中使用与ASPX文件并排的“真实”代码隐藏页面,这样您就不必重新声明控件。

To do so you can create the Visual Studio project for the WSP package as an “ASP.NET Web Application”, create ASPX pages with code-behind files side-by-side and use WSP Builder to remove the C# files from the package (the code is still compiled into the assembly and deploy with it). The trick works because WSP Builder can be configured with a local configuration file within the Visual Studio project to remove certain file types.

为此,您可以将WSP包的Visual Studio项目创建为“ASP.NET Web应用程序”,并排创建具有代码隐藏文件的ASPX页面,并使用WSP Builder从包中删除C#文件(代码仍然编译到程序集中并随之部署)。这个技巧很有效,因为可以在Visual Studio项目中使用本地配置文件配置WSP Builder以删除某些文件类型。

Here the local WSPBuilder.exe.config file:

这里是本地WSPBuilder.exe.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
  <add key="Excludefiletypes" value="cs" />
 </appSettings>
</configuration>

#2


1  

You have to make the web controls on the user control public.

您必须公开用户控件上的Web控件。

Here's a quick example showing how to change a user control's textbox from the parent page:

这是一个快速示例,说明如何从父页面更改用户控件的文本框:

WebUserControl1.ascx:

WebUserControl1.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

WebUserControl1.ascx.cs:

WebUserControl1.ascx.cs:

using System;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        public TextBox UserControlTextBox1
        {
            get { return TextBox1; }
            set { TextBox1 = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

WebForm1.aspx:

WebForm1.aspx的:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">       
        <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
    </div>
    </form>
</body>
</html>

WebForm1.aspx.cs:

WebForm1.aspx.cs中:

using System;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl11.UserControlTextBox1.Text = "Your text here...";
        }
    }
}

#1


0  

I guess that you have the page layouts and the code-behind in two different projects or at least in two different locations. You can also use 'real' code-behind pages in SharePoint that are side-by-side with the ASPX file so that you do not have to re-declare the controls at all.

我猜你在两个不同的项目中或至少在两个不同的位置有页面布局和代码隐藏。您还可以在SharePoint中使用与ASPX文件并排的“真实”代码隐藏页面,这样您就不必重新声明控件。

To do so you can create the Visual Studio project for the WSP package as an “ASP.NET Web Application”, create ASPX pages with code-behind files side-by-side and use WSP Builder to remove the C# files from the package (the code is still compiled into the assembly and deploy with it). The trick works because WSP Builder can be configured with a local configuration file within the Visual Studio project to remove certain file types.

为此,您可以将WSP包的Visual Studio项目创建为“ASP.NET Web应用程序”,并排创建具有代码隐藏文件的ASPX页面,并使用WSP Builder从包中删除C#文件(代码仍然编译到程序集中并随之部署)。这个技巧很有效,因为可以在Visual Studio项目中使用本地配置文件配置WSP Builder以删除某些文件类型。

Here the local WSPBuilder.exe.config file:

这里是本地WSPBuilder.exe.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
  <add key="Excludefiletypes" value="cs" />
 </appSettings>
</configuration>

#2


1  

You have to make the web controls on the user control public.

您必须公开用户控件上的Web控件。

Here's a quick example showing how to change a user control's textbox from the parent page:

这是一个快速示例,说明如何从父页面更改用户控件的文本框:

WebUserControl1.ascx:

WebUserControl1.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

WebUserControl1.ascx.cs:

WebUserControl1.ascx.cs:

using System;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        public TextBox UserControlTextBox1
        {
            get { return TextBox1; }
            set { TextBox1 = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

WebForm1.aspx:

WebForm1.aspx的:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">       
        <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
    </div>
    </form>
</body>
</html>

WebForm1.aspx.cs:

WebForm1.aspx.cs中:

using System;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl11.UserControlTextBox1.Text = "Your text here...";
        }
    }
}

相关文章