2010-09-27 54 views
0

我正在开发SharePoint中的发布门户。页面布局,母版页使用Visual Studio设计,我使用wspbuilder将页面布局部署到内容数据库中。在后面的代码中访问页面布局的控件

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

有没有解决方法?

问候, Raghuraman.V

回答

0

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

要做到这一点,你可以创建为WSP封装为 “ASP.NET Web应用程序” Visual Studio项目,创建代码隐藏文件并排侧,并使用WSP 拆除。ASPX页面来自包的C#文件(代码仍然编译到程序集中并与其一起部署)。这个技巧是可行的,因为WSP Builder可以使用Visual Studio项目中的本地配置文件配置 以删除某些文件 类型。

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

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

你必须让用户控件公开在网络控制。

这里展示了如何从父页面更改用户控件的文本框一个简单的例子:

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:

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:

<%@ 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中:

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..."; 
     } 
    } 
} 
+0

嗯,其实保护就足够了... – Bernd 2010-09-27 19:25:26

+0

@Bernd,当我改变文本框来保护,而不是公众,我得到一个错误说WebApplication1.WebUserControl1.UserControlTextBox1由于其保护级别而无法访问。 – MattHughesATL 2010-09-28 17:27:11

+0

对不起 - 我没有正确阅读你的代码。当然,你不能从另一个班级访问受保护的资产 - 我的错误。我的意思是通过在WebUserControl1.ascx.cs中声明受保护的控件,而不是声明一个公共属性来访问代码隐藏控件,例如:protected TextBox TextBox1; – Bernd 2010-09-29 07:27:55

相关问题